async_job 0.1.4

Simple async cron job crate for Rust
Documentation
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
//! # async_job: a simple async cron runner
//!
//! Use the `Job` trait to create your cron job struct, pass it to the `Runner` and then start it via `run()` method.
//! Runner will spawn new async task where it will start looping through the jobs and will run their handle
//! method once the scheduled time is reached.
//!
//! If your OS has enough threads to spare each job will get its own thread to execute, if not it will be
//! executed in the same thread as the loop but will hold the loop until the job is finished.
//!
//! Please look at the [**`Job trait`**](./trait.Job.html) documentation for more information.
//!
//! ## Example
//! ```
//! use async_job::{Job, Runner, Schedule, async_trait};
//! use tokio::time::Duration;
//! use tokio;
//!
//! struct ExampleJob;
//!
//! #[async_trait]
//! impl Job for ExampleJob {
//!     fn schedule(&self) -> Option<Schedule> {
//!         Some("1/5 * * * * *".parse().unwrap())
//!     }
//!     async fn handle(&mut self) {
//!         println!("Hello, I am a cron job running at: {}", self.now());
//!     }
//! }
//!
//! async fn run() {
//!     let mut runner = Runner::new();
//!
//!     println!("Adding ExampleJob to the Runner");
//!     runner = runner.add(Box::new(ExampleJob));
//!
//!     println!("Starting the Runner for 20 seconds");
//!     runner = runner.run().await;
//!     tokio::time::sleep(Duration::from_millis(20 * 1000)).await;
//!
//!     println!("Stopping the Runner");
//!     runner.stop().await;
//! }
//!
//! #[tokio::main]
//! async fn main() {
//!     run().await;
//! }
//! ```
//!
//! Output:
//! ```shell
//! Adding ExampleJob to the Runner
//! Starting the Runner for 20 seconds
//! Hello, I am a cron job running at: 2021-01-31 03:06:25.908475 UTC
//! Hello, I am a cron job running at: 2021-01-31 03:06:30.912637 UTC
//! Hello, I am a cron job running at: 2021-01-31 03:06:35.926938 UTC
//! Hello, I am a cron job running at: 2021-01-31 03:06:40.962138 UTC
//! Stopping the Runner
//! ```
extern crate chrono;
extern crate cron;

pub use async_trait::async_trait;
use chrono::{DateTime, Duration, Utc};
pub use cron::Schedule;
use lazy_static::lazy_static;
use log::{debug, error, info};
use std::sync::{
    atomic::{AtomicBool, Ordering},
    Arc, RwLock,
};
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender};
use tokio::task::JoinHandle;

lazy_static! {
    /// Singleton instance of a tracker that won't allow
    /// same job to run again while its already running
    /// unless you specificly allow the job to run in
    /// parallel with itself
    pub static ref TRACKER: RwLock<Tracker> = RwLock::new(Tracker::new());
}

#[async_trait]
/// A cron job that runs for a website.
pub trait Job: Send + Sync {
    /// Default implementation of is_active method will
    /// make this job always active
    fn is_active(&self) -> bool {
        true
    }

    /// In case your job takes longer to finish and it's scheduled
    /// to start again (while its still running), default behaviour
    /// will skip the next run while one instance is already running.
    /// (if your OS has enough threads, and is spawning a thread for next job)
    ///
    /// To override this behaviour and enable it to run in parallel
    /// with other instances of self, return `true` on this instance.
    fn allow_parallel_runs(&self) -> bool {
        false
    }

    /// Define the run schedule for your job
    fn schedule(&self) -> Option<Schedule>;

    /// This is where your jobs magic happens, define the action that
    /// will happen once the cron start running your job
    ///
    /// If this method panics, your entire job will panic and that may
    /// or may not make the whole runner panic. Handle your errors
    /// properly and don't let it panic.
    async fn handle(&mut self);

    /// Decide wheather or not to start running your job
    fn should_run(&self) -> bool {
        if self.is_active() {
            match self.schedule() {
                Some(schedule) => {
                    for item in schedule.upcoming(Utc).take(1) {
                        let difference = item - Utc::now();
                        if difference <= Duration::milliseconds(100) {
                            return true;
                        }
                    }
                }
                _ => (),
            }
        }

        false
    }

    /// Simple output that will return current time so you don't have to do so
    /// in your job if you wish to display the time of the run.
    fn now(&self) -> DateTime<Utc> {
        Utc::now()
    }
}

/// Struct for marking jobs running
pub struct Tracker(Vec<usize>);

impl Default for Tracker {
    fn default() -> Self {
        Self::new()
    }
}

impl Tracker {
    /// Return new instance of running
    pub fn new() -> Self {
        Tracker(vec![])
    }

    /// Check if id of the job is marked as running
    pub fn running(&self, id: &usize) -> bool {
        self.0.contains(id)
    }

    /// Set job id as running
    pub fn start(&mut self, id: &usize) -> usize {
        if !self.running(id) {
            self.0.push(*id);
        }
        self.0.len()
    }

    /// Unmark the job from running
    pub fn stop(&mut self, id: &usize) -> usize {
        if self.running(id) {
            match self.0.iter().position(|&r| r == *id) {
                Some(i) => self.0.remove(i),
                None => 0,
            };
        }
        self.0.len()
    }
}

/// Runner that will hold all the jobs and will start up the execution
/// and eventually will stop it.
pub struct Runner {
    /// the current jobs
    pub jobs: Vec<Box<dyn Job>>,
    /// the task that is running the handle
    pub thread: Option<JoinHandle<()>>,
    /// is the task running or not
    pub running: bool,
    /// channel sending message
    pub tx: Option<UnboundedSender<Result<(), ()>>>,
    /// tracker to determine crons working
    pub working: Arc<AtomicBool>,
}

