Skip to main content

tablo_jobs/
model.rs

1//! The queue's tables: a [`QueuedJob`] per unit of work and a [`JobSchedule`] per recurring job.
2
3use jiff::Timestamp;
4use uuid::Uuid;
5
6/// One unit of background work, written by [`enqueue`](crate::enqueue) and run by a
7/// [`Worker`](crate::Worker).
8///
9/// It is a [`PanelModel`](tablo_core::PanelModel), so an app can list the queue through a
10/// resource of its own, and give a [`Dead`](JobState::Dead) job its attempts back by setting its
11/// state to [`Queued`](JobState::Queued), `attempts` to 0 and `run_at` to now.
12#[derive(Debug, Clone, toasty::Model, tablo_core::PanelModel)]
13#[table = "tablo_jobs"]
14pub struct QueuedJob {
15    #[key]
16    pub id: Uuid,
17    /// The [`Job::KIND`](crate::Job::KIND) that runs it.
18    #[index]
19    pub kind: String,
20    /// The job's fields, as JSON.
21    #[column(type = text)]
22    #[columns(skip)]
23    pub payload: String,
24    pub state: JobState,
25    /// The [`Job::lane`](crate::Job::lane) it runs in: one job of a lane runs at a time.
26    #[index]
27    pub lane: Option<String>,
28    /// The lane while the job runs, and `None` otherwise. Unique, so the database refuses a
29    /// second running job in one lane, whichever worker claims it.
30    #[unique]
31    #[columns(skip)]
32    pub running_lane: Option<String>,
33    /// The kind and the [`dedupe`](crate::Enqueue::dedupe) key, or the id. While the job waits
34    /// for its first run, a second enqueue with the same key adds nothing. A claim sets it to the
35    /// id, so work arriving during a run, or during the backoff after one, queues behind it.
36    #[unique]
37    #[columns(skip)]
38    pub dedupe_key: String,
39    pub attempts: u64,
40    pub max_attempts: u64,
41    /// Not before this instant.
42    #[index]
43    pub run_at: Timestamp,
44    /// When a running job's worker gives it up, alive or not.
45    pub locked_until: Option<Timestamp>,
46    /// The last failure, for whoever reads a dead job.
47    #[column(type = text)]
48    pub last_error: Option<String>,
49    pub created_at: Timestamp,
50    pub finished_at: Option<Timestamp>,
51    /// Makes a claim a compare-and-swap: of two workers, the second fails its update.
52    #[version]
53    pub version: u64,
54}
55
56/// Where a [`QueuedJob`] stands.
57#[derive(Debug, Clone, Copy, PartialEq, Eq, toasty::Embed, tablo_core::Options)]
58pub enum JobState {
59    /// Waiting for its `run_at`, or for its lane.
60    #[option(color = "gray")]
61    Queued,
62    /// Held by a worker until its `locked_until`.
63    #[option(color = "info")]
64    Running,
65    #[option(color = "success")]
66    Succeeded,
67    /// Out of attempts: it runs again only when someone queues it again.
68    #[option(color = "danger")]
69    Dead,
70}
71
72/// When a recurring job next falls due: one row per schedule, shared by every worker.
73#[derive(Debug, Clone, toasty::Model)]
74#[table = "tablo_job_schedules"]
75pub struct JobSchedule {
76    /// The [`Job::KIND`](crate::Job::KIND) it enqueues.
77    #[key]
78    pub kind: String,
79    pub next_run_at: Timestamp,
80    #[version]
81    pub version: u64,
82}