Skip to main content

tablo_jobs/
lib.rs

1//! A durable job queue for Tablo apps, in the app's own database, and the queued auth mail built
2//! on it.
3//!
4//! A job enqueued through a write's transaction commits with that write or not at all, so "the
5//! invitation exists" and "its mail will go out" cannot disagree. A [`Worker`] runs the queue
6//! against a panel's context, and any number of workers can share it. It is built on Tablo's
7//! public API alone (ADR-0041).
8//!
9//! ```rust,no_run
10//! use serde::{Deserialize, Serialize};
11//! use tablo_jobs::{Job, Worker, enqueue};
12//! use topcoat::context::Cx;
13//!
14//! #[derive(Serialize, Deserialize)]
15//! struct SendInvitation {
16//!     invitation: u64,
17//! }
18//!
19//! impl Job for SendInvitation {
20//!     const KIND: &'static str = "send-invitation";
21//!
22//!     async fn run(self, cx: &Cx) -> topcoat::Result<()> {
23//!         // Load the invitation, and mail it unless it is gone.
24//!         # let _ = cx;
25//!         Ok(())
26//!     }
27//! }
28//!
29//! # async fn invite(tx: &mut dyn toasty::Executor, id: u64) -> topcoat::Result<()> {
30//! // Inside the transaction that writes the invitation:
31//! enqueue(&SendInvitation { invitation: id }).exec(tx).await?;
32//! # Ok(())
33//! # }
34//! # async fn work(panel: &tablo_core::PanelHandle) -> topcoat::Result<()> {
35//! // In the worker process, or beside the server:
36//! Worker::new(panel)?
37//!     .register::<SendInvitation>()
38//!     .drain()
39//!     .await?;
40//! # Ok(())
41//! # }
42//! ```
43//!
44//! The `Db` registers [`QueuedJob`] and [`JobSchedule`]: list them in the app's
45//! [`models!`](tablo_core::models!), as
46//! `tablo::models!(Post, tablo::jobs::QueuedJob, tablo::jobs::JobSchedule)`.
47
48mod job;
49mod mail;
50mod model;
51mod queue;
52mod schedule;
53mod worker;
54
55pub use self::{
56    job::Job,
57    mail::AuthMailQueue,
58    model::{JobSchedule, JobState, QueuedJob},
59    queue::{Enqueue, enqueue},
60    worker::Worker,
61};