Queueable

Trait Queueable 

Source
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§

Source

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§

Source

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::serialize function.
  • A new NewJob instance is created with the current time as available_at.
  • The JobModel::create method 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: Sized because it needs to create a new NewJob based on the concrete type implementing this trait.
Source

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");

Trait Implementations§

Source§

impl<'typetag> Serialize for dyn Queueable + 'typetag

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<'typetag> Serialize for dyn Queueable + Send + 'typetag

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<'typetag> Serialize for dyn Queueable + Send + Sync + 'typetag

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<'typetag> Serialize for dyn Queueable + Sync + 'typetag

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more

Implementors§