pub trait Job:
Send
+ Sync
+ 'static {
// Required method
fn handle<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
where 'life0: 'async_trait,
Self: 'async_trait;
// Provided methods
fn name(&self) -> &'static str { ... }
fn max_retries(&self) -> u32 { ... }
fn retry_delay(&self, attempt: u32) -> Duration { ... }
fn failed<'life0, 'life1, 'async_trait>(
&'life0 self,
error: &'life1 Error,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where 'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait { ... }
fn timeout(&self) -> Duration { ... }
fn idempotency_key(&self) -> Option<String> { ... }
}Expand description
A job that can be executed by a queue worker.
Jobs contain the logic that should run in the background. They must be serializable so they can be stored in the queue.
§Example
use ferro_queue::{Job, Error, async_trait};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
struct ProcessImage {
image_id: i64,
operations: Vec<String>,
}
#[async_trait]
impl Job for ProcessImage {
async fn handle(&self) -> Result<(), Error> {
println!("Processing image {} with {:?}", self.image_id, self.operations);
Ok(())
}
fn max_retries(&self) -> u32 {
5
}
fn retry_delay(&self, attempt: u32) -> std::time::Duration {
// Exponential backoff
std::time::Duration::from_secs(2u64.pow(attempt))
}
}Required Methods§
Provided Methods§
Sourcefn max_retries(&self) -> u32
fn max_retries(&self) -> u32
Maximum number of times to retry the job on failure.
Sourcefn retry_delay(&self, attempt: u32) -> Duration
fn retry_delay(&self, attempt: u32) -> Duration
Delay before retrying after a failure.
Default: full-jitter exponential backoff — rand(0..=min(cap, base * 2^attempt)).
Base 5 s, cap 15 min. Saturating arithmetic prevents overflow on large attempt values.
Sourcefn failed<'life0, 'life1, 'async_trait>(
&'life0 self,
error: &'life1 Error,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
fn failed<'life0, 'life1, 'async_trait>(
&'life0 self,
error: &'life1 Error,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
Called when the job fails after all retries are exhausted.
Sourcefn idempotency_key(&self) -> Option<String>
fn idempotency_key(&self) -> Option<String>
Idempotency key for deduplication on enqueue.
When Some, enqueue skips insertion if a pending or claimed row with
the same (job_type, idempotency_key) already exists (D-15).
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".