A job runs once. A welcome that waits three days and then checks whether the person wrote anything is a workflow. Laravel runs it on the queue workers you already have. At run.sleep, the run suspends, the worker finishes that job, and nothing sits idle for the delay. A restart resumes the run. task::background cannot do that.
The starter does not create the workflow tables. Add one migration whose body is only the macro, then laravel migrate:
laravel::workflow::tables!();
The file still has to live directly in database/migrations/. The macro creates the five tables the workflows guide lists. The default store is database. Tests switch to memory. Do not set WORKFLOW_STORE=memory in production. That store forgets every run when the process exits.
The worker must use a connection that supports delayed jobs. The guide names database, redis, and sqs. laravel dev uses whatever QUEUE_CONNECTION your .env already has. If sleep never resumes, check that connection before rewriting the workflow.
The run
use laravel::db;
use laravel::mail;
use laravel::queue::Queueable;
use laravel::time::ToSpan;
use laravel::workflow::{Run, workflow};
use crate::mail::NoteShared;
use crate::models::{Note, User};
#[derive(Queueable)]
pub struct WelcomeAuthor {
pub user_id: i64,
}
#[workflow(name = "notes.welcome")]
impl WelcomeAuthor {
async fn handle(self, run: &Run) -> laravel::Result<()> {
run.step("send welcome", async {
let user = User::query().find_or_fail(self.user_id).await?;
let note = Note::query()
.filter(Note::user_id.eq(self.user_id))
.latest(Note::created_at)
.first()
.await?;
if let Some(note) = note {
mail::to(&user).queue(NoteShared::new(note)?).await?;
}
Ok(())
})
.await?;
run.sleep(3.days()).await?;
let count = run
.step("count notes", async {
Note::query().filter(Note::user_id.eq(self.user_id)).count().await
})
.await?;
if count == 0 {
run.step("send tips", async {
let _users = db::table("users").count().await?;
Ok(())
})
.await?;
}
Ok(())
}
}
NoteShared arrives in the next lesson. Until that file exists, keep the welcome step as the count, or comment the mail call. Do not invent a second mail API to make this file compile today.
run.step runs its future once and stores the result under the name. Later advances replay the stored result and do not run the future again. The workflows guide says to branch on data that can change only inside a recorded step. The count is inside a step for that reason. The sleep is not a step. It suspends the run.
#[workflow(name = "notes.welcome")] is the stable id. Renaming the struct without a name orphans runs that are already in flight. Declare pub mod workflows; in src/lib.rs, the same way you declared jobs.
Start it when the account is created, after auth.login:
WelcomeAuthor { user_id: user.id }.start().await?;
start persists the run, queues the first advance, and returns a RunId. The struct stores user_id rather than a User. The workflows guide prefers an id when the row can change during a sleep. A model field would serialize only its primary key and reload on every advance, which is the same rule as a job, but the guide still tells you to prefer the id for that case.
3.days() comes from laravel::time::ToSpan, which is what the guide’s run.sleep(3.days()) example relies on. The checkpoint is local: start a run, watch the worker pick up the first step, and see it become free while the sleep is pending. Restart laravel dev. The run should still be scheduled, because it lives in the workflow tables, not in the process.
Check your work
Start the workflow, then confirm a worker is free during the sleep and that the run is still there after you restart laravel dev.