Skip to main content

Crate tablo_jobs

Crate tablo_jobs 

Source
Expand description

A durable job queue for Tablo apps, in the app’s own database, and the queued auth mail built on it.

A job enqueued through a write’s transaction commits with that write or not at all, so “the invitation exists” and “its mail will go out” cannot disagree. A Worker runs the queue against a panel’s context, and any number of workers can share it. It is built on Tablo’s public API alone (ADR-0041).

use serde::{Deserialize, Serialize};
use tablo_jobs::{Job, Worker, enqueue};
use topcoat::context::Cx;

#[derive(Serialize, Deserialize)]
struct SendInvitation {
    invitation: u64,
}

impl Job for SendInvitation {
    const KIND: &'static str = "send-invitation";

    async fn run(self, cx: &Cx) -> topcoat::Result<()> {
        // Load the invitation, and mail it unless it is gone.
        Ok(())
    }
}

// Inside the transaction that writes the invitation:
enqueue(&SendInvitation { invitation: id }).exec(tx).await?;
// In the worker process, or beside the server:
Worker::new(panel)?
    .register::<SendInvitation>()
    .drain()
    .await?;

The Db registers QueuedJob and JobSchedule: list them in the app’s models!, as tablo::models!(Post, tablo::jobs::QueuedJob, tablo::jobs::JobSchedule).

Structs§

AuthMailQueue
Queues the letters a panel’s auth sends, for a Worker with auth_mail to send.
Enqueue
A job on its way to the queue, from enqueue: set its delay or its dedupe key, then exec it.
JobSchedule
When a recurring job next falls due: one row per schedule, shared by every worker.
QueuedJob
One unit of background work, written by enqueue and run by a Worker.
Worker
Runs a panel’s queued jobs: claims the due ones, runs each with a timeout against the panel’s context, and retries a failure with backoff.

Enums§

JobState
Where a QueuedJob stands.

Traits§

Job
A kind of background work, whose fields are its payload.

Functions§

enqueue
Queues job, once exec writes it.