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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//! [![ci-badge][]][ci] [![docs-badge][]][docs] [![crate-version]][crate-link]
//!
//! # schedule-rs
//!
//! A simple scheduling library inspired by Python's `schedule`.
//!
//! ## Sample usage
//! ```rust,no_run
//! use schedule_rs::prelude::*;
//!
//! let function = || {
//!     println!("Hello World!");
//! };
//! let task = Schedule::every().minute().run(function);
//! TaskRunner::one(task).run();
//!
//! std::thread::park();
//! ```
//!
//! [ci]: https://github.com/Elinvynia/schedule-rs/actions?query=workflow%3ARust
//! [ci-badge]: https://img.shields.io/github/workflow/status/Elinvynia/schedule-rs/Rust/master?style=flat-square
//! [docs]: https://docs.rs/schedule-rs
//! [docs-badge]: https://img.shields.io/badge/docs-online-5023dd.svg?style=flat-square
//! [crate-link]: https://crates.io/crates/schedule-rs
//! [crate-version]: https://img.shields.io/crates/v/schedule-rs.svg?style=flat-square

#![forbid(unsafe_code)]
#![warn(missing_debug_implementations)]
#![warn(missing_docs)]

use chrono::prelude::*;
use chrono::Duration;
use std::thread;

///! Basic re-exports.
pub mod prelude {
    pub use crate::Schedule;
    pub use crate::TaskRunner;
}

/// Base type for creating tasks.
#[derive(Debug, Copy, Clone)]
pub struct Schedule;

impl Schedule {
    /// Run a task every single interval of time.
    pub fn every() -> ScheduleBuilderOne {
        ScheduleBuilderOne
    }

    /// Run a task every X intervals of time.
    pub fn every_num(amount: u32) -> ScheduleBuilderOnes {
        ScheduleBuilderOnes { amount }
    }
}

/// Builder for time values of 1 (minute, hour, etc).
#[derive(Debug)]
pub struct ScheduleBuilderOne;

impl ScheduleBuilderOne {
    /// Run a task every minute.
    pub fn minute(self) -> ScheduleBuilderTwo {
        ScheduleBuilderTwo {
            interval: Interval::Minute(1),
            limit: None,
        }
    }

    /// Run a task every hour.
    pub fn hour(self) -> ScheduleBuilderTwo {
        ScheduleBuilderTwo {
            interval: Interval::Hour(1),
            limit: None,
        }
    }

    /// Run a task every day.
    pub fn day(self) -> ScheduleBuilderTwo {
        ScheduleBuilderTwo {
            interval: Interval::Day(1),
            limit: None,
        }
    }

    /// Run a task every week.
    pub fn week(self) -> ScheduleBuilderTwo {
        ScheduleBuilderTwo {
            interval: Interval::Week(1),
            limit: None,
        }
    }

    /// Run a task every month.
    pub fn month(self) -> ScheduleBuilderTwo {
        ScheduleBuilderTwo {
            interval: Interval::Month(1),
            limit: None,
        }
    }

    /// Run a task every year.
    pub fn year(self) -> ScheduleBuilderTwo {
        ScheduleBuilderTwo {
            interval: Interval::Year(1),
            limit: None,
        }
    }

    /// Run a task on every Monday.
    pub fn monday(self) -> ScheduleBuilderTwoDates {
        ScheduleBuilderTwoDates {
            interval: Interval::Week(1),
            limit: None,
            day: Weekday::Mon,
            time: None,
        }
    }

    /// Run a task on every Tuesday.
    pub fn tuesday(self) -> ScheduleBuilderTwoDates {
        ScheduleBuilderTwoDates {
            interval: Interval::Week(1),
            limit: None,
            day: Weekday::Tue,
            time: None,
        }
    }

    /// Run a task on every Wednesday.
    pub fn wednesday(self) -> ScheduleBuilderTwoDates {
        ScheduleBuilderTwoDates {
            interval: Interval::Week(1),
            limit: None,
            day: Weekday::Wed,
            time: None,
        }
    }

    /// Run a task on every Thursday.
    pub fn thursday(self) -> ScheduleBuilderTwoDates {
        ScheduleBuilderTwoDates {
            interval: Interval::Week(1),
            limit: None,
            day: Weekday::Thu,
            time: None,
        }
    }

