schedule/
agenda.rs

1use std::vec::Vec;
2
3use job::Job;
4
5#[derive(Default)]
6pub struct Agenda<'a> {
7    jobs: Vec<Job<'a>>,
8}
9
10impl<'a> Agenda<'a> {
11    pub fn new() -> Agenda<'a> {
12        Default::default()
13    }
14
15    pub fn add(&mut self, job: Job<'a>) {
16        // Add new job
17        self.jobs.push(job);
18
19        // Re-sort job list
20        self.jobs.sort_by_key(|j| j.next_run_at);
21    }
22
23    pub fn run_pending(&mut self) {
24        for job in &mut self.jobs {
25            if job.is_ready() {
26                job.run();
27            } else {
28                // The jobs array is sorted so the first non-ready job we hit
29                // means we're at the end of what we care
30                break;
31            }
32        }
33
34        // Re-sort job list
35        self.jobs.sort_by_key(|j| j.next_run_at);
36    }
37}