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
#![deny(missing_docs)]
use anyhow::Error;
#[cfg(feature = "with-actix")]
mod actix_job;
mod job;
mod job_info;
mod processor_map;
mod stats;
mod storage;
pub use crate::{
job::{new_job, new_scheduled_job, process, Job},
job_info::{JobInfo, NewJobInfo, ReturnJobInfo},
processor_map::{CachedProcessorMap, ProcessorMap},
stats::{JobStat, Stats},
storage::{memory_storage, Storage},
};
#[cfg(feature = "with-actix")]
pub use actix_job::ActixJob;
#[derive(Debug, thiserror::Error)]
pub enum JobError {
#[error("Error performing job: {0}")]
Processing(#[from] Error),
#[error("Could not make JSON value from arguments")]
Json,
#[error("This job type was not registered for the client")]
Unregistered,
}
#[derive(Clone, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
pub enum JobResult {
Success,
Failure,
Unregistered,
Unexecuted,
}
impl JobResult {
pub fn success() -> Self {
JobResult::Success
}
pub fn failure() -> Self {
JobResult::Failure
}
pub fn unregistered() -> Self {
JobResult::Unregistered
}
pub fn is_failure(&self) -> bool {
*self == JobResult::Failure
}
pub fn is_success(&self) -> bool {
*self == JobResult::Success
}
pub fn is_unregistered(&self) -> bool {
*self == JobResult::Unregistered
}
pub fn is_unexecuted(&self) -> bool {
*self == JobResult::Unexecuted
}
}
#[derive(Clone, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
pub enum JobStatus {
Pending,
Running,
}
impl JobStatus {
pub fn pending() -> Self {
JobStatus::Pending
}
pub fn running() -> Self {
JobStatus::Running
}
pub fn is_pending(&self) -> bool {
*self == JobStatus::Pending
}
pub fn is_running(&self) -> bool {
*self == JobStatus::Running
}
}
impl std::fmt::Display for JobStatus {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
JobStatus::Pending => write!(f, "Pending"),
JobStatus::Running => write!(f, "Running"),
}
}
}
#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd, thiserror::Error)]
#[error("Invalid job status")]
pub struct JobStatusError;
impl std::str::FromStr for JobStatus {
type Err = JobStatusError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"Pending" => Ok(JobStatus::Pending),
"Running" => Ok(JobStatus::Running),
_ => Err(JobStatusError),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
pub enum Backoff {
Linear(usize),
Exponential(usize),
}
#[derive(Clone, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
pub enum MaxRetries {
Infinite,
Count(usize),
}
impl MaxRetries {
fn compare(&self, retry_count: u32) -> ShouldStop {
match *self {
MaxRetries::Infinite => ShouldStop::Requeue,
MaxRetries::Count(ref count) => {
if (retry_count as usize) <= *count {
ShouldStop::Requeue
} else {
ShouldStop::LimitReached
}
}
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ShouldStop {
LimitReached,
Requeue,
}
impl ShouldStop {
pub fn should_requeue(&self) -> bool {
*self == ShouldStop::Requeue
}
}
impl From<serde_json::error::Error> for JobError {
fn from(_: serde_json::error::Error) -> Self {
JobError::Json
}
}