tasklet 0.2.12

An asynchronous task scheduling library
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
use crate::errors::{TaskError, TaskResult};
use crate::generator::TaskGenerator;
use crate::task::{run_task, Status, Task, TaskCmd, TaskResponse};
use crate::{scheduler_log, task_log};
use chrono::prelude::*;
use chrono::Utc;
use futures::future::join_all;
use futures::StreamExt;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use tokio::sync::{mpsc, oneshot};
use tokio::task::JoinHandle;

/// Task execution possible statuses.
#[derive(Debug, PartialEq)]
pub(crate) enum ExecutionStatus {
    Success(usize),
    NoExecution,
    HadError(usize, usize),
}

/// Handler for task threads.
/// Contains the join handle and sender for each task.
///
/// When a task is finished the handle must be destroyed and sender dropped, in order to totally remove the task from the execution context.
///
/// The #id must be set upon the task initialization in order to be easier to query for later use.
#[derive(Debug)]
pub struct TaskHandle {
    id: usize,
    handle: JoinHandle<()>,
    sender: mpsc::Sender<TaskCmd>,
    is_init: bool,
}

/// Task scheduler and executor.
pub struct TaskScheduler<T>
where
    T: TimeZone + Clone + Send + 'static,
{
    /// The task handles from the registered tasks.
    handles: Vec<TaskHandle>,
    /// The (optional) task generation function.
    task_gen: Option<TaskGenerator<T>>,
    /// The sleep time in ms.
    sleep: usize,
    /// The id that should be assigned to the next appended task.
    next_id: usize,
    /// The main timezone used for the scheduler.
    timezone: T,
}

