navree-queue 1.5.0

Navree QueueModule: database and memory job queues, workers, retries with backoff, unique and delayed jobs, job batches and operations with progress.
Documentation
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//! The records a [`QueueStore`](crate::QueueStore) stores and returns:
//! [`NewJob`], [`ReservedJob`] and [`FailedJob`].

use chrono::{DateTime, Utc};
use navree_contracts::JobOptions;
use serde::{Deserialize, Serialize};
use serde_json::Value;

/// A job that used up its attempts (or could not run at all), as kept in
/// `failed_jobs` until it is retried or flushed.
///
/// It is also the navree-orm model of the database driver's `failed_jobs`
/// table ([`FAILED_JOBS_TABLE`](crate::FAILED_JOBS_TABLE)), which the
/// driver writes and reads through the ORM: the payload and options are
/// stored as JSON text, the failure time as Unix milliseconds, and code
/// queries them with typed columns (`FailedJob::query().where_(FailedJob::QUEUE.eq("mail"))`).
/// Other drivers keep failed jobs their own way.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, navree_orm::Model)]
#[model(table = "failed_jobs")]
pub struct FailedJob {
    /// The job id (the id `dispatch` returned).
    pub id: String,
    /// The queue it was on.
    pub queue: String,
    /// The job type ([`Job::NAME`](navree_contracts::Job::NAME)).
    pub job_type: String,
    /// The job value as JSON.
    #[model(cast = navree_orm::cast::JsonText)]
    pub payload: Value,
    /// The options it was dispatched with.
    #[model(cast = navree_orm::cast::JsonText)]
    pub options: JobOptions,
    /// The error of the last attempt.
    pub error: String,
    /// How many attempts ran.
    pub attempts: u32,
    /// When it failed.
    #[model(cast = "unix_timestamp_ms")]
    pub failed_at: DateTime<Utc>,
}

/// A failed job without its payload, as an overview lists it.
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct FailedJobBrief {
    /// The job id.
    pub id: String,
    /// The queue it was on.
    pub queue: String,
    /// The job type.
    pub job_type: String,
    /// The error of the last attempt.
    pub error: String,
    /// How many attempts ran.
    pub attempts: u32,
    /// How many it could have.
    pub max_attempts: u32,
    /// When it failed.
    pub failed_at: DateTime<Utc>,
}

impl From<&FailedJob> for FailedJobBrief {
    fn from(job: &FailedJob) -> FailedJobBrief {
        FailedJobBrief {
            id: job.id.clone(),
            queue: job.queue.clone(),
            job_type: job.job_type.clone(),
            error: job.error.clone(),
            attempts: job.attempts,
            max_attempts: job.options.max_attempts,
            failed_at: job.failed_at,
        }
    }
}

/// The failed jobs in figures, and the newest of them: what a page polled every
/// few seconds reads, however many have failed ([`Queue::failed_summary`](crate::Queue::failed_summary)).
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize)]
pub struct FailedSummary {
    /// Every failed job.
    pub total: u64,
    /// The ones that failed at or after the time asked for.
    pub since: u64,
    /// The newest, newest first.
    pub newest: Vec<FailedJobBrief>,
}

impl FailedSummary {
    /// The summary of `failed` (newest first).
    pub(crate) fn of(failed: &[FailedJob], since: DateTime<Utc>, limit: usize) -> FailedSummary {
        FailedSummary {
            total: failed.len() as u64,
            since: failed.iter().filter(|job| job.failed_at >= since).count() as u64,
            newest: failed.iter().take(limit).map(Into::into).collect(),
        }
    }
}

/// Where a job that is not finished yet stands ([`PendingJob::state`]).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum PendingState {
    /// Available now and waiting for a worker.
    Waiting,
    /// Available later: dispatched with a delay, or released for a retry
    /// after its backoff.
    Delayed,
    /// Reserved by a worker (a crashed worker's reservation counts until
    /// another worker takes the job over).
    Running,
}

/// A job that is not finished yet, as [`Queue::pending_jobs`](crate::Queue::pending_jobs)
/// lists it.
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct PendingJob {
    /// The job id.
    pub id: String,
    /// The queue it is on.
    pub queue: String,
    /// The job type ([`Job::NAME`](navree_contracts::Job::NAME)).
    pub job_type: String,
    /// The job value as JSON.
    pub payload: Value,
    /// The options it was dispatched with.
    pub options: JobOptions,
    /// Attempts started so far (a running job's current one included).
    pub attempts: u32,
    /// Waiting, delayed or running.
    pub state: PendingState,
    /// When it is (or was) available to workers.
    pub available_at: DateTime<Utc>,
    /// When a worker reserved it, while it runs.
    pub reserved_at: Option<DateTime<Utc>>,
    /// When it was queued (or last retried from the failed jobs).
    pub created_at: DateTime<Utc>,
}

