Skip to main content

Module queue

Module queue 

Source
Expand description

Background jobs stored in SQLite, with retries and a failed-jobs table.

use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
struct SendReceipt { order_id: i64 }

impl Job for SendReceipt {
    const NAME: &'static str = "send-receipt";
    const MAX_ATTEMPTS: u32 = 5;

    async fn handle(self, ctx: JobContext) -> Result {
        let order = Order::find_or_404(&ctx.state.db, self.order_id).await?;
        // … send it
        Ok(())
    }
}

App::new().job::<SendReceipt>();            // register the handler
state.dispatch(SendReceipt { order_id }).await?;   // in a handler

serve runs workers in the same process (QUEUE_WORKERS, default 2; 0 to turn off); my-app queue:work runs them on their own, and queue:work --queue high,default drains high before default.

More than one job at a time:

// One after another; a failure stops the rest.
state.queue.chain()
    .then(Import { file: "a.csv".into() })
    .then(Notify { user_id: 7 })
    .dispatch()
    .await?;

// Side by side, with progress and follow-up jobs.
let batch = state.queue.batch("import-october")
    .push(Import { file: "a.csv".into() })
    .push(Import { file: "b.csv".into() })
    .then(Notify { user_id: 7 })   // all succeeded
    .catch(Notify { user_id: 1 })  // the first failure (which cancels the rest)
    .dispatch()
    .await?;
let status = state.queue.batch_status(batch).await?; // total, pending, failed, progress()

Structs§

Batch
Jobs that run side by side and are tracked together; from Queue::batch. The first job that fails for good cancels the batch (jobs that haven’t run are skipped) unless Batch::allow_failures.
BatchStatus
How a batch is doing; from Queue::batch_status.
Chain
Jobs that run one after another; from Queue::chain.
Dashboard
A page showing the queue (/_renox/queue): jobs waiting per queue, how long the oldest has waited, throughput, failed jobs (retry or forget them) and recent batches.
FailedJob
A job that used up its attempts.
JobContext
What a job gets when it runs.
Middleware
A condition checked before a job’s attempt; see Job::middleware.
Queue
Dispatches jobs and wakes the workers of this process.
QueueCounts
Jobs of one queue.
QueueStats
The queue at a glance; from Queue::stats.
Worker
Runs queued jobs.

Constants§

DASHBOARD_GATE
The gate that decides who sees the dashboard.

Traits§

Job
A unit of background work. It is stored as JSON, so keep it to ids and small values rather than whole models.