    /// Run a task on every Friday.
    pub fn friday(self) -> ScheduleBuilderTwoDates {
        ScheduleBuilderTwoDates {
            interval: Interval::Week(1),
            limit: None,
            day: Weekday::Fri,
            time: None,
        }
    }

    /// Run a task on every Saturday.
    pub fn saturday(self) -> ScheduleBuilderTwoDates {
        ScheduleBuilderTwoDates {
            interval: Interval::Week(1),
            limit: None,
            day: Weekday::Sat,
            time: None,
        }
    }

    /// Run a task on every Sunday.
    pub fn sunday(self) -> ScheduleBuilderTwoDates {
        ScheduleBuilderTwoDates {
            interval: Interval::Week(1),
            limit: None,
            day: Weekday::Sun,
            time: None,
        }
    }
}

/// Builder for time values of more than one (minutes, hours, etc).
#[derive(Debug, Copy, Clone)]
pub struct ScheduleBuilderOnes {
    amount: u32,
}

impl ScheduleBuilderOnes {
    /// Run a task every X minutes.
    pub fn minutes(self) -> ScheduleBuilderTwo {
        ScheduleBuilderTwo {
            interval: Interval::Minute(self.amount),
            limit: None,
        }
    }

    /// Run a task every X hours.
    pub fn hours(self) -> ScheduleBuilderTwo {
        ScheduleBuilderTwo {
            interval: Interval::Hour(self.amount),
            limit: None,
        }
    }

    /// Run a task every X days.
    pub fn days(self) -> ScheduleBuilderTwo {
        ScheduleBuilderTwo {
            interval: Interval::Day(self.amount),
            limit: None,
        }
    }

    /// Run a task every X weeks.
    pub fn weeks(self) -> ScheduleBuilderTwo {
        ScheduleBuilderTwo {
            interval: Interval::Week(self.amount),
            limit: None,
        }
    }

    /// Run a task every X months.
    pub fn months(self) -> ScheduleBuilderTwo {
        ScheduleBuilderTwo {
            interval: Interval::Month(self.amount),
            limit: None,
        }
    }

    /// Run a task every X years.
    pub fn years(self) -> ScheduleBuilderTwo {
        ScheduleBuilderTwo {
            interval: Interval::Year(self.amount),
            limit: None,
        }
    }

    /// Run a task every X Mondays.
    pub fn monday(self) -> ScheduleBuilderTwoDates {
        ScheduleBuilderTwoDates {
            interval: Interval::Week(self.amount),
            limit: None,
            day: Weekday::Mon,
            time: None,
        }
    }

    /// Run a task every X Tuesdays.
    pub fn tuesday(self) -> ScheduleBuilderTwoDates {
        ScheduleBuilderTwoDates {
            interval: Interval::Week(self.amount),
            limit: None,
            day: Weekday::Tue,
            time: None,
        }
    }

    /// Run a task every X Wednesdays.
    pub fn wednesday(self) -> ScheduleBuilderTwoDates {
        ScheduleBuilderTwoDates {
            interval: Interval::Week(self.amount),
            limit: None,
            day: Weekday::Wed,
            time: None,
        }
    }

    /// Run a task every X Thursdays.
    pub fn thursday(self) -> ScheduleBuilderTwoDates {
        ScheduleBuilderTwoDates {
            interval: Interval::Week(1),
            limit: None,
            day: Weekday::Thu,
            time: None,
        }
    }

    /// Run a task every X Fridays.
    pub fn friday(self) -> ScheduleBuilderTwoDates {
        ScheduleBuilderTwoDates {
            interval: Interval::Week(1),
            limit: None,
            day: Weekday::Fri,
            time: None,
        }
    }

    /// Run a task every X Saturdays.
    pub fn saturday(self) -> ScheduleBuilderTwoDates {
        ScheduleBuilderTwoDates {
            interval: Interval::Week(1),
            limit: None,
            day: Weekday::Sat,
            time: None,
        }
    }

    /// Run a task every X Sundays.
    pub fn sunday(self) -> ScheduleBuilderTwoDates {
        ScheduleBuilderTwoDates {
            interval: Interval::Week(1),
            limit: None,
            day: Weekday::Sun,
            time: None,
        }
    }
}

#[derive(Debug, Copy, Clone)]
enum Interval {
    Minute(u32),
    Hour(u32),
    Day(u32),
    Week(u32),
    Month(u32),
    Year(u32),
}

/// Builder step that allows you to set a limit and provide the function.
#[derive(Debug)]
pub struct ScheduleBuilderTwo {
    interval: Interval,
    limit: Option<u64>,
}