/// Which pending jobs [`Queue::pending_jobs`](crate::Queue::pending_jobs)
/// lists.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct PendingQuery {
    /// Only jobs of this queue.
    pub queue: Option<String>,
    /// Only jobs whose type or queue contains this text (case-insensitive).
    pub search: Option<String>,
    /// At most this many (default [`PendingQuery::DEFAULT_LIMIT`]).
    pub limit: Option<u64>,
}

impl PendingQuery {
    /// How many jobs a query without a limit returns.
    pub const DEFAULT_LIMIT: u64 = 500;

    /// The limit to apply.
    pub(crate) fn limit(&self) -> u64 {
        self.limit.unwrap_or(Self::DEFAULT_LIMIT)
    }

    /// The lowercased search text, if any.
    pub(crate) fn needle(&self) -> Option<String> {
        self.search
            .as_deref()
            .map(str::trim)
            .filter(|s| !s.is_empty())
            .map(str::to_lowercase)
    }

    /// Whether a job of `queue` and `job_type` matches.
    pub(crate) fn matches(&self, queue: &str, job_type: &str) -> bool {
        if self.queue.as_deref().is_some_and(|q| q != queue) {
            return false;
        }
        match self.needle() {
            Some(needle) => {
                queue.to_lowercase().contains(&needle) || job_type.to_lowercase().contains(&needle)
            }
            None => true,
        }
    }
}

/// One queue's unfinished jobs at a moment, from [`Queue::status`](crate::Queue::status).
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct QueueStatus {
    /// The queue.
    pub queue: String,
    /// Jobs available now, waiting for a worker.
    pub waiting: u64,
    /// Jobs available later (delayed, or released for a retry).
    pub delayed: u64,
    /// Jobs reserved by a worker.
    pub running: u64,
    /// When the oldest waiting job became available (`None`: none waits).
    pub oldest_available_at: Option<DateTime<Utc>>,
    /// When the status was taken (the queue's clock).
    pub at: DateTime<Utc>,
}

impl QueueStatus {
    /// Every job that is not finished yet: waiting, delayed and running.
    pub fn pending(&self) -> u64 {
        self.waiting + self.delayed + self.running
    }

    /// How long the oldest waiting job has waited (zero when none waits).
    pub fn oldest_wait(&self) -> std::time::Duration {
        self.oldest_available_at
            .and_then(|since| (self.at - since).to_std().ok())
            .unwrap_or_default()
    }
}

/// Which failed jobs [`Queue::retry`](crate::Queue::retry) puts back on
/// their queues. Converts from a string: `"all"` is [`All`](RetryTarget::All),
/// anything else a job id.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum RetryTarget {
    /// Every failed job.
    All,
    /// The failed job with this id.
    Id(String),
}

impl From<&str> for RetryTarget {
    fn from(value: &str) -> Self {
        if value == "all" {
            RetryTarget::All
        } else {
            RetryTarget::Id(value.to_owned())
        }
    }
}

impl From<String> for RetryTarget {
    fn from(value: String) -> Self {
        RetryTarget::from(value.as_str())
    }
}

impl FailedJob {
    /// Reserved `job` as it fails at `now` (Unix milliseconds) with
    /// `error`, after its attempts so far.
    pub(crate) fn of(job: &ReservedJob, error: &str, now: i64) -> FailedJob {
        FailedJob {
            id: job.id.clone(),
            queue: job.queue.clone(),
            job_type: job.job_type.clone(),
            payload: job.payload.clone(),
            options: job.options.clone(),
            error: error.to_owned(),
            attempts: job.attempts,
            failed_at: from_millis(now),
        }
    }

    /// The job as it goes back on its queue at `now` (Unix milliseconds):
    /// same id, attempts start over, not unique.
    pub fn requeued(&self, now: i64) -> NewJob {
        NewJob {
            id: self.id.clone(),
            queue: self.queue.clone(),
            job_type: self.job_type.clone(),
            payload: self.payload.clone(),
            options: self.options.clone(),
            available_at: now,
            created_at: now,
            unique_key: None,
            unique_for_ms: None,
            trace_parent: None,
            membership: Membership::default(),
        }
    }
}

/// What a job belongs to: a batch (as one of its jobs or as one of its
/// callbacks) and an operation. The default belongs to neither.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct Membership {
    /// The batch the job belongs to.
    pub batch_id: Option<String>,
    /// Whether the job is a `then`, `catch` or `finally` callback of its
    /// batch rather than one of its jobs.
    pub callback: bool,
    /// The operation the job does the work of.
    pub operation_id: Option<String>,
}

