navree-queue 1.4.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
//! Job batches: [`Batch`] (what to dispatch) and [`JobBatch`] (a dispatched
//! batch's progress, as stored in `job_batches`).

use chrono::{DateTime, Utc};
use navree_contracts::{Job, JobEnvelope};
use navree_db::Uow;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::record::from_millis;
use crate::{Queue, QueueError};

/// A group of jobs dispatched together and followed as one: how many are
/// still pending, how many failed, whether the group was cancelled, plus
/// callbacks for its outcome. Dispatch it with [`dispatch`](Batch::dispatch)
/// or [`dispatch_in`](Batch::dispatch_in) (or the same methods on
/// [`Queue`]: [`dispatch_batch`](crate::Queue::dispatch_batch),
/// [`dispatch_batch_in`](crate::Queue::dispatch_batch_in)).
///
/// ```no_run
/// # use navree_contracts::{BoxError, Job, JobContext};
/// # use serde::{Deserialize, Serialize};
/// # #[derive(Serialize, Deserialize)] struct ImportChunk { rows: Vec<u32> }
/// # impl Job for ImportChunk { const NAME: &'static str = "import.chunk"; async fn handle(self, _: JobContext) -> Result<(), BoxError> { Ok(()) } }
/// # #[derive(Serialize, Deserialize)] struct FinishImport { id: u32 }
/// # impl Job for FinishImport { const NAME: &'static str = "import.finish"; async fn handle(self, _: JobContext) -> Result<(), BoxError> { Ok(()) } }
/// # #[derive(Serialize, Deserialize)] struct ReportImportFailure { id: u32 }
/// # impl Job for ReportImportFailure { const NAME: &'static str = "import.failed"; async fn handle(self, _: JobContext) -> Result<(), BoxError> { Ok(()) } }
/// use navree_queue::{Batch, Queue};
///
/// # async fn example(queue: &Queue, uow: &navree_db::Uow) -> Result<(), navree_queue::QueueError> {
/// let chunks = vec![vec![1, 2], vec![3, 4]];
/// let batch = Batch::new("recipients.import")
///     .add(chunks.into_iter().map(|rows| ImportChunk { rows }))
///     .then(FinishImport { id: 7 })
///     .catch(ReportImportFailure { id: 7 })
///     .dispatch_in(queue, uow)
///     .await?;
/// println!("batch {} has {} jobs", batch.id, batch.total_jobs);
/// # Ok(())
/// # }
/// ```
///
/// - The batch **finishes** when each of its jobs has succeeded, failed
///   (used its attempts) or been skipped.
/// - **`then`** runs once it finishes with no failed job and without being
///   cancelled; **`catch`** runs as soon as the first job fails;
///   **`finally`** runs once it finishes, whatever happened.
/// - The first failed job **cancels** the batch, unless it
///   [`allow_failures`](Batch::allow_failures): the jobs that have not
///   started yet are then skipped. [`Queue::cancel_batch`](crate::Queue::cancel_batch)
///   cancels it on demand; running jobs see it through
///   `ctx.is_cancelled()` and `ctx.progress(..)`.
/// - Jobs in a batch are never unique (`unique_for_secs` is ignored), so
///   every job counts.
#[derive(Clone, Debug)]
pub struct Batch {
    pub(crate) name: String,
    pub(crate) jobs: Vec<JobEnvelope>,
    pub(crate) callbacks: Callbacks,
    pub(crate) allow_failures: bool,
    pub(crate) operation_id: Option<Uuid>,
    /// The first job that could not be serialized; dispatching fails with it.
    pub(crate) error: Option<String>,
    /// The queue every job goes to instead of its own (`on_queue`).
    pub(crate) queue: Option<String>,
}

/// A batch's callback jobs.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub(crate) struct Callbacks {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub(crate) then: Option<JobEnvelope>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub(crate) catch: Option<JobEnvelope>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub(crate) finally: Option<JobEnvelope>,
}

