async_simple/
async_simple.rs1use std::sync::Arc;
2
3use chrono::{FixedOffset, Local, TimeZone};
4use cron_tab::AsyncCron;
5use tokio::sync::Mutex;
6
7#[tokio::main]
8async fn main() {
9 let local_tz = Local::from_offset(&FixedOffset::east_opt(7).unwrap());
10 let mut cron = AsyncCron::new(local_tz);
11
12 cron.start().await;
13
14 let counter = Arc::new(Mutex::new(1));
15 cron.add_fn("* * * * * *", move || {
16 let counter = counter.clone();
17 async move {
18 let mut counter = counter.lock().await;
19 *counter += 1;
20 let now = Local::now().to_string();
21 println!("{} counter value: {}", now, counter);
22 }
23 })
24 .await
25 .unwrap();
26
27 std::thread::sleep(std::time::Duration::from_secs(10));
28
29 cron.stop().await;
31}