use chrono::{DateTime, UTC};
use schedule::Schedule;
pub struct Job<'a> {
schedule: Box<Schedule + 'a>,
last_run_at: Option<DateTime<UTC>>,
pub next_run_at: Option<DateTime<UTC>>,
function: Box<(FnMut() -> ()) + 'a>,
}
impl<'a> Job<'a> {
pub fn new<T>(function: T, schedule: Box<Schedule>) -> Job<'a>
where T: 'a,
T: FnMut() -> ()
{
Job {
next_run_at: schedule.next(None),
last_run_at: None,
schedule: schedule,
function: Box::new(function),
}
}
pub fn is_ready(&self) -> bool {
if let Some(next_run_at) = self.next_run_at {
UTC::now() >= next_run_at
} else {
false
}
}
pub fn run(&mut self) {
self.last_run_at = Some(UTC::now());
(self.function)();
self.next_run_at = self.schedule.next(self.last_run_at);
}
}