celery/beat/
scheduled_task.rs1use super::Schedule;
2use crate::protocol::TryCreateMessage;
3use std::{cmp::Ordering, time::SystemTime};
4
5pub struct ScheduledTask {
8 pub name: String,
9 pub message_factory: Box<dyn TryCreateMessage>,
10 pub queue: String,
11 pub schedule: Box<dyn Schedule>,
12 pub total_run_count: u32,
13 pub last_run_at: Option<SystemTime>,
14 pub next_call_at: SystemTime,
15}
16
17impl ScheduledTask {
18 pub fn new<S>(
20 name: String,
21 message_factory: Box<dyn TryCreateMessage>,
22 queue: String,
23 schedule: S,
24 next_call_at: SystemTime,
25 ) -> ScheduledTask
26 where
27 S: Schedule + 'static,
28 {
29 ScheduledTask {
30 name,
31 message_factory,
32 queue,
33 schedule: Box::new(schedule),
34 total_run_count: 0,
35 last_run_at: None,
36 next_call_at,
37 }
38 }
39
40 pub(super) fn reschedule_task(mut self) -> Option<ScheduledTask> {
44 match self.schedule.next_call_at(self.last_run_at) {
45 Some(next_call_at) => {
46 self.next_call_at = next_call_at;
47 Some(self)
48 }
49 None => None,
50 }
51 }
52}
53
54impl Ord for ScheduledTask {
58 fn cmp(&self, other: &Self) -> Ordering {
59 other.next_call_at.cmp(&self.next_call_at)
63 }
64}
65
66impl PartialOrd for ScheduledTask {
67 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
68 Some(self.cmp(other))
69 }
70}
71
72impl PartialEq for ScheduledTask {
73 fn eq(&self, other: &Self) -> bool {
74 other.cmp(self) == Ordering::Equal
75 }
76}
77
78impl Eq for ScheduledTask {}