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
use crate::error::SchedulerError;
use crate::job::{Job, JobScheduler};
use crate::scheduler::Scheduler;
use chrono::Utc;
use chrono_tz::{Tz, UTC};
use log::*;
use std::convert::TryInto;
use std::sync::Arc;

pub mod error;
pub mod job;
pub mod scheduler;

pub struct JobExecutor {
    timezone: Option<Tz>,
    jobs: Vec<Arc<JobScheduler>>,
}

/// Creates a new Executor that uses the Local time zone for the execution times evaluation.
/// For example, the cron expressions will refer to the Local time zone.
pub fn new_executor_with_local_tz() -> JobExecutor {
    new_executor_with_tz(None)
}

/// Creates a new Executor that uses the UTC time zone for the execution times evaluation.
/// For example, the cron expressions will refer to the UTC time zone.
pub fn new_executor_with_utc_tz() -> JobExecutor {
    new_executor_with_tz(Some(UTC))
}

/// Creates a new Executor that uses a custom time zone for the execution times evaluation.
/// For example, the cron expressions will refer to the specified time zone.
pub fn new_executor_with_tz(timezone: Option<Tz>) -> JobExecutor {
    JobExecutor {
        timezone,
        jobs: vec![],
    }
}

impl JobExecutor {
    /// Returns true if the JobExecutor contains no jobs.
    pub fn is_empty(&self) -> bool {
        self.jobs.is_empty()
    }

    /// Returns the number of jobs in the JobExecutor.
    pub fn len(&self) -> usize {
        self.jobs.len()
    }

    /// Clear the JobExecutor, removing all jobs.
    pub fn clear(&mut self) {
        self.jobs.clear()
    }

    /// Returns true if there is at least one job pending.
    pub fn is_pending(&self) -> bool {
        for job in &self.jobs {
            if job.is_pending() {
                return true;
            }
        }
        false
    }

    /// Run pending jobs in the JobExecutor.
    pub fn run_pending(&mut self) {
        for job_scheduler in &self.jobs {
            //println!("check JOB IS PENDING: {}", job.is_pending());
            if job_scheduler.is_pending() {
                match job_scheduler.job.is_running() {
                    Ok(is_running) => {
                        //println!("JOB IS RUNNING? {}", is_running);
                        if !is_running {
                            let job_clone = job_scheduler.clone();
                            std::thread::spawn(move || {
                                let timestamp = Utc::now().timestamp();
                                let group = job_clone.job.group();
                                let name = job_clone.job.name();
                                let span =
                                    tracing::error_span!("run_pending", group, name, timestamp);
                                let _enter = span.enter();

                                info!("Start execution of Job [{}/{}]", group, name);
                                match job_clone.run() {
                                    Ok(()) => {
                                        info!(
                                            "Execution of Job [{}/{}] completed successfully",
                                            group, name
                                        );
                                    }
                                    Err(err) => {
                                        error!(
                                            "Execution of Job [{}/{}] completed with errors. Err: {}",
                                            group,
                                            name,
                                            err
                                        );
                                    }
                                }
                            });
                        } else {
                            debug!(
                                "Job [{}/{}] is pending but already running. It will not be executed.",
                                job_scheduler.job.group(),
                                job_scheduler.job.name()
                            )
                        }
                    }
                    Err(err) => error!(
                        "Cannot start execution of Job [{}/{}] because status is unknown. Err: {}",
                        job_scheduler.job.group(),
                        job_scheduler.job.name(),
                        err
                    ),
                }
            }
        }
    }

    /// Adds a job to the JobExecutor.
    pub fn add_job<S: TryInto<Scheduler>>(
        &mut self,
        schedule: S,
        job: Job,
    ) -> Result<(), SchedulerError>
    where
        SchedulerError: std::convert::From<<S as std::convert::TryInto<Scheduler>>::Error>,
    {
        self.add_job_with_scheduler(schedule.try_into()?, job);
        Ok(())
    }

