use async_trait::async_trait;
use chrono::Utc;
use chrono_tz::Tz;
use foxtive_cron::contracts::{JobContract, Schedule, ValidatedSchedule};
use foxtive_cron::{Cron, CronResult};
use std::borrow::Cow;
struct LocalizedJob {
id: String,
schedule: ValidatedSchedule,
timezone: Tz,
}
#[async_trait]
impl JobContract for LocalizedJob {
async fn run(&self) -> CronResult<()> {
let now_utc = Utc::now();
let now_local = now_utc.with_timezone(&self.timezone);
println!(
"[{}] Running task. UTC: {}, Local ({}): {}",
self.id, now_utc, self.timezone, now_local
);
Ok(())
}
fn id(&self) -> Cow<'_, str> {
Cow::Borrowed(&self.id)
}
fn name(&self) -> Cow<'_, str> {
Cow::Borrowed("Localized Job")
}
fn schedule(&self) -> &dyn Schedule {
&self.schedule
}
fn timezone(&self) -> Tz {
self.timezone
}
}
#[tokio::main]
async fn main() {
let mut cron = Cron::new();
let ny_tz: Tz = "America/New_York".parse().unwrap();
let job_ny = LocalizedJob {
id: "ny-task".to_string(),
schedule: ValidatedSchedule::parse("*/10 * * * * * *").unwrap(), timezone: ny_tz,
};
cron.add_job(job_ny).unwrap();
let tokyo_tz: Tz = "Asia/Tokyo".parse().unwrap();
let job_tokyo = LocalizedJob {
id: "tokyo-task".to_string(),
schedule: ValidatedSchedule::parse("*/10 * * * * * *").unwrap(), timezone: tokyo_tz,
};
cron.add_job(job_tokyo).unwrap();
println!("Scheduler starting with multiple time zones...");
cron.run().await;
}