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:
- Implement
SerializeandDeserialize(for storage) - Implement
Send + Sync(for Tokio task boundaries) - Provide a
const NAME: &'static strunique identifier - 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§
Required Methods§
Provided Methods§
Sourcefn options() -> JobOptions
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.