BackgroundJob

Trait BackgroundJob 

Source
pub trait BackgroundJob:
    Serialize
    + DeserializeOwned
    + Send
    + Sync
    + 'static {
    type Context: Clone + Send + 'static;

    const JOB_NAME: &'static str;
    const PRIORITY: i16 = 0i16;
    const DEDUPLICATED: bool = false;
    const QUEUE: &'static str = DEFAULT_QUEUE;

    // Required method
    fn run(&self, ctx: Self::Context) -> impl Future<Output = Result<()>> + Send;

    // Provided method
    fn enqueue<'a>(
        &'a self,
        pool: &'a PgPool,
    ) -> BoxFuture<'a, Result<Option<i64>, EnqueueError>> { ... }
}
Expand description

The main trait for defining background jobs. Trait for defining background jobs that can be enqueued and executed asynchronously.

Required Associated Constants§

Source

const JOB_NAME: &'static str

Unique name of the task.

This MUST be unique for the whole application.

Provided Associated Constants§

Source

const PRIORITY: i16 = 0i16

Default priority of the task.

[Self::enqueue_with_priority] can be used to override the priority value.

Source

const DEDUPLICATED: bool = false

Whether the job should be deduplicated.

If true, the job will not be enqueued if there is already an unstarted job with the same data.

Source

const QUEUE: &'static str = DEFAULT_QUEUE

Job queue where this job will be executed.

Required Associated Types§

Source

type Context: Clone + Send + 'static

The application data provided to this job at runtime.

Required Methods§

Source

fn run(&self, ctx: Self::Context) -> impl Future<Output = Result<()>> + Send

Execute the task. This method should define its logic.

Provided Methods§

Source

fn enqueue<'a>( &'a self, pool: &'a PgPool, ) -> BoxFuture<'a, Result<Option<i64>, EnqueueError>>

Enqueue this job for background execution.

Returns the job ID if successfully enqueued, or None if deduplicated.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§