1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
extern crate chrono;

use std::sync::mpsc::{channel, Receiver, Sender};
use std::cmp::Ordering;

use chrono::prelude::*;

mod events;

pub use self::events::{BasicEvent, Eventer};
pub use chrono::Duration;

type InternalTime = DateTime<Utc>;

pub struct Scheduler {
    stuff: Vec<ScheduledEvent>,
    new_items_rx: Receiver<Box<Eventer + Send>>,
    new_items_tx: Sender<Box<Eventer + Send>>,
}

impl Scheduler {
    /// Create a new Scheduler instance
    pub fn new() -> Self {
        let (tx,rx) = channel();
        Scheduler {
            stuff: vec!(),
            new_items_tx: tx,
            new_items_rx: rx,
        }
    }

    /// Obtain a handle to send new tasks to
    pub fn add_handle(&self) -> Sender<Box<Eventer + Send>> {
        self.new_items_tx.clone()
    }

    /// Run the scheduler. Will block forever
    pub fn run(&mut self) {
        loop {
            self.step()
        }
    }

    fn step(&mut self) {
        let time_to_next = self.process_pending();

        match self.new_items_rx.recv_timeout(time_to_next.to_std().unwrap()) {
            Ok(evt) => {
                //println!("PING");
                let mut new_evts: Vec<_> =
                    self.new_items_rx.try_iter().fold(vec![evt], |mut acc, x| {
                        acc.push(x);
                        acc
                    });

                // Immediately run all new tasks
                for evt in new_evts.drain(..) {
                    self.process_single(evt);
                }
            }
            _ => {
                //println!("PONG");
                // Timeout, its probably time to run an event
            }
        }
    }

    fn process_single(&mut self, mut evt: Box<Eventer + Send>) {
        match evt.execute() {
            Some(d) => {
                // reschedule
                self.insert(ScheduledEvent {
                    when_next: Utc::now() + d,
                    what: evt,
                });
            }
            None => {} // Nothing to reschedule
        }
    }

    fn process_pending(&mut self) -> Duration {
        // println!("Processing Pending");
        loop {
            // Is there a pending item?
            if self.stuff.len() == 0 {
                return Duration::hours(24);
            }

            let now = Utc::now();
            let next = self.stuff
                .get(0)
                .unwrap()
                .when_next;

            if next <= now {
                let x = self.stuff.remove(0);
                self.process_single(x.what);
            } else {
                return next.signed_duration_since(now);
            }
        }
    }

    fn insert(&mut self, evt: ScheduledEvent) {
        let idx = match self.stuff.binary_search(&evt) {
            Ok(idx) => idx,
            Err(idx) => idx,
        };

        self.stuff.insert(idx, evt);
    }
}

struct ScheduledEvent {
    when_next: InternalTime,
    what: Box<Eventer + Send>,
}

impl Ord for ScheduledEvent {
    fn cmp(&self, other: &ScheduledEvent) -> Ordering {
        self.when_next.cmp(&other.when_next)
    }
}

impl PartialOrd for ScheduledEvent {
    fn partial_cmp(&self, other: &ScheduledEvent) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl PartialEq for ScheduledEvent {
    fn eq(&self, other: &ScheduledEvent) -> bool {
        self.when_next == other.when_next
    }
}

// This probably shouldn't be a thing
impl Eq for ScheduledEvent {}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn it_works() {

    }
}