← All guides

· 4 min read

Durable workflows in Laravel Rust, explained

Recover a failed subscription payment over ten days with one async function that survives sleeps, deploys and restarts, instead of chained delayed jobs.

  • workflows
  • queues

Every subscription business has this process. A customer’s card is declined at renewal. You email them, wait a few days, try the card again, wait a bit longer, try once more, and if it still fails, pause the subscription.

In Laravel PHP that usually means a job that dispatches a delayed job, then another, plus a retry_stage column to remember where each customer is. Then comes a scheduled command to rescue the ones that got stuck after a deploy. Laravel Rust has one feature for the whole thing: workflows.

The workflow

// src/workflows/recover_payment.rs
use laravel::queue::Queueable;
use laravel::time::ToSpan;
use laravel::workflow::{Run, workflow};

use crate::models::Invoice;

#[derive(Queueable)]
pub struct RecoverPayment {
    pub invoice_id: i64,
}

#[workflow(name = "billing.recover-payment")]
impl RecoverPayment {
    async fn handle(self, run: &Run) -> laravel::Result<()> {
        run.step("email the customer", self.send_failed_payment_email()).await?;

        for days in [3, 7] {
            run.sleep(days.days()).await?;

            let paid = run.step(format!("retry charge after {days} days"), self.retry_charge()).await?;

            if paid {
                return Ok(());
            }
        }

        run.step("pause the subscription", self.pause_subscription()).await?;

        Ok(())
    }
}

impl RecoverPayment {
    async fn retry_charge(&self) -> laravel::Result<bool> {
        let invoice = Invoice::find_or_fail(self.invoice_id).await?;

        // Your own method: charges the card through your payment provider.
        invoice.charge_again().await
    }

    // send_failed_payment_email and pause_subscription follow the same pattern.
}

The struct derives Queueable, the way a job would, and its fields are the workflow’s input. The #[workflow] block holds handle: the whole ten-day process, top to bottom, in plain Rust. Each workflow lives in its own file in src/workflows/.

When the payment webhook reports a declined renewal, start a run the way you’d dispatch a job:

RecoverPayment { invoice_id: invoice.id }.start().await?;

The payment recovery on a timeline: on day 0 the renewal fails and the customer is emailed; the run sleeps 3 days and retries the card; it sleeps 7 more days and retries again; if both retries fail, the subscription is paused. No worker waits during the sleeps.

Why a ten-day process survives deploys

You don’t run anything new: workflows use the queue workers you already have. Each time a run moves forward, a queued job calls handle again, from the first line:

Four advances of the same run. Day 0: the email step runs, then the run sleeps. Day 3: the email step returns its stored result without sending again, and the first retry runs and is declined. Day 10: both earlier steps return stored results, and the second retry starts but the server crashes. On the retry after the crash, the earlier steps return stored results and the second retry runs and succeeds.

  • A step that finished on an earlier advance doesn’t run again. It hands back the result it stored, so the customer never gets the email twice.
  • The first unfinished step runs, and its result is saved.
  • sleep queues the next advance with a delay and frees the worker.

No process sits waiting for ten days, and deploys or crashed servers in between lose nothing.

Tip: sleeps need a queue that can delay jobs. Use the database, redis or sqs connection. SQS FIFO queues can’t delay, and on sync the run stops at the first sleep.

Three rules for replay-safe code

Because handle replays from the top, it has to reach the same decisions every time. Laravel checks what it can and fails the run with an explanation when a rule is broken.

1. Every side effect goes in a step. Charging a card or sending an email outside a step would happen again on every replay. That’s why retry_charge is a step and not a line in handle.

2. Decide on inputs and stored results only. if paid is safe because paid came out of a step. The current time or a random number read directly in handle is not; read them inside a step.

3. Step names are unique within a run. The loop above calls retry_charge twice, so each call gets the number of days in its name: retry charge after 3 days and retry charge after 7 days.

Tip: make charges safe to repeat. If a worker dies in the middle of a step, the step’s result was never stored, so the step runs again on the next attempt. For a payment, pass your provider an idempotency key built from the invoice and the step name, so a repeated attempt can’t charge twice.

Tip: name the workflow before the first deploy. A sleeping run finds its code by the workflow’s name, and the default name is the module path. Move or rename the struct later and runs that are already waiting can’t find it. Give it a name of its own, as RecoverPayment does in the example above, and that can’t happen.

Tip: keep IDs in the input, not models. A model field in the input is stored as its key and reloaded on every replay, so it shows the row as it is now, not as it was when the run started. That’s why RecoverPayment holds invoice_id and loads the invoice inside the step.

When a run gives up

Add a failed method next to handle. It runs once, after the run has failed for good, for example if the invoice was deleted. That makes it the place to alert your billing team, so no customer silently falls through the cracks.

The short version

Anything shaped like “do this, wait, check that, maybe do one more thing” used to mean delayed jobs and status columns. In Laravel Rust it’s one async function you can read from top to bottom.