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
use crate::error::SchedulerError;
use crate::scheduler::Scheduler;
use chrono::{DateTime, TimeZone, Utc};
use std::convert::TryInto;
use std::sync::{Mutex, RwLock};

pub struct JobScheduler<T: TimeZone + Send + Sync + 'static>
where
    <T as chrono::offset::TimeZone>::Offset: std::marker::Send,
{
    pub job: Job,
    schedule: Scheduler,
    timezone: T,
    next_run_at: Mutex<Option<DateTime<T>>>,
    last_run_at: Mutex<Option<DateTime<T>>>,
}

impl<T: TimeZone + Send + Sync + 'static> JobScheduler<T>
where
    <T as chrono::offset::TimeZone>::Offset: std::marker::Send,
{
    pub fn new<S: TryInto<Scheduler>>(
        schedule: S,
        timezone: T,
        job: Job,
    ) -> Result<Self, SchedulerError>
    where
        SchedulerError: std::convert::From<<S as std::convert::TryInto<Scheduler>>::Error>,
    {
        // Determine the next time it should run
        let schedule = schedule.try_into()?;
        let next_run_at = schedule.next(&Utc::now().with_timezone(&timezone));

        Ok(JobScheduler {
            job,
            schedule,
            timezone,
            next_run_at: Mutex::new(next_run_at),
            last_run_at: Mutex::new(None),
        })
    }

    /// Returns true if this job is pending execution.
    pub fn is_pending(&self) -> bool {
        // Check if paused
        if !self.job.is_active {
            return false;
        }

        // Check if NOW is on or after next_run_at
        if let Some(next_run_at) = self.next_run_at.lock().unwrap().as_ref() {
            *next_run_at < Utc::now().with_timezone(&self.timezone)
        } else {
            false
        }
    }

    /// Run the job immediately and re-schedule it.
    pub fn run(&self) -> Result<(), Box<dyn std::error::Error>> {
        // Execute the job function
        let run_result = self.job.run();

        let now = Utc::now().with_timezone(&self.timezone);

        // Determine the next time it should run
        let mut next_run_at = self.next_run_at.lock().unwrap();
        *next_run_at = self.schedule.next(&now);

        // Save the last time this ran
        let mut last_run_at = self.last_run_at.lock().unwrap();
        *last_run_at = Some(now);

        run_result
    }
}

pub type JobFn = dyn Fn() -> Result<(), Box<dyn std::error::Error>> + Send;

pub struct Job {
    function: Mutex<Box<JobFn>>,
    group: String,
    name: String,
    is_active: bool,
    is_running: RwLock<bool>,
}

impl Job {
    pub fn new<
        G: Into<String>,
        N: Into<String>,
        F: Fn() -> Result<(), Box<dyn std::error::Error>> + Send,
    >(
        group: G,
        name: N,
        function: F,
    ) -> Self
    where
        F: 'static,
    {
        Job {
            function: Mutex::new(Box::new(function)),
            name: name.into(),
            group: group.into(),
            is_running: RwLock::new(false),
            is_active: true,
        }
    }

    /// Returns true if this job is currently running.
    pub fn is_running(&self) -> Result<bool, SchedulerError> {
        let read = self
            .is_running
            .read()
            .map_err(|err| SchedulerError::JobLockError {
                message: format!(
                    "Cannot read is_running status of job [{}/{}]. Err: {}",
                    self.group, self.name, err
                ),
            })?;
        Ok(*read)
    }

    pub fn name(&self) -> &str {
        &self.name
    }

    pub fn group(&self) -> &str {
        &self.group
    }

    /// Run the job immediately and re-schedule it.
    pub fn run(&self) -> Result<(), Box<dyn std::error::Error>> {
        self.set_running(true)?;

        // Execute the job function
        let run_result = {
            let function = self
                .function
                .lock()
                .map_err(|err| SchedulerError::JobLockError {
                    message: format!(
                        "Cannot execute job [{}/{}]. Err: {}",
                        self.group, self.name, err
                    ),
                })?;
            (function)()
        };

        self.set_running(false)?;

        run_result
    }

    fn set_running(&self, is_running: bool) -> Result<(), SchedulerError> {
        let mut write = self
            .is_running
            .write()
            .map_err(|err| SchedulerError::JobLockError {
                message: format!(
                    "Cannot write is_running status of job [{}/{}]. Err: {}",
                    self.group, self.name, err
                ),
            })?;

        if is_running.eq(&*write) {
            return Err(SchedulerError::JobLockError {
                message: format!(
                    "Wrong Job status found for job [{}/{}]. Expected: {}",
                    self.group, self.name, !is_running
                ),
            });
        }

        *write = is_running;
        Ok(())
    }
}

#[cfg(test)]
pub mod test {

    use super::*;
    use std::sync::mpsc::channel;
    use std::sync::Arc;
    use std::time::Duration;

    #[test]
    fn should_be_running() {
        let lock = Arc::new(Mutex::new(true));
        let lock_clone = lock.clone();
        let (tx, rx) = channel();
        let tx_clone = tx.clone();

        let job_scheduler = Arc::new(
            JobScheduler::new(
                Duration::new(1, 0),
                Utc,
                Job::new("g", "n", move || {
                    println!("job - started");
                    tx_clone.send("").unwrap();
                    println!("job - Trying to get the lock");
                    let _lock = lock_clone.lock().unwrap();
                    println!("job - lock acquired");
                    Ok(())
                }),
            )
            .unwrap(),
        );

        assert!(!job_scheduler.job.is_running().unwrap());

        {
            let _lock = lock.lock().unwrap();
            let job_clone = job_scheduler.clone();
            std::thread::spawn(move || {
                println!("starting job");
                job_clone.run().unwrap();
                println!("end job execution");
                tx.send("").unwrap();
            });
            rx.recv().unwrap();
            assert!(job_scheduler.job.is_running().unwrap());
        }

        rx.recv().unwrap();
        assert!(!job_scheduler.job.is_running().unwrap());
    }
}