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
//! # cronjob
//!
//! The `cronjob` library lets you create cronjobs for your methods.
//!
//! ## Getting started
//!
//! ``` no_run
//! extern crate cronjob;
//! use cronjob::{CronJob, get_timezone_offset};
//!
//! fn main() {
//!     // Create offset for your required timezone.
//!     let offset = get_timezone_offset(2);
//!     // Create the `CronJob` object.
//!     let mut cron = CronJob::new("Test Cron", "* * * * * * *", offset, on_cron);
//!     // Start the cronjob
//!     cron.start_job();
//! }
//!
//! // Our cronjob handler
//! fn on_cron(name: &str) {
//!     println!("{}: It's time!", name);
//! }
//! ```
//!
//! ## Threaded
//! ``` no_run
//! extern crate cronjob;
//! use cronjob::{CronJob, get_timezone_offset};
//!
//! fn main() {
//!     // Create offset for your required timezone.
//!     let offset = get_timezone_offset(2);
//!     // Create the `CronJob` object
//!     let mut cron = CronJob::new("Test Cron Threaded", "* * * * * * *", offset, on_cron);
//!     // start the cronjob
//!     CronJob::start_job_threaded(cron);
//! }
//!
//! // Our cronjob handler
//! fn on_cron(name: &str) {
//!     println!("{}: It's time!", name);
//! }



extern crate cron;
extern crate chrono;

pub use cronjob::CronJob;

use command::Command;
use chrono::FixedOffset;

mod cronjob;
mod command;

const HOUR: i32 = 3600;

/// Use this to automaticly get an `FixedOffset` for the `CronJob` object
pub fn get_timezone_offset(hours: i32) -> FixedOffset {
    if hours > 0 {
        FixedOffset::east(HOUR * hours)
    } else {
        FixedOffset::west(HOUR * (hours * -1))
    }
}

/// Implementation of `Command` for your methods to be used by the `CronJob` object
impl<T: Sync + Send + 'static + FnMut(&str)> Command for T {
    fn execute(&mut self, name: &str) {
        self(name);
    }
}