Documentation menu

Application services

Queues

A queue moves slow work out of the request: the request pushes a job and answers at once, and a worker process (php taphra queue:work) runs the job in the background. This page covers the connection settings, job classes, pushing jobs, workers, retries and failed jobs.

Contents

#

Connections and drivers

#

Connections are named in app/Config/queue.yml. Like the other config files it has an all: section and one section per environment; an environment's entry replaces the all: entry of the same name.

app/Config/queue.yml
all:
  default:
    type: database
    connection: queue        # a connection from databases.yml

prod:
  default:
    type: redis
    host: 127.0.0.1
    port: 6379
    database: 0
    # password: secret
type Backend How a worker waits for jobs Settings
database a table _queue_jobs_ in any SQL connection, created on first use polls once a second connection, autoMigrate (default true)
redis a Redis server, through Predis BRPOP: wakes as soon as a job arrives host, port, database, password
beanstalkd a beanstalkd server, through Pheanstalk reserve: wakes as soon as a job arrives host, port, timeout

The database driver needs nothing beyond a database, so it suits development and small apps. The connection it names is an entry of app/Config/databases.yml; for development a SQLite file is enough:

app/Config/databases.yml
dev:
  queue:
    type: sqlite
    name: tmp/queue.sqlite   # relative to the app folder
    host: ""
    port: ""
    user: ""
    password: ""

A queue holds several named channels (a beanstalkd "tube", a Redis list, a queue column value). Use them to separate kinds of work, such as emails and reports, and run a worker per channel.

To add another backend, extend Taphra\Queue\AbstractQueue, name the class after the type (type: sqs loads SqsQueue) and put it in app/Queue/ as App\Queue\SqsQueue.

Writing a job

#

A job is a class that implements Taphra\Queue\JobInterface and has a public static method for the worker to call. The method receives the job's parameters and returns true when the work is done. Returning false or throwing marks the attempt as failed.

app/Jobs/ExportReportJob.php
<?php
// app/Jobs/ExportReportJob.php
namespace App\Jobs;

use Taphra\Queue\JobInterface;

final class ExportReportJob implements JobInterface
{
    public static function handle(string $month): bool
    {
        $csv = "month,orders\n{$month},128\n";   // build the real report here
        return file_put_contents(APP_TMP_PATH . "/report-{$month}.csv", $csv) !== false;
    }
}

Workers only call a public static method of a class that implements JobInterface, and never a method whose name starts with __. A payload that names a plain function or any other class is refused without running and handled as a failed job, so whoever can write to the queue cannot make the worker run arbitrary code. Every public static method of a job class can be named by a payload: keep helpers private, or put them in another class.

The worker boots the app like a request does, so the job can use DataStores, the cache, Email and the configuration.

Pushing a job

#

Queue::connection($name) opens a connection from queue.yml, and push($channel, $payload) adds a job. A string payload is stored as it is; an array (or any other value) is stored as JSON, and pull() gives the same array back. For queue:work, the payload names the class, the method and the parameters:

PHP
use App\Jobs\ExportReportJob;
use Taphra\Queue\Queue;

$jobId = Queue::connection('default')->push('reports', json_encode([
    'type'   => 'job',
    'name'   => 'export_report',          // shown in the worker's output
    'class'  => ExportReportJob::class,
    'method' => 'handle',
    'params' => ['2026-11'],              // passed to handle() in order
]));

The parameters go through json_encode(), so pass ids and plain values, not objects: the job loads what it needs when it runs. Always pass the payload as a string; the drivers store a string.

The same connection can read jobs directly, which is what a worker does:

PHP
use Taphra\Queue\Queue;

$queue = Queue::connection('default');
$job   = $queue->pull('reports', 0);     // wait 0 seconds; null when the channel is empty
$data  = json_decode($job->getData(), true);

$job->requeue();                         // put it back, for the worker below

pull() reserves the job so no other worker takes it. After processing, call done() to remove it, requeue() to put it back, or delete() to mark it failed.

Running a worker

#
Shell
php taphra queue:work reports

The worker runs until it is stopped and prints a line per job, such as [2026-11-30 09:15:02] Done: export_report.

Option Default Meaning
queue (argument) queue.defaultQueue from app.yml, else default the channel to process
--connection default the connection from queue.yml
--sleep 3 seconds to wait for a job before checking again (the BRPOP timeout with Redis)
--tries 3 attempts before a job is given up
--concurrency 1 worker processes; more than one needs ext-pcntl

With ext-pcntl loaded, SIGTERM and SIGINT (Ctrl+C) let the current job finish before the worker exits. Without it the process stops at once, in the middle of a job if one is running.

php taphra queue:listen is a development helper that restarts itself when a .php or .yml file in app/ changes. It differs from queue:work: it reads the connection from the queue: key of app.yml (with a type and the driver's settings), not from queue.yml; it runs each job once, without retries; and after a change it starts the new listener in the background and exits, so its output no longer reaches your terminal. Restarting queue:work by hand after a change is often simpler.

Retries and failed jobs

#

When a job returns false or throws, the worker waits and puts it back: 1 second after the first attempt, then 2, 4 and so on, up to 60 seconds. The worker itself sleeps during that wait, so it processes no other job meanwhile. Attempts are counted inside the worker process, so a restarted worker starts counting again.

After --tries attempts the job is given up:

Driver A job that was given up
database stays in _queue_jobs_ with status = 'failed'
redis is removed
beanstalkd is buried, and stays in the tube until you delete or kick it

With the database driver, list failed jobs with SQL, for example SELECT id, queue, payload FROM _queue_jobs_ WHERE status = 'failed'. php taphra queue:failed does not list them yet: in 1.0 it only prints a reminder.

Workers in production

#

Run workers under a process manager (systemd, Supervisor, a container orchestrator) that starts them on boot and restarts them when they exit. A minimal systemd unit:

INI
# /etc/systemd/system/myapp-queue.service
[Unit]
Description=myapp queue worker
After=network.target

[Service]
User=www-data
WorkingDirectory=/var/www/myapp
Environment=APP_ENV=prod
ExecStart=/usr/bin/php taphra queue:work reports --tries=5
Restart=always

[Install]
WantedBy=multi-user.target
  • A worker loads the code once, when it starts. Restart workers on every deploy (systemctl restart myapp-queue), or they keep running the old code.
  • Set APP_ENV for the worker as for the web server; an unset or unknown value means production.
  • Install ext-pcntl so a restart lets the current job finish.
  • A worker has no memory or job-count limit of its own. Long-running workers are restarted by the process manager; schedule a periodic restart if a job leaks memory.

deployment.md covers the rest of a production setup.

Limits in 1.0

#
  • There is no delayed dispatch: a pushed job is available at once. For work that must happen later, use the scheduler.
  • A job that was running when its worker died is not retried automatically: with the database driver it stays reserved, with Redis it is gone. Beanstalkd releases it again after its time to run.