cron_job/lib.rs
1//! # cron-job
2//!
3//! The `cron-job` library lets you create cronjobs. This is basically
4//! an implementation of the `cron` library.
5//!
6//! ## Getting started
7//!
8//! ### Running functions
9//!
10//! You can schedule your own functions as jobs.
11//!
12//! ``` Functions
13//! extern crate cron_job;
14//! use cron_job::CronJob;
15//!
16//! fn main() {
17//! // Create CronJob
18//! let mut cron = CronJob::default();
19//! // Add the function
20//! cron.new_job("* * * * * *", run_on_cron);
21//! // Start job
22//! cron.start();
23//! }
24//!
25//! // The function to be executed.
26//! fn run_on_cron() {
27//! println!("Executed function");
28//! }
29//! ```
30//!
31//! Multiple functions with different cron expression can also be added.
32//!
33//! ``` Multiple functions
34//! extern crate cron_job;
35//! use cron_job::CronJob;
36//!
37//! fn main() {
38//! // Create CronJob
39//! let mut cron = CronJob::default();
40//! // Add the function to be run every second
41//! cron.new_job("* * * * * *", run_every_second);
42//! // Add the function to be run every 5 seconds
43//! cron.new_job("*/5 * * * * *", run_every_five_seconds);
44//! // Start jobs
45//! cron.start();
46//! }
47//!
48//! // The function to be executed every second.
49//! fn run_every_second() {
50//! println!("1 second");
51//! }
52//!
53//! // The function to be executed every 5 seconds.
54//! fn run_every_five_seconds() {
55//! println!("5 seconds");
56//! }
57//! ```
58//!
59//! ## Running jobs implementing Job trait
60//!
61//! Since the function used as job cannot have any parameters, the
62//! `Job` trait is available to be implemented to structs. This way
63//! if any parameter needs to be passed to the function, can be
64//! passed as the struct property.
65//!
66//! ``` Job
67//! extern crate cron_job;
68//! use cron_job::CronJob;
69//!
70//! fn main() {
71//! // Create HelloJob
72//! let helloJob = HelloJob{ name: "John" };
73//! // Create CronJob
74//! let mut cron = CronJob::default();
75//! // Say hello every second
76//! cron.new_job("* * * * * *", helloJob);
77//! // Start jobs
78//! cron.start();
79//! }
80//!
81//! // The job to be executed
82//! struct HelloJob {
83//! name: String
84//! }
85//!
86//! impl Job for HelloJob {
87//! fn run(&self) {
88//! println!("Hello, {}!", self.name);
89//! }
90//! }
91//! ```
92//!
93//! Functions and job can be mixed together.
94//!
95//! ``` Function and job
96//! extern crate cron_job;
97//! use cron_job::CronJob;
98//!
99//! fn main() {
100//! // Create HelloJob
101//! let helloJob = HelloJob{ name: "John" };
102//! // Create CronJob
103//! let mut cron = CronJob::default();
104//! // Run function every second
105//! cron.new_job("* * * * * *", run_every_second);
106//! // Say hello every second
107//! cron.new_job("* * * * * *", helloJob);
108//! // Start jobs
109//! cron.start();
110//! }
111//! // The function to be executed every second.
112//! fn run_every_second() {
113//! println!("1 second");
114//! }
115//!
116//! // The job to be executed
117//! struct HelloJob {
118//! name: String
119//! }
120//!
121//! // Very important, implement the Job trait and its functions.
122//! impl Job for HelloJob {
123//! fn run(&self) {
124//! println!("Hello, {}!", self.name);
125//! }
126//! }
127//! ```
128
129extern crate chrono;
130extern crate cron;
131
132mod cronjob;
133mod job;
134
135pub use crate::cronjob::CronJob;
136pub use job::Job;
137
138impl<T: Sync + Send + 'static + FnMut()> Job for T {
139 fn run(&mut self) {
140 self();
141 }
142}