Queues and scheduling
Use a background job when the request can finish before the work does. For example, once an order is saved, a worker can write its picking list separately. Give the job the small set of values it needs and resolve application services when it runs.
Defining Jobs
A job has a stable type() used in its stored payload and a handle() method that performs the work. The picking-list job stores the order ID and item quantities. Atom injects Paths when it invokes handle().
<?php
declare(strict_types=1);
namespace App\Jobs;
use Atom\Queue\Job;
use Atom\Support\Paths;
use RuntimeException;
final readonly class WritePickingList extends Job
{
/** @param array<string, int> $items Product codes mapped to quantities. */
public function __construct(private int $orderId, private array $items)
{
}
public static function type(): string
{
return "warehouse.write-picking-list";
}
public function handle(Paths $paths): void
{
$lines = ["Order {$this->orderId}"];
foreach ($this->items as $code => $quantity) {
$lines[] = "{$quantity} x {$code}";
}
$path = $paths->resolve("@root/storage/pick-{$this->orderId}.txt");
if (file_put_contents($path, implode(PHP_EOL, $lines) . PHP_EOL, LOCK_EX) === false) {
throw new RuntimeException("Could not write the picking list.");
}
}
}
Register the job class in the application's jobs() hook so a worker can reconstruct the class from the stored type.
use App\Jobs\WritePickingList;
use Atom\Queue\JobRegistry;
protected function jobs(JobRegistry $jobs): void
{
$jobs->register(WritePickingList::class);
}
Dispatch a job
Inject Atom\Queue\JobDispatcherInterface into the service that saves the order. Once the order is committed, pass the job to the dispatcher.
$dispatcher->dispatch(
new WritePickingList($orderId, $items),
queue: "warehouse",
);
The job's constructor values become the queued payload. Keep those values JSON-compatible. Add delay: 300 to make the job available after five minutes on an asynchronous driver.
Drivers
The default sync driver runs the job inline during dispatch() and ignores delays. To let a worker process jobs separately, select an asynchronous driver. The file driver stores jobs locally and needs no queue tables.
QUEUE_DRIVER=file
Use QUEUE_DRIVER=database to store jobs in the configured database. Publish its migration with php atom queue:publish and apply it with php atom migrate.
Workers and Retries
Run a worker for the same named queue used by the dispatcher. queue:once processes at most one available job and exits. queue:work keeps polling for new work.
php atom queue:once --queue=warehouse
php atom queue:work --queue=warehouse
When a job throws, the worker can make it available again after QUEUE_RETRY_DELAY seconds. Once it reaches QUEUE_MAX_ATTEMPTS, it moves to failed storage. Use php atom queue:failed --queue=warehouse to inspect those failures. Design handlers so retrying a job can safely repeat the work. For example, the picking-list job writes to the same order-specific filename on each attempt.
See Scheduling to dispatch jobs at recurring times.