Process a note after the response

Dispatch a job that stores only the note’s primary key, and let a rolled-back transaction drop it.

The background course is still 0.x. A job attribute that moves belongs to the queues guide for the crate you installed.

task::background dies with the process. A queue job is written down and picked up by a worker, including after a restart. laravel dev already starts that worker next to the server and Vite. Saving a file under src/ rebuilds and restarts both. On a server you will run queue:work yourself. The ship course shows that command.

The job

Create src/jobs/process_note.rs, then declare the module the structure guide shows. Rust skips an undeclared file without a warning, and the #[job] macro never runs:

// src/lib.rs
pub mod jobs;
// src/jobs/mod.rs
mod process_note;

pub use process_note::ProcessNote;
use laravel::queue::{Queueable, job};

use crate::models::Note;

#[derive(Queueable)]
pub struct ProcessNote {
    pub note: Note,
}

#[job(tries = 3)]
impl ProcessNote {
    async fn handle(self) -> laravel::Result<()> {
        self.note.load(Note::user).await?;

        // The row is the one in the database now, not the struct you dispatched.

        Ok(())
    }
}

Dispatch stores the primary key, not the struct you held and not its relationships. Before handle, the worker reloads the row. If the row is gone, the worker deletes the job and does not run handle. The queues guide logs that at info and names the missing model. Relationships are not serialized, so handle loads Note::user itself.

The default is one attempt. tries = 3 is the documented option for a job that should run again after an error or a panic. Backoff and timeouts are further options on the same attribute. Leave them off until the guide’s table matches a failure you have actually seen.

From the handler that created the note:

ProcessNote { note }.dispatch().await?;

handle has no HTTP request and no session. It does get a fresh application context, so config and URL generation work. Do not put a database connection or an open file in the struct. The queues guide limits fields to plain data or models.

A rollback must not enqueue

Dispatch inside db::transaction waits for the commit. A rollback discards the job. The worker then cannot see a note that never landed:

use laravel::db;

db::transaction(async |_tx| {
    let note = create!(Note {
        user: &user,
        title: &form.title,
        body: &form.body,
    })
    .await?;

    ProcessNote { note }.dispatch().await?;

    Ok::<_, laravel::Error>(())
})
.await?;

Inside the transaction, dispatch returns the job id immediately and pushes after commit. If the push fails after commit, the database change stays and db::transaction returns that error. The queues guide says to retry the dispatch, not to rerun the transaction. dispatch_sync is the opposite: it runs handle immediately, even on an open transaction, so it can see uncommitted rows. Do not use it to “be safe” inside this closure.

The checkpoint is easier to observe if you return an error after dispatch and before the closure ends. The transaction rolls back, the note is absent, and the worker has no ProcessNote for it. A note you commit should show up in the laravel dev output when the worker runs handle.

Check your work

Create a note, then force the insert to roll back. laravel dev’s worker should not run ProcessNote for that row.

Back to roadmap