Skip to main content

Job

Trait Job 

Source
pub trait Job:
    Serialize
    + DeserializeOwned
    + Send
    + Sync
    + 'static {
    const NAME: &'static str;

    // Required method
    fn execute<'life0, 'async_trait>(
        self,
        ctx: &'life0 JobContext,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;

    // Provided method
    fn options() -> JobOptions { ... }
}
Expand description

A background job that can be enqueued and executed by workers.

§Implementing Job

Your job struct must:

  1. Implement Serialize and Deserialize (for storage)
  2. Implement Send + Sync (for Tokio task boundaries)
  3. Provide a const NAME: &'static str unique identifier
  4. Implement async execute(self, ctx) -> Result<()>

§Example

use backyard_core::{Job, JobContext, Result};
use async_trait::async_trait;
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize)]
struct SendEmail {
    to: String,
    subject: String,
}

#[async_trait]
impl Job for SendEmail {
    const NAME: &'static str = "SendEmail";

    async fn execute(self, _ctx: &JobContext) -> Result<()> {
        println!("Sending {} to {}", self.subject, self.to);
        Ok(())
    }
}

Required Associated Constants§

Source

const NAME: &'static str

Unique identifier for this job type. Must be stable and unique across your entire application.

Required Methods§

Source

fn execute<'life0, 'async_trait>( self, ctx: &'life0 JobContext, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Execute the job.

§Arguments
  • ctx - Job context including the queue (for enqueueing other jobs from within)
§Returns
  • Ok(()) - Job succeeded
  • Err(BackyardError) - Job failed; will be retried or moved to dead letter queue

Provided Methods§

Source

fn options() -> JobOptions

Default enqueue options for this job type. Override to customize queue name, retries, priority, etc.

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§