Skip to main content

Queueable

Derive Macro Queueable 

Source
#[derive(Queueable)]
{
    // Attributes available to this derive:
    #[serde]
}
Expand description

Implement Queueable so that the queue can store the struct, with its model fields stored as keys.

use laravel::queue::{Queueable, job};

#[derive(Queueable)]
pub struct SendWelcomeEmail {
    pub user: User,
    pub note: String,
}

#[job]
impl SendWelcomeEmail {
    async fn handle(mut self) -> laravel::Result<()> {
        update!(self.user { welcomed: true }).await?;

        Ok(())
    }
}

The queue stores a model field as the model’s key, an Option of a model as the key or null, and a Vec of models as an array of keys. For example, it stores the job above as {"user": 42, "note": "Hi"}. The queue stores every other field as JSON through the field’s Serialize and Deserialize implementations, including a model nested inside another field’s type.

Before the job runs, the worker reads the rows from the database’s writer connection, with one query per model type and without relationships. A Vec field leaves out the models whose rows are gone and keeps the rest in their stored order. When the row of a model field or an Option field is gone, the worker deletes the job without running it. With the fail_when_missing_models option, the job fails for good with a MissingModels error instead.

A field may take the following #[serde(..)] attributes, which have Serde’s meaning. They let jobs that are already on the queue still run after you change the field:

AttributeMeaning
defaultA job stored without the field gets Default::default(). A model field must be an Option or a Vec
default = "path"A job stored without the field gets the value the function returns. Not on a model field
rename = "name"Store and read the field under name
alias = "name"Also read the field from name. May repeat

Any other #[serde(..)] attribute, on a field or on the struct, is a compile error:

ⓘ
#[derive(laravel::queue::Queueable)]
pub struct SendReport {
    #[serde(skip)]
    pub draft: String,
}

A model has no default value, so a model field that isn’t an Option or a Vec can’t take default:

ⓘ
#[derive(laravel::queue::Queueable)]
pub struct SendWelcomeEmail {
    #[serde(default)]
    pub user: User,
}

The struct can’t be generic:

ⓘ
#[derive(laravel::queue::Queueable)]
pub struct Generic<T> { pub value: T }