impl Membership {
    /// One of the jobs of batch `batch_id`.
    pub(crate) fn batch_job(batch_id: &str, operation_id: Option<&str>) -> Self {
        Self {
            batch_id: Some(batch_id.to_owned()),
            callback: false,
            operation_id: operation_id.map(str::to_owned),
        }
    }

    /// A callback of batch `batch_id`.
    pub(crate) fn batch_callback(batch_id: &str, operation_id: Option<&str>) -> Self {
        Self {
            batch_id: Some(batch_id.to_owned()),
            callback: true,
            operation_id: operation_id.map(str::to_owned),
        }
    }

    /// One of a batch's jobs (not a callback).
    pub(crate) fn in_batch(&self) -> Option<&str> {
        match (&self.batch_id, self.callback) {
            (Some(batch), false) => Some(batch),
            _ => None,
        }
    }
}

/// A job ready to be stored: what [`QueueStore::insert`](crate::QueueStore::insert)
/// receives. Times are Unix milliseconds.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct NewJob {
    /// The job id (a UUID v7, so ids sort in dispatch order).
    pub id: String,
    /// The queue.
    pub queue: String,
    /// The job type ([`Job::NAME`](navree_contracts::Job::NAME)).
    pub job_type: String,
    /// The job value as JSON.
    pub payload: Value,
    /// The options it was dispatched with.
    pub options: JobOptions,
    /// When a worker may take it first.
    pub available_at: i64,
    /// When it was dispatched.
    pub created_at: i64,
    /// Set for unique jobs: a pending job with the same key (dispatched
    /// within [`unique_for_ms`](NewJob::unique_for_ms)) takes its place.
    pub unique_key: Option<String>,
    /// How long a pending unique job blocks its duplicates (`None`: while
    /// it is pending).
    pub unique_for_ms: Option<i64>,
    /// The W3C `traceparent` of the span that dispatched the job, so its
    /// attempts continue that trace.
    pub trace_parent: Option<String>,
    /// The batch and operation the job belongs to (none for a plain
    /// dispatch). A driver stores it with the job and hands it back on
    /// the [`ReservedJob`].
    #[serde(default)]
    pub membership: Membership,
}

impl NewJob {
    /// Whether a pending duplicate created at `created_at` (Unix
    /// milliseconds) still blocks this job.
    pub fn blocked_by(&self, created_at: i64) -> bool {
        self.unique_for_ms
            .is_none_or(|window| self.created_at < created_at.saturating_add(window))
    }

    /// How long after a worker reserved it the job counts as abandoned, in
    /// milliseconds: its `timeout_secs` (one day when 0) plus
    /// [`STALE_RESERVATION_GRACE_SECS`](crate::STALE_RESERVATION_GRACE_SECS).
    /// A store hands an abandoned reservation to the next worker.
    pub fn stale_after_ms(&self) -> i64 {
        stale_after_ms(&self.options)
    }
}

/// A job a worker reserved: what [`QueueStore::reserve`](crate::QueueStore::reserve)
/// returns.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ReservedJob {
    /// The job id.
    pub id: String,
    /// The queue.
    pub queue: String,
    /// The job type.
    pub job_type: String,
    /// The job value as JSON.
    pub payload: Value,
    /// The options it was dispatched with.
    pub options: JobOptions,
    /// How many attempts started, including the one that is starting.
    pub attempts: u32,
    /// The `traceparent` recorded at dispatch (see [`NewJob::trace_parent`]).
    pub trace_parent: Option<String>,
    /// The batch and operation the job belongs to (see
    /// [`NewJob::membership`]).
    #[serde(default)]
    pub membership: Membership,
}

/// How long a reservation of a job without a time limit (`timeout_secs`
/// 0) lasts before it counts as abandoned: one day.
pub(crate) const UNLIMITED_RESERVATION_SECS: u64 = 86_400;

/// How long after it was taken a reservation counts as abandoned, in
/// milliseconds: the job's timeout plus
/// [`STALE_RESERVATION_GRACE_SECS`](crate::STALE_RESERVATION_GRACE_SECS).
pub(crate) fn stale_after_ms(options: &JobOptions) -> i64 {
    let timeout = match options.timeout_secs {
        0 => UNLIMITED_RESERVATION_SECS,
        secs => secs,
    };
    let secs = timeout.saturating_add(crate::STALE_RESERVATION_GRACE_SECS);
    i64::try_from(secs.saturating_mul(1000)).unwrap_or(i64::MAX)
}

/// Unix milliseconds → `DateTime<Utc>` (the epoch for out-of-range values).
pub(crate) fn from_millis(ms: i64) -> DateTime<Utc> {
    DateTime::from_timestamp_millis(ms).unwrap_or_default()
}