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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
use crate::error::SchedulerError;
use chrono::prelude::*;
use chrono_tz::Tz;
use std::time::Duration;

pub enum Scheduler {
    /// Set to execute on set time periods
    Cron(cron::Schedule),

    /// Set to execute exactly `duration` away from the previous execution.
    /// If
    Interval { interval_duration: Duration, execute_at_startup: bool },

    /// Multi shceduler: the execution is trigger where at least one of the schedulers in matched
    Multi(Vec<Scheduler>),

    /// Set to execute to never
    Never,
}

impl Scheduler {
    pub fn from(schedule: &[&dyn TryToScheduler]) -> Result<Scheduler, SchedulerError> {
        schedule.to_scheduler()
    }

    // Determine the next time we should execute (from a reference point)
    pub fn next(&mut self, after: &DateTime<Utc>, timezone: Option<Tz>) -> Option<DateTime<Utc>> {
        match *self {
            Scheduler::Cron(ref cs) => {
                if let Some(tz) = timezone {
                    cs.after(&after.with_timezone(&tz)).next().map(|date| date.with_timezone(&Utc))
                } else {
                    cs.after(&after).next()
                }
            }

            Scheduler::Interval { ref interval_duration, ref mut execute_at_startup } => {
                if *execute_at_startup {
                    *execute_at_startup = false;
                    Some(*after)
                } else {
                    let ch_duration = match time::Duration::from_std(*interval_duration) {
                        Ok(value) => value,
                        Err(_) => {
                            return None;
                        }
                    };
                    Some(*after + ch_duration)
                }
            }

            Scheduler::Multi(ref mut schedulers) => {
                let mut result = None;
                for scheduler in schedulers {
                    if let Some(local_next) = scheduler.next(after, timezone) {
                        result = match result {
                            Some(current_next) => {
                                if local_next < current_next {
                                    Some(local_next)
                                } else {
                                    Some(current_next)
                                }
                            }
                            None => Some(local_next),
                        }
                    }
                }
                result
            }

            Scheduler::Never => None,
        }
    }
}

pub trait TryToScheduler {
    fn to_scheduler(&self) -> Result<Scheduler, SchedulerError>;
}

impl TryToScheduler for Vec<String> {
    fn to_scheduler(&self) -> Result<Scheduler, SchedulerError> {
        let refs: Vec<&str> = self.iter().map(|s| s.as_ref()).collect();
        refs.to_scheduler()
    }
}

impl TryToScheduler for Vec<&str> {
    fn to_scheduler(&self) -> Result<Scheduler, SchedulerError> {
        match self.len() {
            0 => Ok(Scheduler::Never),
            1 => self[0].to_scheduler(),
            _ => {
                let mut result = vec![];
                for scheduler in self {
                    result.push(scheduler.to_scheduler()?);
                }
                Ok(Scheduler::Multi(result))
            }
        }
    }
}

impl TryToScheduler for Vec<&dyn TryToScheduler> {
    fn to_scheduler(&self) -> Result<Scheduler, SchedulerError> {
        (&self[..]).to_scheduler()
    }
}

impl TryToScheduler for &[&dyn TryToScheduler] {
    fn to_scheduler(&self) -> Result<Scheduler, SchedulerError> {
        match self.len() {
            0 => Ok(Scheduler::Never),
            1 => self[0].to_scheduler(),
            _ => {
                let mut result = vec![];
                for scheduler in *self {
                    result.push(scheduler.to_scheduler()?);
                }
                Ok(Scheduler::Multi(result))
            }
        }
    }
}

impl<'a> TryToScheduler for &'a str {
    fn to_scheduler(&self) -> Result<Scheduler, SchedulerError> {
        Ok(Scheduler::Cron(self.parse().map_err(|err| SchedulerError::ScheduleDefinitionError {
            message: format!("Cannot create schedule for [{}]. Err: {:?}", self, err),
        })?))
    }
}

impl TryToScheduler for String {
    fn to_scheduler(&self) -> Result<Scheduler, SchedulerError> {
        self.as_str().to_scheduler()
    }
}

impl TryToScheduler for Duration {
    fn to_scheduler(&self) -> Result<Scheduler, SchedulerError> {
        Ok(Scheduler::Interval { interval_duration: *self, execute_at_startup: false })
    }
}

impl TryToScheduler for (Duration, bool) {
    fn to_scheduler(&self) -> Result<Scheduler, SchedulerError> {
        Ok(Scheduler::Interval { interval_duration: self.0, execute_at_startup: self.1 })
    }
}

impl Into<Scheduler> for Vec<Scheduler> {
    fn into(self) -> Scheduler {
        Scheduler::Multi(self)
    }
}

#[cfg(test)]
pub mod test {

    use super::*;
    use chrono_tz::UTC;

