example/
example.rs

1//! `cargo run --example example`
2extern crate async_job;
3
4use async_job::{async_trait, Job, Runner, Schedule};
5use tokio;
6use tokio::time::Duration;
7
8struct ExampleJob;
9
10#[async_trait]
11impl Job for ExampleJob {
12    fn schedule(&self) -> Option<Schedule> {
13        Some("1/5 * * * * *".parse().unwrap())
14    }
15    async fn handle(&mut self) {
16        println!("Hello, I am a cron job running at: {}", self.now());
17    }
18}
19
20async fn run() {
21    let mut runner = Runner::new();
22    println!("Adding ExampleJob to the Runner");
23    runner = runner.add(Box::new(ExampleJob));
24    println!("Starting the Runner for 20 seconds");
25    runner = runner.run().await;
26    tokio::time::sleep(Duration::from_millis(20 * 1000)).await;
27    println!("Stopping the Runner");
28    runner.stop().await;
29}
30
31#[tokio::main]
32async fn main() {
33    run().await;
34}