tokio_easy_timer/
job.rs

1mod async_handler;
2mod async_job;
3mod jobschedule;
4mod sync_handler;
5mod sync_job;
6pub use self::async_handler::AsyncHandler;
7pub use self::async_job::{AsyncJob, AsyncJobBuilder};
8pub use self::jobschedule::JobScheduleBuilder;
9pub use self::sync_handler::SyncHandler;
10pub use self::sync_job::{SyncJob, SyncJobBuilder};
11use crate::{extensions::Extensions, interval::Interval, prelude::TimeUnits};
12use chrono::TimeZone;
13
14pub trait Job<Tz>
15where
16    Tz: TimeZone,
17{
18    fn box_clone(&self) -> Box<dyn Job<Tz> + Send>;
19
20    /// Start spawn jobs
21    fn start_schedule(&self, e: Extensions, tz: Tz);
22}
23
24pub trait JobBuilder<Args> {
25    /// Constructs a new job builder
26    fn new() -> Self;
27
28    /// Specify another time the task will run
29    fn and(&mut self) -> &mut Self;
30
31    /// The task will run n times in sequence. You need to specify the number and the interval between tasks.
32    fn repeat_seq(&mut self, n: u32, interval: Interval) -> &mut Self;
33
34    /// Spawn n tasks at the same time. You need to specify the number and the interval between tasks.
35    fn repeat_async(&mut self, n: u32, interval: Interval) -> &mut Self;
36
37    /// Usually not use it directly, use `at_*` and `since_*` is better
38    fn get_mut_cron_builder(&mut self) -> &mut JobScheduleBuilder;
39
40    /// Specify a specific run time, equivalent to cron 'n'
41    fn at(&mut self, interval: Interval) -> &mut Self {
42        self.get_mut_cron_builder().at(interval);
43        self
44    }
45
46    /// Specify a specific run time, equivalent to the corn expression 'm/n'
47    fn since_every(&mut self, start: Interval, interval: Interval) -> &mut Self {
48        self.get_mut_cron_builder().since_every(start, interval);
49        self
50    }
51
52    /// Specify a specific run time, equivalent to the corn expression '0/n'
53    fn every(&mut self, interval: Interval) -> &mut Self {
54        self.get_mut_cron_builder().every(interval);
55        self
56    }
57
58    /// Specify a specific run time, equivalent to the corn expression 'm-n'
59    fn from_to(&mut self, start: Interval, end: Interval) -> &mut Self {
60        self.get_mut_cron_builder().from_to(start, end);
61        self
62    }
63
64    /// Specify a specific run time, the same as `at`
65    fn at_datetime(
66        &mut self,
67        year: Option<i32>,
68        month: Option<u32>,
69        day: Option<u32>,
70        hour: Option<u32>,
71        min: Option<u32>,
72        sec: Option<u32>,
73    ) -> &mut Self {
74        year.map(|x| self.at((x as u32).year()));
75        month.map(|x| self.at((x as u32).month()));
76        day.map(|x| self.at((x as u32).day()));
77        hour.map(|x| self.at((x as u32).hour()));
78        min.map(|x| self.at((x as u32).minute()));
79        sec.map(|x| self.at((x as u32).second()));
80        self
81    }
82
83    /// Specify a specific run time, the same as `at`
84    fn at_time(&mut self, hour: u32, min: u32, sec: u32) -> &mut Self {
85        self.at_datetime(None, None, None, Some(hour), Some(min), Some(sec));
86        self
87    }
88
89    /// Specify a specific run time, the same as `at`
90    fn at_date(&mut self, year: i32, month: u32, day: u32) -> &mut Self {
91        self.at_datetime(Some(year), Some(month), Some(day), None, None, None);
92        self
93    }
94
95    fn get_mut_since(&mut self) -> &mut (i32, u32, u32, u32, u32, u32);
96
97    /// Specify the datetime after which the task will start, the same as `since`
98    fn since_datetime(
99        &mut self,
100        year: i32,
101        month: u32,
102        day: u32,
103        hour: u32,
104        min: u32,
105        sec: u32,
106    ) -> &mut Self {
107        *(self.get_mut_since()) = (year, month, day, hour, min, sec);
108        self
109    }
110
111    /// Specify the date after which the task will start, the same as `since`
112    fn since_date(&mut self, year: i32, month: u32, day: u32) -> &mut Self {
113        self.get_mut_since().0 = year;
114        self.get_mut_since().1 = month;
115        self.get_mut_since().2 = day;
116        self
117    }
118
119    /// Specify the time after which the task will start, the same as `since`
120    fn since_time(&mut self, hour: u32, min: u32, sec: u32) -> &mut Self {
121        self.get_mut_since().3 = hour;
122        self.get_mut_since().4 = min;
123        self.get_mut_since().5 = sec;
124        self
125    }
126
127    /// Specify when the task will start after, like `since`
128    fn after(&mut self, delay: u64) -> &mut Self {
129        self.get_mut_cron_builder().add_delay(delay);
130        self
131    }
132}