impl Default for Runner {
    fn default() -> Self {
        Self::new()
    }
}

impl Runner {
    /// Create new runner
    pub fn new() -> Self {
        Runner {
            jobs: vec![],
            thread: None,
            running: false,
            tx: None,
            working: Arc::new(AtomicBool::new(false)),
        }
    }

    /// Add jobs into the runner
    ///
    /// Does nothing if already running.
    #[allow(clippy::should_implement_trait)]
    pub fn add(mut self, job: Box<dyn Job>) -> Self {
        if !self.running {
            self.jobs.push(job);
        }
        self
    }

    /// Number of jobs ready to start running
    pub fn jobs_to_run(&self) -> usize {
        self.jobs.len()
    }

    /// Start the loop and job execution
    pub async fn run(self) -> Self {
        if self.jobs.is_empty() {
            return self;
        }

        let working = Arc::new(AtomicBool::new(false));
        let (thread, tx) = spawn(self, working.clone()).await;

        Self {
            thread,
            jobs: vec![],
            running: true,
            tx,
            working,
        }
    }

    /// Stop the spawned runner
    pub async fn stop(&mut self) {
        if !self.running {
            return;
        }
        if let Some(thread) = self.thread.take() {
            if let Some(tx) = &self.tx {
                match tx.send(Ok(())) {
                    Ok(_) => (),
                    Err(e) => error!("Could not send stop signal to cron runner thread: {}", e),
                };
            }
            thread.abort()
        }
    }

    /// Lets us know if the cron worker is running
    pub fn is_running(&self) -> bool {
        self.running
    }

    /// Lets us know if the worker is in the process of executing a job currently
    pub fn is_working(&self) -> bool {
        self.working.load(Ordering::Relaxed)
    }
}

/// Spawn the thread for the runner and return its sender to stop it
async fn spawn(
    runner: Runner,
    working: Arc<AtomicBool>,
) -> (
    Option<JoinHandle<()>>,
    Option<UnboundedSender<Result<(), ()>>>,
) {
    let (tx, mut rx): (
        UnboundedSender<Result<(), ()>>,
        UnboundedReceiver<Result<(), ()>>,
    ) = unbounded_channel();

    let handler = tokio::spawn(async move {
        let mut jobs = runner.jobs;

        loop {
            if rx.try_recv().is_ok() {
                info!("Stopping the cron runner thread");
                break;
            }

            for (id, job) in jobs.iter_mut().enumerate() {
                let no: String = (id + 1).to_string();

                if job.should_run()
                    && (job.allow_parallel_runs()
                        || match TRACKER.read() {
                            Ok(s) => !s.running(&id),
                            _ => false,
                        })
                {
                    match TRACKER.write() {
                        Ok(mut s) => {
                            s.start(&id);
                        }
                        _ => (),
                    }

                    let now = Utc::now();
                    debug!(
                        "START: {} --- {}",
                        format!("cron-job-thread-{}", no),
                        now.format("%H:%M:%S%.f")
                    );

                    working.store(true, Ordering::Relaxed);

                    job.handle().await;

                    working.store(
                        match TRACKER.write() {
                            Ok(mut s) => s.stop(&id) != 0,
                            _ => false,
                        },
                        Ordering::Relaxed,
                    );

                    debug!(
                        "FINISH: {} --- {}",
                        format!("cron-job-thread-{}", no),
                        now.format("%H:%M:%S%.f")
                    );
                }
            }
            tokio::time::sleep(std::time::Duration::from_millis(100)).await;
        }
    });

    (Some(handler), Some(tx))
}

#[cfg(test)]
mod tests {
    use super::{Job, Runner};
    use async_trait::async_trait;
    use cron::Schedule;
    use std::str::FromStr;
    struct SomeJob;

    #[async_trait]
    impl Job for SomeJob {
        fn schedule(&self) -> Option<Schedule> {
            Some(Schedule::from_str("0 * * * * *").unwrap())
        }

        async fn handle(&mut self) {}
    }
    struct AnotherJob;
    #[async_trait]
    impl Job for AnotherJob {
        fn schedule(&self) -> Option<Schedule> {
            Some(Schedule::from_str("0 * * * * *").unwrap())
        }

        async fn handle(&mut self) {}
    }
    #[tokio::test]
    async fn create_job() {
        let mut some_job = SomeJob;

        assert_eq!(some_job.handle().await, ());
    }

    #[tokio::test]
    async fn test_adding_jobs_to_runner() {
        let some_job = SomeJob;
        let another_job = AnotherJob;

        let runner = Runner::new()
            .add(Box::new(some_job))
            .add(Box::new(another_job));

        assert_eq!(runner.jobs_to_run(), 2);
    }

    #[tokio::test]
    async fn test_jobs_are_empty_after_runner_starts() {
        let some_job = SomeJob;
        let another_job = AnotherJob;

        let runner = Runner::new()
            .add(Box::new(some_job))
            .add(Box::new(another_job))
            .run()
            .await;

        assert_eq!(runner.jobs_to_run(), 0);
    }

    #[tokio::test]
    async fn test_stopping_the_runner() {
        let some_job = SomeJob;
        let another_job = AnotherJob;

        let mut runner = Runner::new()
            .add(Box::new(some_job))
            .add(Box::new(another_job))
            .run()
            .await;

        assert_eq!(runner.stop().await, ());
    }
}