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
use super::utils;
use crate::utils::TryGetDuration as _;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use sqlx::{FromRow, Row, postgres::PgRow};
use std::time::Duration;
/// Policy to apply to the jobs in this queue.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all(serialize = "lowercase"))]
#[non_exhaustive]
pub enum QueuePolicy {
/// Standard (default).
///
/// Supports all standard features such as deferral, priority, and throttling.
#[default]
Standard,
/// Short.
///
/// All standard features, but only allows 1 job to be _queued_, unlimited active.
/// Can be extended with `singletonKey`
Short,
/// Singleton.
///
/// All standard features, but only allows 1 job to be _active_, unlimited queued.
/// Can be extended with `singletonKey`
Singleton,
/// Stately.
///
/// Combination of short and singleton: only allows 1 job per state, queued and/or active.
/// Can be extended with `singletonKey`
Stately,
/// Exclusive.
///
/// Only allows 1 job to be queued or active. Can be extended with singletonKey.
Exclusive,
}
impl TryFrom<String> for QueuePolicy {
type Error = String;
fn try_from(value: String) -> Result<Self, Self::Error> {
match value.as_str() {
"short" => Ok(Self::Short),
"singleton" => Ok(Self::Singleton),
"stately" => Ok(Self::Stately),
"standard" => Ok(Self::Standard),
other => Err(format!("Unsupported queue policy: {other}")),
}
}
}
impl std::fmt::Display for QueuePolicy {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = match self {
Self::Standard => "standard",
Self::Short => "short",
Self::Singleton => "singleton",
Self::Stately => "stately",
Self::Exclusive => "exclusive",
};
write!(f, "{s}")
}
}
/// Queue configuration.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct Queue<'a> {
/// Queue name.
pub name: &'a str,
/// Policy to apply to this queue.
pub policy: QueuePolicy,
/// Name of the dead letter queue.
///
/// Note that the dead letter queue itself should be created
/// ahead of time.
pub dead_letter: Option<&'a str>,
/// Number of retry attempts for jobs in this queue.
pub retry_limit: Option<usize>,
/// Time to wait before a retry attempt.
pub retry_delay: Option<Duration>,
/// Whether to use a backoff between retry attempts.
pub retry_backoff: Option<bool>,
/// Time to wait before expiring this job.
///
/// Should be between 1 second and 24 hours, or simply unset (default).
pub expire_in: Option<Duration>,
/// For how long this job should be retained in the system.
///
/// Should be greater than or eqaul to 1 second, or simply unset (default).
pub retain_for: Option<Duration>,
/// Whether the queue should form a dedicated partition.
pub partition: Option<bool>,
}
impl<'a> Queue<'a> {
/// Returns a builder for [`Queue`]
pub fn builder() -> QueueBuilder<'a> {
QueueBuilder::default()
}
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub(crate) struct QueueOptions<'a> {
/// Policy to apply to this queue.
pub policy: &'a QueuePolicy,
/// Number of retry attempts for jobs in this queue.
#[serde(skip_serializing_if = "Option::is_none")]
pub retry_limit: Option<usize>,
/// Time to wait before a retry attempt.
#[serde(
serialize_with = "utils::serialize_duration_as_secs",
skip_serializing_if = "Option::is_none"
)]
pub retry_delay: Option<Duration>,
/// Whether to use a backoff between retry attempts.
#[serde(skip_serializing_if = "Option::is_none")]
pub retry_backoff: Option<bool>,
/// Name of the dead letter queue.
///
/// Note that the dead letter queue itself should be created
/// ahead of time.
#[serde(skip_serializing_if = "Option::is_none")]
pub dead_letter: Option<&'a str>,
/// Time to wait before expiring this job.
///
/// Should be between 1 second and 24 hours, or simply unset (default).
#[serde(
serialize_with = "utils::serialize_duration_as_secs",
rename = "expireInSeconds",
skip_serializing_if = "Option::is_none"
)]
pub expire_in: Option<Duration>,
/// For how long this job should be retained in the system.
///
/// Should be greater than or eqaul to 1 second, or simply unset (default).
#[serde(
serialize_with = "utils::serialize_duration_as_secs",
rename = "retentionSeconds",
skip_serializing_if = "Option::is_none"
)]
pub retain_for: Option<Duration>,
#[serde(skip_serializing_if = "Option::is_none")]
/// Whether the queue should form a dedicated partition.
pub partition: Option<bool>,
}
impl<'a> Queue<'a> {
pub(crate) fn opts(&'a self) -> QueueOptions<'a> {
QueueOptions {
policy: &self.policy,
dead_letter: self.dead_letter,
retry_limit: self.retry_limit,
retry_delay: self.retry_delay,
retry_backoff: self.retry_backoff,
expire_in: self.expire_in,
retain_for: self.retain_for,
partition: self.partition,
}
}
}
/// Convenience builder for [`Queue`]
#[derive(Debug, Clone, Default)]
pub struct QueueBuilder<'a> {
name: &'a str,
policy: QueuePolicy,
dead_letter: Option<&'a str>,
retry_limit: Option<usize>,
retry_delay: Option<Duration>,
retry_backoff: Option<bool>,
expire_in: Option<Duration>,
retain_for: Option<Duration>,
partition: Option<bool>,
}
impl<'a> QueueBuilder<'a> {
/// Queue name.
pub fn name(mut self, val: &'a str) -> Self {
self.name = val;
self
}
/// Policy to apply to this queue.
pub fn policy(mut self, val: QueuePolicy) -> Self {
self.policy = val;
self
}
/// Name of the dead letter queue.
///
/// Note that the dead letter queue itself should be created
/// ahead of time.
pub fn dead_letter(mut self, val: &'a str) -> Self {
self.dead_letter = Some(val);
self
}
/// Number of retry attempts for jobs in this queue.
pub fn retry_limit(mut self, val: usize) -> Self {
self.retry_limit = Some(val);
self
}
/// Time to wait before a retry attempt.
pub fn retry_delay(mut self, val: Duration) -> Self {
self.retry_delay = Some(val);
self
}
/// Whether to use a backoff between retry attempts.
pub fn retry_backoff(mut self, val: bool) -> Self {
self.retry_backoff = Some(val);
self
}
/// Time to wait before expiring this job.
///
/// Should be between 1 second and 24 hours, or simply unset (default)
pub fn expire_in(mut self, val: Duration) -> Self {
self.expire_in = Some(val);
self
}
/// For how long this job should be retained in the system.
///
/// Should be greater than or equal to 1 second, or simply unset (default)
pub fn retain_for(mut self, val: Duration) -> Self {
self.retain_for = Some(val);
self
}
/// Whether the queue should form a dedicated partition.
pub fn partition(mut self, val: bool) -> Self {
self.partition = Some(val);
self
}
/// Terminal method for the builder returing [`Queue`]
pub fn build(self) -> Queue<'a> {
Queue {
name: self.name,
policy: self.policy,
dead_letter: self.dead_letter,
retry_limit: self.retry_limit,
retry_delay: self.retry_delay,
retry_backoff: self.retry_backoff,
expire_in: self.expire_in,
retain_for: self.retain_for,
partition: self.partition,
}
}
}
/// Job queue info.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct QueueDetails {
/// Queue name.
pub name: String,
/// Queue policy.
pub policy: QueuePolicy,
/// Number of retry attempts.
pub retry_limit: usize,
/// Time to wait before a retry attempt.
pub retry_delay: Duration,
/// Whether to use a backoff between retry attempts.
pub retry_backoff: bool,
/// Time to wait before expiring this job.
pub expire_in: Duration,
/// For how long this job should be retained in the system.
pub retain_for: Duration,
/// For how long this job should be retained in the system after it is _completed_.
pub delete_after: Duration,
/// Name of the dead letter queue.
pub dead_letter: Option<String>,
/// Date and time when this queue was created.
pub created_at: DateTime<Utc>,
/// Date and time when this queue was updated.
pub updated_at: DateTime<Utc>,
}
impl FromRow<'_, PgRow> for QueueDetails {
fn from_row(row: &PgRow) -> sqlx::Result<Self> {
let name: String = row.try_get("name")?;
let policy: QueuePolicy = row.try_get("policy").and_then(|v: String| {
QueuePolicy::try_from(v).map_err(|e| sqlx::Error::ColumnDecode {
index: "policy".to_string(),
source: e.into(),
})
})?;
let retry_limit: usize = row.try_get("retry_limit").and_then(|v: i32| match v {
v if v >= 0 => Ok(v as usize),
v => Err(sqlx::Error::ColumnDecode {
index: "retry_limit".to_string(),
source: format!("'retry_limit' should be non-negative, got {v}").into(),
}),
})?;
let retry_delay: Duration = row.try_get_duration("retry_delay")?;
let retry_backoff: bool = row.try_get("retry_backoff")?;
let expire_in: Duration = row.try_get_duration("expire_seconds")?;
let retain_for: Duration = row.try_get_duration("retention_seconds")?;
let delete_after: Duration = row.try_get_duration("deletion_seconds")?;
let dead_letter: Option<String> = row.try_get("dead_letter")?;
let created_at: DateTime<Utc> = row.try_get("created_at")?;
let updated_at: DateTime<Utc> = row.try_get("updated_at")?;
Ok(QueueDetails {
name,
policy,
retry_limit,
retry_delay,
retry_backoff,
expire_in,
retain_for,
delete_after,
dead_letter,
created_at,
updated_at,
})
}
}