impl Batch {
    /// An empty batch named `name` (`recipients.import`): what logs,
    /// `queue:batches` and [`JobBatch::name`] show.
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            jobs: Vec::new(),
            callbacks: Callbacks::default(),
            allow_failures: false,
            operation_id: None,
            error: None,
            queue: None,
        }
    }

    /// Puts every job of the batch, and its callbacks, on `queue` instead
    /// of the queue each job type names (`JobOptions::queue`): jobs added
    /// before and after the call.
    pub fn on_queue(mut self, queue: impl Into<String>) -> Self {
        let queue = queue.into();
        let callbacks = [
            &mut self.callbacks.then,
            &mut self.callbacks.catch,
            &mut self.callbacks.finally,
        ];
        for envelope in self.jobs.iter_mut().chain(callbacks.into_iter().flatten()) {
            envelope.options.queue = queue.clone();
        }
        self.queue = Some(queue);
        self
    }

    /// Adds `jobs` to the batch.
    // The builder's verb (`Batch::new(..).add(jobs)`); it takes jobs, not
    // another batch, so `std::ops::Add` does not fit.
    #[allow(clippy::should_implement_trait)]
    pub fn add<J: Job>(mut self, jobs: impl IntoIterator<Item = J>) -> Self {
        for job in jobs {
            if let Some(envelope) = self.envelope(&job) {
                self.jobs.push(envelope);
            }
        }
        self
    }

    /// Adds one job to the batch.
    pub fn push<J: Job>(self, job: J) -> Self {
        self.add([job])
    }

    /// The job to dispatch once the batch finishes with no failed job and
    /// without being cancelled.
    pub fn then<J: Job>(mut self, job: J) -> Self {
        self.callbacks.then = self.envelope(&job);
        self
    }

    /// The job to dispatch as soon as one of the batch's jobs fails.
    pub fn catch<J: Job>(mut self, job: J) -> Self {
        self.callbacks.catch = self.envelope(&job);
        self
    }

    /// The job to dispatch once the batch finishes, whatever happened.
    pub fn finally<J: Job>(mut self, job: J) -> Self {
        self.callbacks.finally = self.envelope(&job);
        self
    }

    /// Keeps running the other jobs when one fails (by default the first
    /// failure cancels the batch). `then` still runs only when no job
    /// failed.
    pub fn allow_failures(mut self) -> Self {
        self.allow_failures = true;
        self
    }

    /// Ties the batch to the operation `id` (see
    /// [`Operations`](crate::Operations)): its progress becomes the
    /// operation's, its jobs and callbacks report into it, and the
    /// operation finishes when the batch and its callbacks have run.
    /// [`Operations::dispatch_batch`](crate::Operations::dispatch_batch)
    /// sets it for you.
    pub fn operation(mut self, id: Uuid) -> Self {
        self.operation_id = Some(id);
        self
    }

    /// How many jobs the batch has.
    pub fn len(&self) -> usize {
        self.jobs.len()
    }

    /// Whether the batch has no jobs (it finishes as soon as it is
    /// dispatched).
    pub fn is_empty(&self) -> bool {
        self.jobs.is_empty()
    }

    /// The batch's name.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Dispatches the batch on `queue`: the fluent form of
    /// [`Queue::dispatch_batch`] (it joins the current unit of work the
    /// same way).
    pub async fn dispatch(self, queue: &Queue) -> Result<JobBatch, QueueError> {
        queue.dispatch_batch(self).await
    }

    /// Dispatches the batch on `queue` as part of `uow`: the fluent form of
    /// [`Queue::dispatch_batch_in`]. Nothing is stored if `uow` rolls back.
    pub async fn dispatch_in(self, queue: &Queue, uow: &Uow) -> Result<JobBatch, QueueError> {
        queue.dispatch_batch_in(uow, self).await
    }

    fn envelope<J: Job>(&mut self, job: &J) -> Option<JobEnvelope> {
        match serde_json::to_value(job) {
            Ok(payload) => {
                let mut options = J::options();
                if let Some(queue) = &self.queue {
                    options.queue = queue.clone();
                }
                Some(JobEnvelope {
                    job_type: J::NAME.to_owned(),
                    payload,
                    options,
                    delay_secs: None,
                    unique_key: None,
                })
            }
            Err(error) => {
                self.error
                    .get_or_insert_with(|| format!("job `{}`: {error}", J::NAME));
                None
            }
        }
    }

    /// The serialization error of the first job that had one.
    pub(crate) fn check(&self) -> Result<(), QueueError> {
        match &self.error {
            Some(error) => Err(QueueError::Payload(serde::de::Error::custom(error))),
            None => Ok(()),
        }
    }
}

