32 lines
827 B
Rust
32 lines
827 B
Rust
//! Based on code from <https://github.com/tuyentv96/rust-crontab/blob/01a266e8e1c7f6ee86b3d1010b8818ffc4db6518/README.md>
|
|
|
|
use chrono::{FixedOffset, Local, TimeZone};
|
|
use cron_tab;
|
|
|
|
fn main() {
|
|
let local_tz =
|
|
Local::from_offset(&FixedOffset::east_opt(7).expect("Failed to init fixed offset"));
|
|
let mut cron = cron_tab::Cron::new(local_tz);
|
|
|
|
let first_job_id = cron.add_fn("* * * * * * *", print_now).unwrap();
|
|
|
|
// start cron in background
|
|
cron.start();
|
|
|
|
cron.add_fn("* * * * * *", move || {
|
|
println!("add_fn {}", Local::now().to_string());
|
|
})
|
|
.unwrap();
|
|
|
|
// remove job_test
|
|
cron.remove(first_job_id);
|
|
|
|
std::thread::sleep(std::time::Duration::from_secs(10));
|
|
|
|
// stop cron
|
|
cron.stop();
|
|
}
|
|
|
|
fn print_now() {
|
|
println!("now: {}", Local::now().to_string());
|
|
}
|