A simple trait for async cron jobs in Rust.
cargo add async_job
use async_job::{Job, Runner, Schedule, async_trait};
struct ExampleJob;
#[async_trait]
impl Job for ExampleJob {
fn schedule(&self) -> Option<Schedule> {
Some("1/5 * * * * *".parse().unwrap())
}
// run any async or sync task here with mutation capabilities
async fn handle(&mut self) {
println!("Hello, I am a cron job running at: {}", self.now());
}
}
If you need to use a single threaded env disable the default feature and set the feature rt
,
rt
: Single threaded tokio runtime.rt-multi-thread
: Multi threaded tokio runtime. Enabled by default
Run the example with cargo run --example example