pub trait Queueable: Serialize + Deserialize {
// Required method
fn handle(&self) -> Result<Option<String>, Box<dyn Error>>;
// Provided methods
fn dispatch(&self) -> Result<(), Box<dyn Error>>
where Self: Sized { ... }
fn schedule_at(&self, at_time: NaiveDateTime) -> Result<(), Box<dyn Error>>
where Self: Sized { ... }
}Expand description
A trait representing a Queueable struct in the system.
This trait uses typetag::serde to enable dynamic serialization/deserialization,
allowing different job types to be stored or retrieved from a database while
preserving their concrete types at runtime.
Required Methods§
Sourcefn handle(&self) -> Result<Option<String>, Box<dyn Error>>
fn handle(&self) -> Result<Option<String>, Box<dyn Error>>
Called when a worker reserves this job and needs to process it.
§Returns
Returns Ok(()) if processing completes successfully. Otherwise, returns
an error describing the issue encountered.
§Example
ⓘ
struct MyJob {
// fields...
}
impl Queueable for MyJob {
fn handle(&self) -> Result<Option<String>, Box<dyn std::error::Error>> {
// custom logic here
Ok(())
}
}Provided Methods§
Sourcefn dispatch(&self) -> Result<(), Box<dyn Error>>where
Self: Sized,
fn dispatch(&self) -> Result<(), Box<dyn Error>>where
Self: Sized,
Queues this job for immediate processing by inserting it into the jobs table.
Implementation notes:
- The job is serialized via the
serialize::serializefunction. - A new
NewJobinstance is created with the current time asavailable_at. - The
JobModel::createmethod is then used to insert the job into the database.
§Returns
Ok(())if the job was successfully serialized and inserted.Err(Box<dyn Error>)if serialization fails or the insert operation encounters an error.
§Constraints
- This method requires
Self: Sizedbecause it needs to create a newNewJobbased on the concrete type implementing this trait.
Sourcefn schedule_at(&self, at_time: NaiveDateTime) -> Result<(), Box<dyn Error>>where
Self: Sized,
fn schedule_at(&self, at_time: NaiveDateTime) -> Result<(), Box<dyn Error>>where
Self: Sized,
Schedules this job to be available at a specified future time, instead of right away.
Similar to dispatch, but sets a custom available_at timestamp,
delaying when the job becomes eligible for processing.
§Parameters
at_time: The timestamp after which this job can be processed.
§Returns
Ok(())if the job was successfully serialized and inserted into the database with the specified availability time.Err(Box<dyn Error>)if serialization fails or the insertion encounters an error.
§Example
ⓘ
let future_time = chrono::Local::now().naive_local() + chrono::Duration::minutes(30);
my_job.schedule_at(future_time)
.expect("Failed to schedule the job for later processing");