use std::future::Future;
use std::pin::Pin;
use std::time::Duration;
pub enum ScheduleConfig {
Interval(Duration),
IntervalWithDelay {
interval: Duration,
initial_delay: Duration,
},
Cron(String),
}
pub struct ScheduledTaskDef<T: Clone + Send + Sync + 'static> {
pub name: String,
pub schedule: ScheduleConfig,
pub task: Box<dyn Fn(T) -> Pin<Box<dyn Future<Output = ()> + Send>> + Send + Sync>,
}
pub trait ScheduledResult {
fn log_if_err(self, task_name: &str);
}
impl ScheduledResult for () {
fn log_if_err(self, _: &str) {}
}
impl<E: std::fmt::Display> ScheduledResult for Result<(), E> {
fn log_if_err(self, task_name: &str) {
if let Err(e) = self {
tracing::error!(task = %task_name, error = %e, "Scheduled task failed");
}
}
}
pub type SchedulerStartFn<T> = Box<
dyn FnOnce(Vec<ScheduledTaskDef<T>>, T) + Send,
>;
pub type SchedulerStopFn = Box<dyn FnOnce() + Send>;