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
//! # CronTab
//!
//! A cron job library for Rust.
//!
//! ## Usage
//!
//! Add cron_tab crate to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! cron_tab = "0.1.0"
//! ```
//!
//! Creating a schedule for a job is done using the `FromStr` impl for the
//! `Schedule` type of the [cron](https://github.com/tuyentv96/cron_tab) library.
//!
//! The scheduling format is as follows:
//!
//! ```text
//! sec   min   hour   day of month   month   day of week   year
//! *     *     *      *              *       *             *
//! ```
//!
//! A simple example:
//!
//! ```rust,ignore
//! extern crate cron_tab;
//!
//! use chrono::{FixedOffset, Local, TimeZone, Utc};
//!
//! fn main() {
//!     let local_tz = Local::from_offset(&FixedOffset::east(7));
//!     let utc_tz = Utc;
//!
//!     let mut cron = cron_tab::Cron::new(utc_tz);
//!
//!     let job_test_id = cron.add_fn("* * * * * * *", test).unwrap();
//!
//!     cron.start();
//!
//!     std::thread::sleep(std::time::Duration::from_secs(2));
//!     let anonymous_job_id = cron
//!         .add_fn("* * * * * *", || {
//!             println!("anonymous fn");
//!         })
//!         .unwrap();
//!
//!     // remove job_test
//!     cron.remove(job_test_id);
//!
//!     loop {
//!         std::thread::sleep(std::time::Duration::from_secs(2));
//!     }
//! }
//!
//! fn test() {
//!     println!("now: {}", Local::now().to_string());
//! }
//!
//! ```

#[macro_use]
extern crate crossbeam_channel;

use chrono::{DateTime, TimeZone, Utc};
use cron;
use std::ops::{DerefMut, Sub};
use std::str::FromStr;
use std::sync::{Arc, Mutex};
use std::thread;
use std::time;
use thiserror;

