~/docs
Resonate
Resonate
2.x 2.4.0, current
Torque
Toggle sidebar
Torque
webpatser/torque 2.4.0

Torque 2.x

Deployment

Supervisor config, zero-downtime reload, containers and Redis settings for running Torque in production.

Supervisor

Generate a Supervisor config:

php artisan torque:supervisor --workers=4 --user=forge
  • --php: the PHP binary written into the config (default PHP_BINARY). PHP_BINARY is the fully resolved binary and can go stale after an upgrade, so in production pass a stable path such as --php=/usr/bin/php8.6.
  • --app-path: the application path written into the config; it must be absolute. When base_path() is a Deployer release (releases/N), the sibling current symlink is detected and used automatically.
  • --stdout: print the config instead of writing a file, for example php artisan torque:supervisor --stdout | sudo tee /etc/supervisor/conf.d/torque.conf.

Without --stdout the file is storage/torque-supervisor.conf. Copy it into place:

sudo cp storage/torque-supervisor.conf /etc/supervisor/conf.d/torque.conf
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start torque

stopwaitsecs in the generated config derives from drain_grace_seconds. Regenerate the config after changing the grace, and set the same number in systemd or container stop timeouts and in deploy tools that wait on torque:stop or torque:reload.

Zero-downtime reload

torque:stop followed by torque:start works for cold deploys but leaves a gap in which jobs queue up in Redis. torque:reload swaps the master in one step. Pick the mode by who owns the respawn.

Under a process supervisor

Use --drain. The supervisor owns respawning, so the reload only signals:

# supervisord (autorestart=true): drain, exit, supervisor respawns from the
# current release. systemd: wire it as ExecReload=; Kubernetes: preStop.
php artisan torque:reload --drain

A spawned replacement would escape supervision permanently, which is why the default mode warns when the master looks supervised.

Unsupervised hosts

The default takeover handshake:

php artisan torque:reload

The reload spawns torque:start --takeover=<oldPid>, which boots its fleet in its own session, waits for its own workers' first metrics heartbeat (takeover_ready_timeout_seconds, default 30), and only then claims the PID file and signals the old master to drain. A replacement whose fleet never becomes healthy aborts the takeover with the old master untouched.

In-flight jobs finish on the old master, and Redis handles claim-once semantics across both fleets. For takeover restarts keep drain_grace_seconds below the stream retry_after so the brief overlap cannot double-claim jobs. --drain restarts are unaffected. One accepted risk on unsupervised hosts: a takeover master SIGKILLed after claiming the PID file leaves nothing to respawn it.

Two clocks, one ceiling

--timeout is how long torque:reload itself keeps watching. drain_grace_seconds is what the master and its workers hold themselves to. The worst case of a drain is the reload pause (reload_pause_seconds, 10) plus the grace plus a slack (15): 85 seconds at package defaults. Left unset, --timeout derives from that worst case. Pass it explicitly for deploy tools with their own run timeout:

# Deployer's run() defaults to a 300s timeout; keep the reload inside it.
php artisan torque:reload --drain --if-running --timeout=240

A short explicit timeout means "stop watching", not "cut the drain short". Past it the command reports that the master is still draining and returns successfully, without a signal. torque:stop derives its pre-SIGKILL window the same way and takes the same --timeout override.

A drain pause belongs to the master that wrote it. A master starting into a drain:<pid> pause whose PID is no longer running deletes the key and logs it. A deliberate torque:pause is never cleared automatically, and torque:status names which of the two a pause is.

Mutual exclusion between masters is a lifetime flock on storage/torque.lock. The PID file self-heals every second. Concurrent reloads fail fast on storage/torque.reload.lock.

Containers

When storage/ is a bind mount or persistent volume, storage/torque.pid outlives the container. torque:start verifies that the recorded PID belongs to a running Torque master (through /proc/<pid>/cmdline on Linux, ps elsewhere), so a stale PID file reads as "not running" even when its number was recycled. torque:stop treats such a file the same way: it warns, signals nothing, keeps the file, sweeps orphaned torque:worker processes and exits 0. No manual rm storage/torque.pid is needed.

The heartbeat files of the stall watchdog live in storage/framework/torque-heartbeats and need a local disk.

Worker respawn

A worker that dies within 10 seconds of its spawn, other than by an orderly stop (status 0 or 12) or a watchdog kill, is not replaced at once. The cause is usually a failed exec (PHP_BINARY gone after a PHP upgrade, see torque:supervisor --php), an exception while booting, or an unusable setting such as FLEDGE_EVENT_LOOP. The replacement waits 1 second, doubling per such death up to 30, and after three in a row the master logs an error naming the last exit status. The master keeps supervising either way.

Redis and the app

  • Give Torque's Redis a maxmemory and set maxmemory-policy noeviction. With eviction on, a runaway writer silently evicts other keys; with noeviction the writer gets OOM command not allowed instead. Torque's own keys are all bounded.
  • Prune failed_jobs too. The dead-letter cap bounds Redis, not your database: Schedule::command('queue:prune-failed --hours=168')->daily();.
  • Optionally schedule torque:prune hourly. The master runs the same housekeeping every dead_letter.prune_interval seconds, but a scheduled run covers a fleet that is down while the scheduler still runs.
  • The Redis user needs EVAL and EVALSHA.

Architecture

Master Process (torque:start)
+-- Worker 1 (Fledge event loop)
| +-- 50 Fibers (non-blocking poll + yield)
| +-- Redis Pool
| +-- MySQL Pool
| +-- HTTP Pool
+-- Worker 2
+-- Worker N
+-- AutoScaler (optional)
 
Redis Streams
+-- torque:{default} (XREADGROUP consumer groups)
+-- torque:{default}:delayed (sorted set, cluster-safe)
+-- torque:stream:dead-letter
+-- torque:worker:* (per-worker stats with heartbeat TTL)
+-- torque:job:* (per-job event streams, auto-expiring)
  1. The master spawns N workers with pcntl_fork() followed at once by pcntl_exec() (php artisan torque:worker), so every worker is a fresh PHP process that inherits no event loop, Fiber or connection state.
  2. Each worker installs the configured event loop driver and runs it with M Fiber slots.
  3. Each Fiber polls with non-blocking XREADGROUP (no BLOCK). With no work available it yields to the event loop for poll_interval_ms, so timers always fire.
  4. Fiber startup is staggered across the poll interval.
  5. Idle Fibers claim stale messages from dead consumers through XAUTOCLAIM, with the per-queue retry_after as idle threshold.
  6. On completion: XACK plus XDEL. On failure: retry with backoff or dead-letter.
  7. A shared pause flag, updated by a timer, replaces per-Fiber Redis checks.

Redis Streams, not lists, give consumer groups, acknowledgement, a native pending entries list, non-blocking reads and hash-tagged keys for cluster mode.

Not planned

These Horizon features have no Torque equivalent:

Horizon feature Why not
Per-environment supervisors One master with one slot pool. Per-stream max_concurrency, prefix isolation and env() in config/torque.php cover the main uses
Queue grouping and per-supervisor pause Same single pool. Pause a stream with queue:pause, or all of Torque with torque:pause
Tag monitoring with retention Not planned
Fast termination torque:reload takes over with a new fleet while the old one drains