    /// Adds a job to the JobExecutor.
    pub fn add_job_with_scheduler(&mut self, schedule: Scheduler, job: Job) {
        self.jobs
            .push(Arc::new(JobScheduler::new(schedule, self.timezone, job)));
    }
}

#[cfg(test)]
pub mod test {

    use super::*;
    use chrono::Utc;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::sync::mpsc::channel;
    use std::time::Duration;

    #[test]
    fn should_not_run_an_already_running_job() {
        let mut executor = new_executor_with_utc_tz();

        let count = Arc::new(AtomicUsize::new(0));
        let count_clone = count.clone();

        let (tx, rx) = channel();

        executor
            .add_job(
                Duration::new(0, 1),
                Job::new("g", "n", None, move || {
                    tx.send("").unwrap();
                    println!("job - started");
                    count_clone.fetch_add(1, Ordering::SeqCst);
                    std::thread::sleep(Duration::new(1, 0));
                    Ok(())
                }),
            )
            .unwrap();

        for i in 0..100 {
            println!("run_pending {}", i);
            executor.run_pending();
            std::thread::sleep(Duration::new(0, 2));
        }

        println!("run_pending completed");
        rx.recv().unwrap();

        assert_eq!(count.load(Ordering::Relaxed), 1);
    }

    #[test]
    fn a_running_job_should_not_block_the_executor() {
        let mut executor = new_executor_with_local_tz();

        let (tx, rx) = channel();

        let count_1 = Arc::new(AtomicUsize::new(0));
        let count_clone_1 = count_1.clone();
        let tx_1 = tx.clone();
        executor
            .add_job(
                Duration::new(0, 1),
                Job::new("g", "n", None, move || {
                    tx_1.send("").unwrap();
                    println!("job 1 - started");
                    count_clone_1.fetch_add(1, Ordering::SeqCst);
                    std::thread::sleep(Duration::new(1, 0));
                    Ok(())
                }),
            )
            .unwrap();

        let count_2 = Arc::new(AtomicUsize::new(0));
        let count_2_clone = count_2.clone();
        let tx_2 = tx.clone();
        executor
            .add_job(
                Duration::new(0, 1),
                Job::new("g", "n", None, move || {
                    tx_2.send("").unwrap();
                    println!("job 2 - started");
                    count_2_clone.fetch_add(1, Ordering::SeqCst);
                    std::thread::sleep(Duration::new(1, 0));
                    Ok(())
                }),
            )
            .unwrap();

        let count_3 = Arc::new(AtomicUsize::new(0));
        let count_3_clone = count_3.clone();
        let tx_3 = tx.clone();
        executor
            .add_job(
                Duration::new(0, 1),
                Job::new("g", "n", None, move || {
                    tx_3.send("").unwrap();
                    println!("job 3 - started");
                    count_3_clone.fetch_add(1, Ordering::SeqCst);
                    std::thread::sleep(Duration::new(1, 0));
                    Ok(())
                }),
            )
            .unwrap();

        let before_millis = Utc::now().timestamp_millis();
        for i in 0..100 {
            println!("run_pending {}", i);
            executor.run_pending();
            std::thread::sleep(Duration::new(0, 1_000_000));
        }
        let after_millis = Utc::now().timestamp_millis();

        assert!((after_millis - before_millis) >= 100);
        assert!((after_millis - before_millis) < 1000);

        rx.recv().unwrap();

        assert_eq!(count_1.load(Ordering::SeqCst), 1);
        assert_eq!(count_2.load(Ordering::SeqCst), 1);
        assert_eq!(count_3.load(Ordering::SeqCst), 1);
    }

    #[test]
    fn should_add_with_explicit_scheduler() {
        let mut executor = new_executor_with_utc_tz();
        executor.add_job_with_scheduler(Scheduler::Never, Job::new("g", "n", None, move || Ok(())));
    }
}