/// `TaskScheduler` implementation.
impl<T> TaskScheduler<T>
where
    T: TimeZone + Clone + Send + 'static,
{
    /// Create a new instance of `TaskSchedule` with default sleep and no tasks to execute.
    ///
    /// # Arguments
    ///
    /// * timezone - the scheduler's timezone.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use tasklet::TaskScheduler;
    /// let _ = TaskScheduler::default(chrono::Utc);
    /// ```
    pub fn default(timezone: T) -> TaskScheduler<T> {
        TaskScheduler {
            handles: Vec::new(),
            /* Originally empty, no registered tasks. */
            task_gen: None,
            sleep: 1000,
            timezone,
            next_id: 0,
        }
    }

    /// Create a new instance of `TaskScheduler` with no tasks to execute.
    ///
    /// # Arguments
    ///
    /// * sleep     - The execution frequency (in ms).
    /// * timezone  - The scheduler's timezone.
    ///
    /// # Examples
    ///
    /// ```
    /// # use tasklet::TaskScheduler;
    /// // Create a new `TaskScheduler` instance that executes every 1000ms.
    /// let _ = TaskScheduler::new(1000, chrono::Local);
    /// ```
    pub fn new(sleep: usize, timezone: T) -> TaskScheduler<T> {
        TaskScheduler {
            sleep,
            ..TaskScheduler::default(timezone)
        }
    }

    /// Set a `TaskGenerator` instance for the TaskScheduler.
    ///
    /// # Arguments
    ///
    /// * task_gen - a `TaskGenerator` instance.
    ///
    /// # Examples
    ///
    /// ```
    /// # use tasklet::{TaskScheduler, TaskGenerator};
    /// // Create a new `TaskScheduler` instance and attach an `TaskGenerator` to it.
    /// let mut scheduler = TaskScheduler::default(chrono::Local);
    /// let mut generator = TaskGenerator::new("1 * * * * * *", chrono::Local, || None);
    /// scheduler.set_task_gen(generator);
    /// ```
    pub fn set_task_gen(&mut self, task_gen: TaskGenerator<T>) -> &mut TaskScheduler<T> {
        self.task_gen = Some(task_gen);
        self
    }

    /// Add a new task in the execution queue.
    ///
    /// # Arguments
    ///
    /// * task - a `Task` instance.
    ///
    /// # Examples
    ///
    /// ```
    /// # use tasklet::{TaskScheduler, Task};
    /// # tokio_test::block_on( async {
    /// // Create a new `TaskScheduler` and attach a task to it.
    /// let mut scheduler = TaskScheduler::default(chrono::Local);
    /// // Add a task that executes every second forever.
    /// scheduler.add_task(Task::new("* * * * * * *", None, None, chrono::Local)).unwrap();
    /// # });
    /// ```
    pub fn add_task(
        &mut self,
        task: TaskResult<Task<T>>,
    ) -> Result<&mut TaskScheduler<T>, TaskError> {
        match task {
            Ok(mut task) => {
                let (sender, receiver) = mpsc::channel(32);

                task.set_receiver(receiver);
                task.set_id(self.next_id);
                let handle = tokio::spawn(run_task(task));

                // Push the handle
                self.handles.push(TaskHandle {
                    id: self.next_id,
                    handle,
                    sender,
                    is_init: false,
                });

                // Increase the id of the next task.
                self.next_id += 1;
                Ok(self)
            }
            Err(e) => Err(e),
        }
    }

    /// Execute all the tasks in the queue.
    ///
    /// After the execution the tasks are rescheduled and if needed,
    /// removed from the list.
    pub(crate) async fn execute_tasks(&mut self) -> ExecutionStatus {
        let mut receivers: Vec<oneshot::Receiver<TaskResponse>> = Vec::new();

        for handle in &self.handles {
            let (sender, recv) = oneshot::channel();
            let _ = handle.sender.send(TaskCmd::Run { sender }).await;
            receivers.push(recv);
        }

        let err_no: Arc<Mutex<usize>> = Arc::new(Mutex::new(0usize));
        let total_runs: Arc<Mutex<usize>> = Arc::new(Mutex::new(0usize));
        futures::stream::iter(receivers)
            .for_each(|r| async {
                match r.await.unwrap().status {
                    Status::Executed => {
                        *total_runs.lock().unwrap() += 1;
                    }
                    Status::Failed => {
                        *err_no.lock().unwrap() += 1;
                        *total_runs.lock().unwrap() += 1;
                    }
                    _ => { /* Do nothing */ }
                };
            })
            .await;

        // Send for reschedule
        receivers = Vec::new();
        for handle in &self.handles {
            let (send, recv) = oneshot::channel();
            let _ = handle
                .sender
                .send(TaskCmd::Reschedule { sender: send })
                .await;
            receivers.push(recv);
        }

        for recv in receivers {
            let res = recv.await.unwrap();
            if res.status == Status::Finished || res.status == Status::ForceRemoved {
                for handle in &self.handles {
                    if handle.id == res.id {
                        task_log!(
                            res.id,
                            log::Level::Debug,
                            "Killing task due to {}",
                            if res.status == Status::Finished {
                                "end of execution cycle"
                            } else {
                                "force removal"
                            }
                        );
                        handle.handle.abort();
                    }
                }
                let index = self.handles.iter().position(|x| x.id == res.id).unwrap();
                self.handles.remove(index);
            }
        }

        // Build the response
        if *total_runs.lock().unwrap() > 0 {
            if *err_no.lock().unwrap() == 0 {
                ExecutionStatus::Success(*total_runs.lock().unwrap())
            } else {
                ExecutionStatus::HadError(*total_runs.lock().unwrap(), *err_no.lock().unwrap())
            }
        } else {
            ExecutionStatus::NoExecution
        }
    }

    /// Send an init signal to all the tasks that are not yet initialized.
    pub(crate) async fn init_tasks(&mut self) {
        let mut receivers: Vec<oneshot::Receiver<TaskResponse>> = Vec::new();
        let mut count: usize = 0;

        // Send init signal to all the tasks that are not initialized yet.
        for handle in &self.handles {
            if !handle.is_init {
                let (sender, recv) = oneshot::channel();
                let _ = handle.sender.send(TaskCmd::Init { sender }).await;
                receivers.push(recv);
                count += 1;
            }
        }

        if count > 0 {
            // Await for all receivers to finish
            join_all(receivers).await.iter().for_each(|r| match r {
                Ok(r) => match r.status {
                    Status::Scheduled => {
                        self.handles
                            .iter_mut()
                            .filter(|h| h.id == r.id)
                            .for_each(|h| {
                                task_log!(h.id, log::Level::Info, "Initialized");
                                h.is_init = true;
                            });
                    }
                    _ => {
                        task_log!(r.id, log::Level::Error, "Failed to initialize");
                    }
                },
                Err(_) => {
                    scheduler_log!(
                        log::Level::Error,
                        "RecvError returned by at least one uninitialized task"
                    );
                }
            });
        }
    }

    /// Execute the `TaskGenerator` instance (if set).
    ///
    /// This function will spawn the task, create its handle and attach it to the scheduler.
    fn run_task_gen(&mut self) -> bool {
        match self.task_gen {
            Some(ref mut tg) => {
                // Execute only if it's time to execute it.
                if tg.next_exec <= Utc::now().with_timezone(&self.timezone) {
                    return match tg.run() {
                        Some(t) => {
                            let _ = self.add_task(t);
                            true
                        }
                        None => false,
                    };
                }
                false
            }
            None => false,
        }
    }

    /// Main execution loop.
    ///
    /// Executes the main flow of the scheduler.
    /// At first initialize all the tasks and then run the execution loop.
    ///
    /// If there is a task generation/discovery method provided, executed on every loop.
    pub async fn run(&mut self) {
        scheduler_log!(
            log::Level::Info,
            "Scheduler started. Total tasks in queue: {}",
            self.handles.len()
        );

        // Initialize the tasks
        self.init_tasks().await;

        loop {
            if self.run_task_gen() {
                // Re-initialize the tasks if any new is added
                self.init_tasks().await;
            }
            match self.execute_tasks().await {
                ExecutionStatus::Success(c) => {
                    scheduler_log!(
                        log::Level::Info,
                        "Execution round completed successfully for {} task(s)",
                        c
                    );
                }
                ExecutionStatus::HadError(c, e) => {
                    scheduler_log!(
                        log::Level::Error,
                        "Execution round ran {} task(s) with {} error(s)",
                        c,
                        e
                    );
                }
                _ => { /* No executions */ }
            }
            tokio::time::sleep(Duration::from_millis(self.sleep as u64)).await;
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::task::TaskStepStatusErr::{Error, ErrorDelete};
    use crate::task::TaskStepStatusOk::Success;
    use crate::TaskBuilder;
    use chrono::Local;
    use std::time::Duration;

    #[tokio::test]
    async fn test_scheduler_normal_flow() {
        // Create a new scheduler instance.
        let mut scheduler = TaskScheduler::new(500, Local);
        // Add a couple of tasks.
        scheduler
            .add_task(Task::new("* * * * * * *", None, Some(2), Local))
            .unwrap()
            .add_task(Task::new("* * * * * * *", None, None, Local))
            .unwrap();
        assert_eq!(scheduler.handles.len(), 2);
        // Initialize the tasks.
        scheduler.init_tasks().await;
        tokio::time::sleep(Duration::from_millis(1000)).await;
        let status: ExecutionStatus = scheduler.execute_tasks().await;
        assert_eq!(status, ExecutionStatus::Success(2));
        assert_eq!(scheduler.handles.len(), 2);
        tokio::time::sleep(Duration::from_millis(1000)).await;
        let status: ExecutionStatus = scheduler.execute_tasks().await;
        assert_eq!(status, ExecutionStatus::Success(2));
        assert_eq!(scheduler.handles.len(), 1);
    }

    #[tokio::test]
    async fn test_scheduler_normal_force_deletion() {
        // Create a new scheduler instance.
        let mut scheduler = TaskScheduler::new(500, Local);
        // Create a task.
        let mut task = Task::new("* * * * * * *", None, Some(1), Local).unwrap();
        task.add_step_default(|| Err(ErrorDelete));
        // Add a couple of tasks.
        scheduler
            .add_task(Ok(task))
            .unwrap()
            .add_task(Task::new("* * * * * * *", None, None, Local))
            .unwrap();
        assert_eq!(scheduler.handles.len(), 2);
        // Initialize the tasks.
        scheduler.init_tasks().await;
        tokio::time::sleep(Duration::from_millis(1000)).await;
        scheduler.execute_tasks().await;
        // The first task should be force-removed at this point
        assert_eq!(scheduler.handles.len(), 1);
        tokio::time::sleep(Duration::from_millis(1000)).await;
        scheduler.execute_tasks().await;
        assert_eq!(scheduler.handles.len(), 1);
    }

    #[tokio::test]
    async fn test_scheduler_normal_flow_no_execution() {
        // Create a new scheduler instance.
        let mut scheduler = TaskScheduler::new(500, Local);
        // Init the scheduler.
        scheduler.init_tasks().await;
        tokio::time::sleep(Duration::from_millis(1000)).await;
        // Run the scheduler
        let status: ExecutionStatus = scheduler.execute_tasks().await;
        // No tasks should be executed.
        assert_eq!(status, ExecutionStatus::NoExecution);
    }

    #[tokio::test]
    async fn test_scheduler_normal_flow_error_case() {
        // Create a new scheduler instance.
        let mut scheduler = TaskScheduler::new(500, Local);

        // Create a task.
        let mut task = Task::new("* * * * * * *", None, Some(1), Local).unwrap();
        task.add_step_default(|| Ok(Success));
        // Return an error in the second step.
        task.add_step_default(|| Err(Error));

        // Add a task.
        scheduler.add_task(Ok(task)).unwrap();
        assert_eq!(scheduler.handles.len(), 1);
        // Initialize the task.
        scheduler.init_tasks().await;
        tokio::time::sleep(Duration::from_millis(1000)).await;
        scheduler.execute_tasks().await;
        // The task should be removed after it's execution circle.
        assert_eq!(scheduler.handles.len(), 0);
    }

    #[tokio::test]
    async fn test_scheduler_with_generator() {
        // Create a new scheduler instance.
        let mut scheduler = TaskScheduler::new(500, Local);

        // Add a task generator function that does now.
        scheduler.set_task_gen(TaskGenerator::new("* * * * * * *", Local, || None));

        // Should start with zero tasks.
        assert_eq!(scheduler.handles.len(), 0);

        // Execute the task generator.
        tokio::time::sleep(Duration::from_millis(1000)).await;
        scheduler.run_task_gen();

        // The number of tasks should be zero again.
        assert_eq!(scheduler.handles.len(), 0);

        // Update the generator to actually create a new task.
        scheduler.set_task_gen(TaskGenerator::new("* * * * * * *", Local, || {
            // Run at second "1" of every minute.

            // Create the task that will execute 2 total times.
            // Return the task for the execution queue.
            Some(TaskBuilder::new(Local).every("* * * * * * *").build())
        }));

        // Execute the task generator.
        tokio::time::sleep(Duration::from_millis(1000)).await;
        scheduler.run_task_gen();

        // The number of tasks should be zero again.
        assert_eq!(scheduler.handles.len(), 1);
    }

    #[test]
    fn test_task_builder_invalid_schedule() {
        // Test with valid schedule
        let result = TaskBuilder::new(chrono::Utc).every("* * * * * * *").build();
        assert!(result.is_ok());

        // Test with invalid schedule
        let result = TaskBuilder::new(chrono::Utc).every("invalid cron").build();
        assert!(result.is_err());

        // Test that the error is the correct type
        match result {
            Err(TaskError::InvalidCronExpression(_)) => {} // Expected
            _ => panic!("Expected InvalidCronExpression error"),
        }
    }
}