jobs/src/bin/using-crate-cron-job.rs

34 lines
866 B
Rust

//! Based on code from <https://github.com/nambrosini/cron-job/blob/e51067cb2395994cb8643204152a1e7dfc161aa5/README.md>
use cron_job::{CronJob, Job};
fn main() {
// Create HelloJob
let hello_job = HelloJob {
name: "John".into(),
};
// Create CronJob
let mut cron = CronJob::default();
// Run function every second
cron.new_job("* * * * * *", run_every_second);
// Say hello every second
cron.new_job("* * * * * *", hello_job);
// Start jobs
cron.start().expect("Failed to start jobs");
}
// The function to be executed every second.
fn run_every_second() {
println!("1 second");
}
// The job to be executed
struct HelloJob {
name: String,
}
// Very important, implement the Job trait and its functions.
impl Job for HelloJob {
fn run(&mut self) {
println!("Hello, {}!", self.name);
}
}