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 Self: 'async_trait,
'life0: '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 Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait { ... }
fn timeout(&self) -> Duration { ... }
}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.