Skip to main content

Job

Trait Job 

Source
pub trait Job:
    Serialize
    + DeserializeOwned
    + Send
    + Sync
    + 'static {
    const NAME: &'static str;
    const QUEUE: &'static str = "default";
    const MAX_ATTEMPTS: u32 = 3;
    const TIMEOUT: Duration = _;
    const ENCRYPTED: bool = false;
    const UNIQUE_FOR: Option<Duration> = None;

    // Required method
    fn handle(self, ctx: JobContext) -> impl Future<Output = Result> + Send;

    // Provided methods
    fn backoff(attempt: u32) -> Duration { ... }
    fn unique_id(&self) -> String { ... }
    fn middleware(&self) -> Vec<Middleware> { ... }
    fn failed(
        self,
        ctx: JobContext,
        error: String,
    ) -> impl Future<Output = ()> + Send { ... }
}
Expand description

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

Required Associated Constants§

Source

const NAME: &'static str

A stable name; stored with each job, so don’t rename it while jobs are queued.

Provided Associated Constants§

Source

const QUEUE: &'static str = "default"

Its queue; queue:work --queue high,default drains queues in order.

Source

const MAX_ATTEMPTS: u32 = 3

Runs before the job is moved to failed_jobs.

Source

const TIMEOUT: Duration = _

How long one attempt may run.

Source

const ENCRYPTED: bool = false

Store the payload encrypted with APP_KEY (it holds personal data or secrets). The worker decrypts it; failed_jobs keeps it encrypted.

Source

const UNIQUE_FOR: Option<Duration> = None

While a job with the same Job::unique_id is queued or running, dispatching another returns the queued one’s id instead, for up to this long (the claim is dropped when the job finishes).

Required Methods§

Source

fn handle(self, ctx: JobContext) -> impl Future<Output = Result> + Send

Does the work; an error fails the attempt (retried unless Error::permanent).

Provided Methods§

Source

fn backoff(attempt: u32) -> Duration

Wait before retrying after the given failed attempt (1-based).

Source

fn unique_id(&self) -> String

What makes two jobs “the same” for Job::UNIQUE_FOR, e.g. the order id. Empty (the default) makes every job of the type the same.

Source

fn middleware(&self) -> Vec<Middleware>

Conditions checked before each attempt; a job that can’t run yet is put back without using up an attempt.

Source

fn failed( self, ctx: JobContext, error: String, ) -> impl Future<Output = ()> + Send

Runs once the job has failed for good (attempts used up, or a permanent error), with the last error, e.g. to tell the user. ctx is the last attempt’s: the state, the job’s id, the attempt, its batch.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§

Source§

impl Job for ServerEvent

Source§

const NAME: &'static str = "renox:analytics"