A simple job scheduler for Actix.
bash
cargo add actix-jobs
Minimal example. For more information please refer to the Docs.
```rust use actixjobs::{Job, Scheduler, runforever};
struct MyJob; impl Job for MyJob { fn cron(&self) -> &str { "*/2 * * * * * *" // every two seconds }
fn run(&mut self) {
println!("Sending an email to all our clients...");
}
}
async fn main() { let mut scheduler = Scheduler::new(); scheduler.add(Box::new(MyJob));
run_forever(scheduler); // This will start the scheduler in a new thread.
// The rest of your program...
} ```
Information about the cron syntax.
run
This can be archieved via actix_rt::spawn
as shown bellow.
```rust use actixjobs::{Job, Scheduler, runforever};
struct MyJob; impl Job for MyJob { fn cron(&self) -> &str { "*/2 * * * * * *" // every two seconds }
fn run(&mut self) {
actix_rt::spawn(async move {
actix_rt::time::sleep(Duration::from_millis(1000)).await;
println!("Some more async stuff...");
}
}
}
async fn main() { let mut scheduler = Scheduler::new(); scheduler.add(Box::new(MyJob));
run_forever(scheduler); // This will start the scheduler in a new thread.
// The rest of your program...
} ```