impl ScheduleBuilderTwo {
    /// Limits the amount of times the function will run, optional.
    pub fn limit(mut self, amount: u64) {
        self.limit = Some(amount)
    }

    /// Finishes building the task with a provided function.
    pub fn run<F: Fn() + Send + 'static>(self, fun: F) -> Scheduled<F> {
        Scheduled {
            execution_time: next_interval(self.interval),
            execution_interval: self.interval,
            execution_limit: self.limit,
            function: fun,
        }
    }
}

/// Builder step that allows you to set a limit and provide the function.
/// Also allows to set the time of day.
#[derive(Debug)]
pub struct ScheduleBuilderTwoDates {
    interval: Interval,
    limit: Option<u64>,
    day: Weekday,
    time: Option<NaiveTime>,
}

impl ScheduleBuilderTwoDates {
    /// Limits the amount of times the function will run, optional.
    pub fn limit(mut self, amount: u64) {
        self.limit = Some(amount)
    }

    /// Sets the time of day when the function will run, optional.
    pub fn at(mut self, time: &str) {
        let naive_time = NaiveTime::parse_from_str(time, "%H:%M").unwrap();
        self.time = Some(naive_time)
    }

    /// Finishes building the task with a provided function.
    pub fn run<F: Fn() + Send + 'static>(self, fun: F) -> Scheduled<F> {
        Scheduled {
            execution_time: first_day_exec(self.day, self.time),
            execution_interval: self.interval,
            execution_limit: self.limit,
            function: fun,
        }
    }
}

fn first_day_exec(day: Weekday, time: Option<NaiveTime>) -> DateTime<Utc> {
    let now = Utc::now();
    let current_day = now.day();
    let current_weekday = now.weekday().number_from_monday();
    let to_add = (current_weekday + day.number_from_monday()) % 7;
    let with_day = Utc::now().with_day(current_day + to_add).unwrap();
    match time {
        Some(t) => with_day
            .with_hour(t.hour())
            .unwrap()
            .with_minute(t.minute())
            .unwrap()
            .with_second(t.second())
            .unwrap(),
        None => with_day,
    }
}

fn next_interval(i: Interval) -> DateTime<Utc> {
    match i {
        Interval::Minute(a) => Utc::now() + Duration::minutes(a as i64),
        Interval::Hour(a) => Utc::now() + Duration::hours(a as i64),
        Interval::Day(a) => Utc::now() + Duration::days(a as i64),
        Interval::Week(a) => Utc::now() + Duration::weeks(a as i64),
        Interval::Month(a) => {
            let time = Utc::now();
            let month = time.month();
            time.with_month(month + a).unwrap()
        }
        Interval::Year(a) => {
            let time = Utc::now();
            let year = time.year();
            time.with_year(year + a as i32).unwrap()
        }
    }
}

/// A built task ready to be run by a runner.
#[derive(Debug)]
pub struct Scheduled<F: Fn() + Send + 'static> {
    execution_time: DateTime<Utc>,
    execution_interval: Interval,
    execution_limit: Option<u64>,
    function: F,
}

/// Runner that runs built tasks.
#[derive(Debug)]
pub struct TaskRunner<F: Fn() + Send + 'static> {
    tasks: Vec<Scheduled<F>>,
}

impl<F: Fn() + Send + 'static> TaskRunner<F> {
    /// Create a new runner for running multiple tasks.
    pub fn new(tasks: Vec<Scheduled<F>>) -> TaskRunner<F> {
        TaskRunner { tasks }
    }

    /// Create a new runner for running a single task.
    pub fn one(task: Scheduled<F>) -> TaskRunner<F> {
        TaskRunner { tasks: vec![task] }
    }

    /// Start the runner.
    pub fn run(mut self) {
        thread::spawn(move || loop {
            let current_time = Utc::now();
            for task in self.tasks.iter_mut() {
                if current_time < task.execution_time {
                    continue;
                };
                (task.function)();
                if let Some(x) = task.execution_limit {
                    task.execution_limit = Some(x - 1);
                };
                task.execution_time = next_interval(task.execution_interval);
            }
            self.tasks = self
                .tasks
                .into_iter()
                .filter(|x| match x.execution_limit {
                    Some(x) => x < 1,
                    None => false,
                })
                .collect();
            thread::sleep(Duration::seconds(5).to_std().unwrap())
        });
    }
}