Skip to main content

Job

Trait Job 

Source
pub trait Job: Queueable {
    const NAME: &'static str;

    // Required method
    fn handle(
        self,
        attempt: &Attempt,
    ) -> impl Future<Output = Result<(), Error>> + Send;

    // Provided methods
    fn options() -> JobOptions { ... }
    fn middleware(&self) -> MiddlewareGroup { ... }
    fn debounce_id(&self) -> String { ... }
    fn failed(
        self,
        error: &Error,
        attempt: &Attempt,
    ) -> impl Future<Output = Result<(), Error>> + Send { ... }
}
Expand description

A Queueable struct with a handler, which the queue keeps and runs later.

Implement this trait with #[job] on the struct’s impl block. The attribute names the job, reads its options, and resolves the handler’s services. The struct’s fields are the job’s data, which the queue stores until the job runs.

Required Associated Constants§

Source

const NAME: &'static str

The name that the queue stores the job under and finds it by.

The name defaults to the type’s path, so moving or renaming the type leaves jobs that are already queued without a handler. Set #[job(name = "..")] to keep the name stable. Names must be unique, and an application with two jobs of the same name, including jobs from packages, doesn’t start.

Required Methods§

Source

fn handle( self, attempt: &Attempt, ) -> impl Future<Output = Result<(), Error>> + Send

Run the job.

An error fails the attempt. The worker retries the job after its backoff, or fails it for good once it has no tries left. A TooManyAttempts error instead releases the job until the rate limit allows it, or fails the job for good when that time falls after its retry_for.

Provided Methods§

Source

fn options() -> JobOptions

The job’s queue, retries, and timeout. Defaults to JobOptions::new.

Source

fn middleware(&self) -> MiddlewareGroup

The middleware that wraps handle on each attempt, with the first middleware outermost.

The worker calls this method before each attempt, so the middleware can depend on the job’s data. Defaults to no middleware.

Source

fn debounce_id(&self) -> String

The ID that tells this job’s debounced dispatches apart.

Of the dispatches with the same ID, only the latest runs. The queue uses the ID only when the job sets debounce_for. Defaults to an empty ID, so every dispatch of the job shares one ID.

The queue records each ID’s latest dispatch in the default cache store. Dispatching fails when the cache can’t be written, and the job runs when the worker can’t read the cache. A job may therefore run more often than it should, but it’s never lost. Before each run, including retries and runs after a release, and before the job’s middleware, the queue removes a job that a later dispatch replaced, with the outcome JobOutcome::Debounced. A replaced job’s chain doesn’t run.

A dispatch’s own delay replaces debounce_for. A dispatch inside a transaction replaces earlier dispatches when the transaction commits. When the connection refuses a dispatch, every earlier waiting dispatch runs. A dispatch made max_wait after a wait started runs, and the next dispatch starts a new wait. A job that starts its handle also ends the wait. A debounced job later in a chain claims its ID when the job before it finishes. queue:retry doesn’t make a job the latest dispatch. The sync connection, dispatch_sync, and test attempts don’t debounce.

Source

fn failed( self, error: &Error, attempt: &Attempt, ) -> impl Future<Output = Result<(), Error>> + Send

Clean up after the job has failed for good.

This method receives a copy of the job restored from its payload, with its models read again, so changes that handle made to its own copy are gone. A job that the worker can’t restore fails without calling this method. The worker reports any error this method returns.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<L> Job for Queued<L>
where L: QueuedListener,

Source§

const NAME: &'static str = L::NAME