Skip to main content

middleware

Attribute Macro middleware 

Source
#[middleware]
Expand description

Turn an async function into a job Middleware struct with the function’s name.

The function takes next: Next and returns laravel::Result<()>, usually by returning next.run().await, which runs the rest of the job. Before next, the function may take Inject<T> services, Section<T> config sections, and an &Attempt, in any order. Any parameters after next become the struct’s fields, and a job passes their values when it lists the middleware:

use laravel::container::Inject;
use laravel::queue::{Attempt, Next, job, middleware};
use laravel::time::ToSpan;

#[middleware]
pub async fn skip_inactive(
    accounts: Inject<Accounts>,
    attempt: &Attempt,
    next: Next,
    account_id: i64,
) -> laravel::Result<()> {
    if !accounts.is_active(account_id).await? {
        attempt.delete();
        return Ok(());
    }

    next.run().await
}

#[derive(laravel::queue::Queueable)]
pub struct SyncContacts {
    pub account_id: i64,
}

#[job]
impl SyncContacts {
    fn middleware(&self) -> skip_inactive {
        skip_inactive(self.account_id)
    }

    async fn handle(self) -> laravel::Result<()> {
        // ...
        Ok(())
    }
}