Skip to main content

Module queue

Module queue 

Source
Expand description

The queue: work pushed now and run by a worker, with retries, a failed list and a lifetime per job.

A job is a serde struct that implements Job; a module registers it by type, and a payload names it. Pushing records the job; a worker claims it, runs it under its timeout, deletes it on success, releases it with its backoff on failure, and moves it to the failed list once its tries or its retry window are spent.

ⓘ
use laterite_core::queue::{Job, JobCx, JobError, QueueRegistry};

#[derive(Serialize, Deserialize)]
struct SendDigest { user_id: i64 }

#[async_trait]
impl Job for SendDigest {
    const NAME: &'static str = "acme.blog::send_digest";
    async fn run(&self, cx: &JobCx) -> Result<(), JobError> {
        digest::send(cx.db(), self.user_id).await?;
        Ok(())
    }
}

fn register(&self, registry: &mut Registry) {
    registry.add_job::<SendDigest>();
}

ctx.queue().push(&SendDigest { user_id: 7 }).await?;

Jobs live in a QueueDriver: the database by default, or Redis with the redis store. queue.mode = inline runs a job as it is pushed, for tests and for an application without a worker.

Modules§

migrations
The queue schema, as a portable one-file migration.

Structs§

DatabaseQueue
Jobs in the application’s database.
FailedJob
A job on the failed list.
JobCx
What a running job sees: the application’s services and its own attempt.
JobFailed
Announced when a job reaches the failed list.
JobMeta
What the worker reads from a payload before running it.
JobReg
A registered job: its name and how to run a payload of it. Built by JobReg::of and contributed through QueueRegistry::add_job.
Options
What Queue::new takes beside the driver and the jobs.
Queue
The queue handle. Cheap to clone: every clone is the same queue.
QueueCount
How many jobs a queue holds.
QueueModule
The module that owns the jobs and failed_jobs tables, listed among an application’s built-ins.
Queued
A job as it waits or runs.
Services
The services a job runs against.
WorkOptions
How one Worker runs.
Worker
One worker loop over a Queue.

Enums§

JobError
Why a run did not succeed.
Mode
How the queue runs.
Pushed
A pushed job’s fate.

Constants§

DEFAULT_QUEUE
The queue a job lands on when it names none.

Traits§

Job
Work to run later. The struct is the payload: whatever it carries is stored as JSON and read back by the worker.
QueueDriver
Where jobs wait. Every method is one storage operation; the worker’s rules live in Worker.
QueueRegistry
registry.add_job::<T>(): the typed way to contribute a job.

Functions§

from_config
The queue a deployment configured: the database by default, Redis for the redis store.
time_text