    #[test]
    fn never_should_not_schedule() {
        let mut schedule = Scheduler::Never;
        assert_eq!(None, schedule.next(&Utc::now(), Some(UTC)))
    }

    #[test]
    fn interval_should_schedule_plus_duration() {
        let now = Utc::now();
        let secs = 10;
        let mut schedule = Duration::new(secs, 0).to_scheduler().unwrap();

        let next = schedule.next(&now, None).unwrap();

        assert!(next.timestamp() >= now.timestamp() + (secs as i64));
    }

    #[test]
    fn interval_should_schedule_at_startup() {
        let now = Utc::now();
        let secs = 10;
        let mut schedule = (Duration::new(secs, 0), true).to_scheduler().unwrap();

        let first = schedule.next(&now, None).unwrap();
        assert_eq!(now.timestamp(), first.timestamp());

        let next = schedule.next(&now, None).unwrap();
        assert!(next.timestamp() >= now.timestamp() + (secs as i64));
    }

    #[test]
    fn should_build_an_interval_schedule_from_duration() {
        let schedule = Duration::new(1, 1).to_scheduler().unwrap();
        match schedule {
            Scheduler::Interval { .. } => assert!(true),
            _ => assert!(false),
        }
    }

    #[test]
    fn should_build_a_periodic_schedule_from_str() {
        let schedule = "* * * * * *".to_scheduler().unwrap();
        match schedule {
            Scheduler::Cron(_) => assert!(true),
            _ => assert!(false),
        }
    }

    #[test]
    fn should_build_a_multi_scheduler_from_empty_array() {
        let schedule = Scheduler::from(&vec![]).unwrap();
        match schedule {
            Scheduler::Never => assert!(true),
            _ => assert!(false),
        }
    }

    #[test]
    fn should_build_a_multi_scheduler_from_single_entry_array() {
        let schedule = Scheduler::from(&[&vec!["* * * * * *"]]).unwrap();
        match schedule {
            Scheduler::Cron(_) => assert!(true),
            _ => assert!(false),
        }
    }

    #[test]
    fn should_build_a_multi_scheduler_from_array() {
        let schedule = Scheduler::from(&[&"* * * * * *", &Duration::from_secs(9)]).unwrap();
        match schedule {
            Scheduler::Multi(inner) => {
                match inner[0] {
                    Scheduler::Cron(_) => assert!(true),
                    _ => assert!(false),
                };
                match inner[1] {
                    Scheduler::Interval { .. } => assert!(true),
                    _ => assert!(false),
                };
            }
            _ => assert!(false),
        }
    }

    #[test]
    fn cron_should_be_time_zone_aware_with_utc() {
        let mut schedule = "* 11 10 * * *".to_scheduler().unwrap();
        let date = Utc.ymd(2010, 1, 1).and_hms(10, 10, 0);

        let expected_utc = Utc.ymd(2010, 1, 1).and_hms(10, 11, 0);

        let next = schedule.next(&date, Some(UTC)).unwrap();

        assert_eq!(next, expected_utc);
    }

    #[test]
    fn cron_should_be_time_zone_aware_with_custom_time_zone() {
        let mut schedule = "* 11 10 * * *".to_scheduler().unwrap();

        let date = Utc.ymd(2010, 1, 1).and_hms(10, 10, 0);
        let expected_utc = Utc.ymd(2010, 1, 2).and_hms(09, 11, 0);

        let tz = chrono_tz::Europe::Rome;

        let next = schedule.next(&date, Some(tz)).unwrap();

        assert_eq!(next.with_timezone(&Utc), expected_utc);
    }

    #[test]
    fn multi_should_return_first_possible_next_execution() {
        let mut schedule = Scheduler::from(&[&"* 10 10 * * *", &"* 20 20 * * *"]).unwrap();

        {
            let date = Utc.ymd(2010, 1, 1).and_hms(10, 8, 0);
            let expected = Utc.ymd(2010, 1, 1).and_hms(10, 10, 0);

            let next = schedule.next(&date, Some(UTC)).unwrap();
            assert_eq!(next.with_timezone(&Utc), expected);
        }

        {
            let date = Utc.ymd(2010, 1, 1).and_hms(11, 8, 0);
            let expected = Utc.ymd(2010, 1, 1).and_hms(20, 20, 0);

            let next = schedule.next(&date, Some(UTC)).unwrap();
            assert_eq!(next.with_timezone(&Utc), expected);
        }

        {
            let date = Utc.ymd(2010, 1, 1).and_hms(22, 8, 0);
            let expected = Utc.ymd(2010, 1, 2).and_hms(10, 10, 0);

            let next = schedule.next(&date, Some(UTC)).unwrap();
            assert_eq!(next.with_timezone(&Utc), expected);
        }
    }
}