/// A dispatched batch, as stored in `job_batches`: what
/// [`Queue::find_batch`](crate::Queue::find_batch) and
/// [`Queue::batches`](crate::Queue::batches) return.
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct JobBatch {
    /// The batch id.
    pub id: String,
    /// The name given to [`Batch::new`].
    pub name: String,
    /// How many jobs the batch has.
    pub total_jobs: u64,
    /// How many have not finished yet.
    pub pending_jobs: u64,
    /// How many failed (used their attempts).
    pub failed_jobs: u64,
    /// The ids of the failed jobs (see `queue:failed`).
    pub failed_job_ids: Vec<String>,
    /// Whether a failed job leaves the others running.
    pub allow_failures: bool,
    /// The operation the batch does the work of.
    pub operation_id: Option<Uuid>,
    /// When it was dispatched.
    pub created_at: DateTime<Utc>,
    /// When it was cancelled (by a failure or on demand).
    pub cancelled_at: Option<DateTime<Utc>>,
    /// When its last job finished.
    pub finished_at: Option<DateTime<Utc>>,
}

impl JobBatch {
    /// Jobs that have finished (succeeded, failed or skipped).
    pub fn processed_jobs(&self) -> u64 {
        self.total_jobs.saturating_sub(self.pending_jobs)
    }

    /// Finished jobs as a whole percentage of all jobs (100 for an empty
    /// batch).
    pub fn progress(&self) -> u8 {
        if self.total_jobs == 0 {
            return 100;
        }
        let percent = u128::from(self.processed_jobs()) * 100 / u128::from(self.total_jobs);
        u8::try_from(percent).unwrap_or(100)
    }

    /// Whether every job has finished.
    pub fn finished(&self) -> bool {
        self.finished_at.is_some()
    }

    /// Whether the batch was cancelled.
    pub fn cancelled(&self) -> bool {
        self.cancelled_at.is_some()
    }

    /// Whether any job failed.
    pub fn has_failures(&self) -> bool {
        self.failed_jobs > 0
    }
}

/// How one of a batch's jobs ended.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Finish {
    /// It ran and succeeded.
    Succeeded,
    /// It did not run (its batch or operation was cancelled) or stopped
    /// because of that.
    Skipped,
    /// It used its attempts.
    Failed,
}

/// A batch row as the drivers keep it.
#[derive(Clone, Debug)]
pub(crate) struct BatchRow {
    pub(crate) id: String,
    pub(crate) name: String,
    pub(crate) total_jobs: u64,
    pub(crate) pending_jobs: u64,
    pub(crate) failed_jobs: u64,
    pub(crate) failed_job_ids: Vec<String>,
    pub(crate) callbacks: Callbacks,
    /// Callbacks dispatched and not finished yet.
    pub(crate) callbacks_pending: u64,
    /// Callbacks that failed.
    pub(crate) callbacks_failed: u64,
    pub(crate) allow_failures: bool,
    pub(crate) operation_id: Option<String>,
    /// Unix milliseconds.
    pub(crate) created_at: i64,
    pub(crate) cancelled_at: Option<i64>,
    pub(crate) finished_at: Option<i64>,
}

/// What finishing a reserved batch job did.
#[derive(Clone, Debug)]
pub(crate) enum Finished {
    /// The worker no longer held the reservation: nothing changed.
    Lost,
    /// The job is gone (into the failed jobs when it failed); the batch
    /// step, when its batch still exists.
    Done(Option<Box<BatchStep>>),
}

/// What finishing a batch job or callback changed.
#[derive(Clone, Debug)]
pub(crate) struct BatchStep {
    /// The batch afterwards.
    pub(crate) batch: BatchRow,
    /// The batch and every callback it dispatched have now all finished
    /// (true for exactly one step of a batch).
    pub(crate) settled_now: bool,
}

impl BatchRow {
    /// A new batch row with `total_jobs` jobs, created at `now`.
    pub(crate) fn new(id: String, batch: &Batch, now: i64) -> Self {
        let total = batch.jobs.len() as u64;
        Self {
            id,
            name: batch.name.clone(),
            total_jobs: total,
            pending_jobs: total,
            failed_jobs: 0,
            failed_job_ids: Vec::new(),
            callbacks: batch.callbacks.clone(),
            callbacks_pending: 0,
            callbacks_failed: 0,
            allow_failures: batch.allow_failures,
            operation_id: batch.operation_id.map(|id| id.to_string()),
            created_at: now,
            cancelled_at: None,
            finished_at: None,
        }
    }

