1use awa_model::JobRow;
2use chrono::{DateTime, Utc};
3use std::future::Future;
4use std::pin::Pin;
5use std::sync::Arc;
6use std::time::Duration;
7
8#[derive(Debug, Clone)]
10pub enum JobEvent<T> {
11 Started { args: T, job: JobRow },
13 WaitingForCallback { args: T, job: JobRow },
16 Completed {
21 args: T,
22 job: JobRow,
23 duration: Duration,
24 },
25 Retried {
27 args: T,
28 job: JobRow,
29 error: String,
30 attempt: i16,
31 next_run_at: DateTime<Utc>,
32 },
33 Exhausted {
35 args: T,
36 job: JobRow,
37 error: String,
38 attempt: i16,
39 },
40 Cancelled {
42 args: T,
43 job: JobRow,
44 reason: String,
45 },
46 Rescued {
52 args: T,
53 job: JobRow,
54 reason: RescueReason,
55 },
56}
57
58#[derive(Debug, Clone, Copy, PartialEq, Eq)]
60pub enum RescueReason {
61 ExpiredCallback,
63 StaleHeartbeat,
65 DeadlineExceeded,
67}
68
69impl RescueReason {
70 pub fn as_str(self) -> &'static str {
72 match self {
73 RescueReason::ExpiredCallback => "expired_callback",
74 RescueReason::StaleHeartbeat => "stale_heartbeat",
75 RescueReason::DeadlineExceeded => "deadline_exceeded",
76 }
77 }
78}
79
80impl<T> JobEvent<T> {
81 pub fn job(&self) -> &JobRow {
83 match self {
84 JobEvent::Started { job, .. }
85 | JobEvent::WaitingForCallback { job, .. }
86 | JobEvent::Completed { job, .. }
87 | JobEvent::Retried { job, .. }
88 | JobEvent::Exhausted { job, .. }
89 | JobEvent::Cancelled { job, .. }
90 | JobEvent::Rescued { job, .. } => job,
91 }
92 }
93}
94
95#[derive(Debug, Clone)]
97pub enum UntypedJobEvent {
98 Started { job: JobRow },
100 WaitingForCallback { job: JobRow },
102 Completed { job: JobRow, duration: Duration },
104 Retried {
106 job: JobRow,
107 error: String,
108 attempt: i16,
109 next_run_at: DateTime<Utc>,
110 },
111 Exhausted {
113 job: JobRow,
114 error: String,
115 attempt: i16,
116 },
117 Cancelled { job: JobRow, reason: String },
119 Rescued { job: JobRow, reason: RescueReason },
121}
122
123impl UntypedJobEvent {
124 pub fn job(&self) -> &JobRow {
126 match self {
127 UntypedJobEvent::Started { job, .. }
128 | UntypedJobEvent::WaitingForCallback { job, .. }
129 | UntypedJobEvent::Completed { job, .. }
130 | UntypedJobEvent::Retried { job, .. }
131 | UntypedJobEvent::Exhausted { job, .. }
132 | UntypedJobEvent::Cancelled { job, .. }
133 | UntypedJobEvent::Rescued { job, .. } => job,
134 }
135 }
136
137 pub(crate) fn into_typed<T>(self, args: T) -> JobEvent<T> {
138 match self {
139 UntypedJobEvent::Started { job } => JobEvent::Started { args, job },
140 UntypedJobEvent::WaitingForCallback { job } => {
141 JobEvent::WaitingForCallback { args, job }
142 }
143 UntypedJobEvent::Completed { job, duration } => JobEvent::Completed {
144 args,
145 job,
146 duration,
147 },
148 UntypedJobEvent::Retried {
149 job,
150 error,
151 attempt,
152 next_run_at,
153 } => JobEvent::Retried {
154 args,
155 job,
156 error,
157 attempt,
158 next_run_at,
159 },
160 UntypedJobEvent::Exhausted {
161 job,
162 error,
163 attempt,
164 } => JobEvent::Exhausted {
165 args,
166 job,
167 error,
168 attempt,
169 },
170 UntypedJobEvent::Cancelled { job, reason } => JobEvent::Cancelled { args, job, reason },
171 UntypedJobEvent::Rescued { job, reason } => JobEvent::Rescued { args, job, reason },
172 }
173 }
174}
175
176pub(crate) type BoxedLifecycleFuture = Pin<Box<dyn Future<Output = ()> + Send + 'static>>;
177pub(crate) type BoxedUntypedEventHandler =
178 Arc<dyn Fn(UntypedJobEvent) -> BoxedLifecycleFuture + Send + Sync + 'static>;