use std::sync::Arc; use chrono::{FixedOffset, Local, TimeZone}; use cron_tab::AsyncCron; use tokio::sync::Mutex; #[tokio::main] async fn main() { let local_tz = Local::from_offset(&FixedOffset::east_opt(7).expect("Failed to init fixed offset")); let mut cron = AsyncCron::new(local_tz); cron.add_fn("* * * * * *", print_now) .await .expect("Failed to add fn 'print_now'."); cron.start().await; let counter = Arc::new(Mutex::new(1)); cron.add_fn("* * * * * *", move || { let counter = counter.clone(); async move { let mut counter = counter.lock().await; *counter += 1; let now = Local::now().to_string(); println!("{} counter value: {}", now, counter); } }) .await .expect("Failed to add using closure"); std::thread::sleep(std::time::Duration::from_secs(10)); // stop cron cron.stop().await; } async fn print_now() { println!("now: {}", Local::now().to_string()); }