    /// For a batch with no jobs: it finishes as it is created. Returns the
    /// callbacks due.
    pub(crate) fn start_empty(&mut self, now: i64) -> Vec<JobEnvelope> {
        if self.total_jobs == 0 {
            self.finish(now)
        } else {
            Vec::new()
        }
    }

    /// Records that job `job_id` ended with `finish` at `now`; returns the
    /// callbacks due now (counted as pending).
    pub(crate) fn job_finished(
        &mut self,
        job_id: &str,
        finish: Finish,
        now: i64,
    ) -> Vec<JobEnvelope> {
        if self.pending_jobs == 0 {
            return Vec::new();
        }
        self.pending_jobs -= 1;
        let mut due = Vec::new();
        if finish == Finish::Failed {
            self.failed_jobs += 1;
            self.failed_job_ids.push(job_id.to_owned());
            if self.failed_jobs == 1
                && let Some(catch) = &self.callbacks.catch
            {
                due.push(catch.clone());
            }
            if !self.allow_failures && self.cancelled_at.is_none() {
                self.cancelled_at = Some(now);
            }
        }
        self.callbacks_pending += due.len() as u64;
        if self.pending_jobs == 0 {
            due.extend(self.finish(now));
        }
        due
    }

    /// Records that one of the batch's callbacks ended.
    pub(crate) fn callback_finished(&mut self, failed: bool) {
        self.callbacks_pending = self.callbacks_pending.saturating_sub(1);
        if failed {
            self.callbacks_failed += 1;
        }
    }

    /// Cancels the batch at `now` unless it finished or was cancelled;
    /// whether it changed.
    pub(crate) fn cancel(&mut self, now: i64) -> bool {
        if self.cancelled_at.is_some() || self.finished_at.is_some() {
            return false;
        }
        self.cancelled_at = Some(now);
        true
    }

    /// Whether the batch and the callbacks it dispatched have all finished.
    pub(crate) fn settled(&self) -> bool {
        self.finished_at.is_some() && self.callbacks_pending == 0
    }

    /// Marks the batch finished; returns `then` (when it applies) and
    /// `finally`, counted as pending callbacks.
    fn finish(&mut self, now: i64) -> Vec<JobEnvelope> {
        let mut due = Vec::new();
        if self.finished_at.is_none() {
            self.finished_at = Some(now);
            if self.failed_jobs == 0
                && self.cancelled_at.is_none()
                && let Some(then) = &self.callbacks.then
            {
                due.push(then.clone());
            }
            if let Some(finally) = &self.callbacks.finally {
                due.push(finally.clone());
            }
        }
        self.callbacks_pending += due.len() as u64;
        due
    }

    /// The public view.
    pub(crate) fn to_batch(&self) -> JobBatch {
        JobBatch {
            id: self.id.clone(),
            name: self.name.clone(),
            total_jobs: self.total_jobs,
            pending_jobs: self.pending_jobs,
            failed_jobs: self.failed_jobs,
            failed_job_ids: self.failed_job_ids.clone(),
            allow_failures: self.allow_failures,
            operation_id: self
                .operation_id
                .as_deref()
                .and_then(|id| Uuid::parse_str(id).ok()),
            created_at: from_millis(self.created_at),
            cancelled_at: self.cancelled_at.map(from_millis),
            finished_at: self.finished_at.map(from_millis),
        }
    }
}

#[cfg(test)]
mod tests {
    use navree_contracts::{BoxError, JobContext};

    use super::*;

    #[derive(Serialize, Deserialize)]
    struct Ping;

    impl Job for Ping {
        const NAME: &'static str = "test.ping";

        async fn handle(self, _ctx: JobContext) -> Result<(), BoxError> {
            Ok(())
        }
    }

    #[test]
    fn on_queue_moves_every_job_and_callback() {
        let batch = Batch::new("pings")
            .push(Ping)
            .then(Ping)
            .on_queue("media")
            .push(Ping)
            .finally(Ping);
        assert_eq!(batch.len(), 2);
        assert!(batch.jobs.iter().all(|job| job.options.queue == "media"));
        let callbacks = [&batch.callbacks.then, &batch.callbacks.finally];
        assert!(
            callbacks
                .iter()
                .all(|job| job.as_ref().is_some_and(|job| job.options.queue == "media"))
        );
        assert_eq!(
            Batch::new("plain").push(Ping).jobs[0].options.queue,
            "default"
        );
    }
}