#[derive(thiserror::Error, Debug)]
pub enum CronError {
    #[error("parse cron expression failed: {0}")]
    ParseError(#[from] cron::error::Error),
    #[error("unknown error")]
    Unknown,
}

#[derive(Clone)] // we implement the Copy trait
/// Entry wrap data about the job
pub struct Entry<Z>
where
    Z: Send + Sync + 'static,
    Z: TimeZone,
{
    pub id: i32,
    pub schedule: cron::Schedule,
    pub next: Option<DateTime<Z>>,
    pub run: Arc<Mutex<dyn FnMut() + Send + Sync + 'static>>,
}

impl<Z> Entry<Z>
where
    Z: Send + Sync + 'static,
    Z: TimeZone,
{
    fn get_next(&self, tz: Z) -> Option<DateTime<Z>> {
        self.schedule.upcoming(tz).next()
    }
}

#[derive(Clone)]
pub struct Cron<Z>
where
    Z: Send + Sync + 'static,
    Z: TimeZone,
    <Z as TimeZone>::Offset: Send,
{
    entries: Arc<Mutex<Vec<Entry<Z>>>>,
    next_id: Arc<Mutex<i32>>,
    running: Arc<Mutex<bool>>,
    tz: Z,
    add_tx: crossbeam_channel::Sender<Entry<Z>>,
    add_rx: crossbeam_channel::Receiver<Entry<Z>>,
    remove_tx: crossbeam_channel::Sender<i32>,
    remove_rx: crossbeam_channel::Receiver<i32>,
    stop_tx: crossbeam_channel::Sender<bool>,
    stop_rx: crossbeam_channel::Receiver<bool>,
}

/// Cron contains and executes the scheduled jobs.
impl<Z> Cron<Z>
where
    Z: Send + Sync + 'static,
    Z: TimeZone,
    <Z as TimeZone>::Offset: Send,
{
    /// Create a new cron.
    ///
    /// ```rust,ignore
    /// let mut cron = cron::Cron::new(Utc);
    /// cron.add_fn("* * * * * *", || {
    /// println!("anonymous fn");
    /// }).unwrap();
    /// cron.start();    
    /// ```
    pub fn new(tz: Z) -> Cron<Z> {
        let (add_tx, add_rx) = crossbeam_channel::unbounded();
        let (remove_tx, remove_rx) = crossbeam_channel::unbounded();
        let (stop_tx, stop_rx) = crossbeam_channel::unbounded();
        Cron {
            entries: Arc::new(Mutex::new(Vec::new())),
            next_id: Arc::new(Mutex::new(0)),
            running: Arc::new(Mutex::new(false)),
            // offset: None,
            tz: tz,
            add_tx,
            add_rx,
            remove_tx,
            remove_rx,
            stop_tx,
            stop_rx,
        }
    }

    /// Add a function to Cron.
    ///
    /// ```rust,ignore
    /// let mut cron = cron::Cron::new(Utc);
    /// cron.add_fn("* * * * * *", || {
    /// println!("anonymous fn");
    /// }).unwrap();
    /// cron.start();    
    /// ```
    pub fn add_fn<T>(&mut self, spec: &str, f: T) -> Result<i32, CronError>
    where
        T: 'static,
        T: FnMut() -> () + Send + Sync,
    {
        let schedule = cron::Schedule::from_str(spec)?;
        self.schedule(schedule, f)
    }

    fn schedule<T>(&mut self, schedule: cron::Schedule, f: T) -> Result<i32, CronError>
    where
        T: Send + Sync + 'static,
        T: FnMut() -> (),
    {
        let mut next_id = self.next_id.lock().unwrap();
        *next_id += 1;

        let entry = Entry {
            id: *next_id,
            schedule,
            next: None,
            run: Arc::new(Mutex::new(f)),
        };

        if *self.running.lock().unwrap() {
            self.add_tx.send(entry).unwrap();
        } else {
            self.entries.lock().unwrap().push(entry);
        }

        Ok(*next_id)
    }

    /// Set timezone offset.
    ///
    /// ```rust,ignore
    /// let mut cron = cron::Cron::new(Utc);
    /// cron.start();    
    /// ```
    pub fn set_timezone(&mut self, tz: Z) {
        self.tz = tz;
    }

    fn get_timezone(&self) -> Z {
        self.tz.clone()
    }

    fn now(&self) -> DateTime<Z> {
        self.get_timezone()
            .from_utc_datetime(&Utc::now().naive_utc())
    }

    fn remove_entry(&self, id: i32) {
        let mut entries = self.entries.lock().unwrap();
        if let Some(index) = entries.iter().position(|e| e.id == id) {
            entries.remove(index);
        }
    }

    /// Remove a job from Cron.
    ///
    /// ```rust,ignore
    /// let mut cron = cron::Cron::new();
    /// let job_id = cron.add_fn("* * * * * *", || {
    /// println!("anonymous fn");
    /// }).unwrap();
    /// cron.start();  
    /// cron.remove(job_id);  
    /// ```
    pub fn remove(&self, id: i32) {
        if *self.running.lock().unwrap() {
            self.remove_tx.send(id).unwrap();
            return;
        }

        self.remove_entry(id);
    }

    /// Stop Cron.
    ///
    /// ```rust,ignore
    /// let mut cron = cron::Cron::new();
    /// let job_id = cron.add_fn("* * * * * *", || {
    /// println!("anonymous fn");
    /// }).unwrap();
    /// cron.start();  
    /// cron.stop();  
    /// ```
    pub fn stop(&self) {
        self.stop_tx.send(true).unwrap();
    }

    /// Start cron.
    /// A thead will be spawn for schedule jobs
    ///
    /// ```rust,ignore
    /// let mut cron = cron::Cron::new(Utc);
    /// let job_id = cron.add_fn("* * * * * *", || {
    /// println!("anonymous fn");
    /// }).unwrap();
    /// cron.start();
    /// ```
    pub fn start(&mut self) {
        let mut cloned_cron = self.clone();
        thread::spawn(move || {
            cloned_cron.run();
        });
    }

    /// Run a loop for schedule jobs
    fn run(&mut self) {
        let mut running = self.running.lock().unwrap();
        *running = true;
        drop(running);

        for entry in self.entries.lock().unwrap().iter_mut() {
            entry.next = entry.get_next(self.get_timezone());
        }

        // default long timer duration
        let mut timer_dur = time::Duration::from_secs(100000000);

        loop {
            self.entries
                .lock()
                .unwrap()
                .sort_by(|b, a| b.next.cmp(&a.next));

            if self.entries.lock().unwrap().len() > 0 {
                // get first entry from sorted entries for timer duration
                let dur = self.entries.lock().unwrap()[0]
                    .next
                    .as_ref()
                    .unwrap()
                    .timestamp_millis()
                    .sub(self.now().timestamp_millis());
                if dur > 0 {
                    if self.entries.lock().unwrap().len() > 0 {
                        timer_dur = time::Duration::from_millis(dur as u64);
                    }
                }
            }

            select! {
                // wait timer expire
                recv(crossbeam_channel::after(timer_dur)) -> _ => {
                    let now = Utc::now();
                    for entry in self.entries.lock().unwrap().iter_mut() {
                        if entry.next.as_ref().unwrap().gt(&now) {
                            break;
                        }

                        (entry.run.lock().unwrap().deref_mut())();
                        entry.next = entry.get_next(self.get_timezone());
                    }
                },
                // wait add new entry
                recv(self.add_rx) -> new_entry => {
                    let mut entry = new_entry.unwrap();
                    entry.next = entry.get_next(self.get_timezone());
                    self.entries.lock().unwrap().push(entry);
                },
                // wait remove entry
                recv(self.remove_rx) -> id => {
                    self.remove_entry(id.unwrap());
                },
                // wait stop cron
                recv(self.stop_rx) -> _ => {
                    return;
                },
            }
        }
    }
}