In Fang 0.9+, you can schedule tasks by implementing the cron method within your Runnable (for blocking workers) or AsyncRunnable (for asynk workers) trait implementation. You must use the fang::Scheduled enum to define the schedule.
CRON Patterns
To execute a task periodically, return Scheduled::CronPattern with a valid cron expression. For example, to run a task every 20 seconds, use the expression "0/20 * * * * * *".
One-time Scheduling
To schedule a task to run once at a specific time in the future, return Scheduled::ScheduleOnce containing a DateTime<Utc> value.
Note: You do not need to start a separate scheduler process; WorkerPool or AsyncWorkerPool will automatically handle re-scheduling periodic tasks.
// Example: Periodic CRON task
impl AsyncRunnable for MyCronTask {
async fn run(&self, _queue: &mut dyn AsyncQueueable) -> Result<(), Error> {
log::info!("CRON!!!!!!!!!!!!!!!",);
Ok(())
}
fn cron(&self) -> Option<Scheduled> {
// cron expression to execute a task every 20 seconds.
let expression = "0/20 * * * * * *";
Some(Scheduled::CronPattern(expression.to_string()))
}
fn uniq(&self) -> bool {
true
}
}
// Example: One-time scheduled task
impl AsyncRunnable for MyCronTask {
async fn run(&self, _queue: &mut dyn AsyncQueueable) -> Result<(), Error> {
log::info!("CRON!!!!!!!!!!!!!!!",);
Ok(())
}
fn cron(&self) -> Option<Scheduled> {
// Schedules the task for 7 seconds in the future
Some(Scheduled::ScheduleOnce(Utc::now() + Duration::seconds(7i64)))
}
fn uniq(&self) -> bool {
true
}
}