Step 11 — Background work

When a task is assigned, DeskFlow should email Alex after the HTTP response — not block the API.

What you will learn

  • Use global defer() for post-response work
  • Log assignment without slowing task.create
Before you start
  • Step 10
  • composer require react/promise if not already in your app (needed for defer/async helpers)

defer after create

In createAction, after save():

$assignee = $data->getString('assignee', 'alex@northwind.studio');

defer(function () use ($assignee, $task) {
    logger()->info('DeskFlow assignment email queued', [
        'assignee' => $assignee,
        'task_id' => $task->id ?? null,
    ]);
});

The client gets JSON immediately; logging runs after send().

For durable jobs with RoadRunner, use async('mail', 'send', …) — see Background work.

Common mistakes

  • Heavy SMTP inside the action — always defer or queue email.
  • Expecting defer to run in a new OS thread — same worker, after response flush.