You push a deploy at 2 PM. Your CI pipeline runs php artisan horizon:terminate to gracefully shut down Horizon before swapping code. The deploy finishes, Horizon restarts, and everything looks normal. Except it is not. Two hours later, customers report that emails are delayed by 40 minutes. You check Redis and find 8,000 jobs piled up. The old Horizon master never actually terminated. It is still holding worker processes on the previous code revision, competing with the new master for Redis keys. Half of your workers are running stale code, silently failing jobs that depend on a new database column added in the deploy.
This is the core risk of horizon:terminate in production: it only asks Horizon to stop. It sends SIGTERM to the master supervisors running on the machine where you ran it, and each master then waits for its workers. If you do not verify that the shutdown finished, your deploy pipeline is built on an assumption that may not hold.
How horizon:terminate Works Internally
Per the Horizon source, the command does two things:
- It sends
SIGTERMto every Horizon master supervisor process on the machine where the command runs. - It writes an
illuminate:queue:restarttimestamp to the cache, so any plainqueue:workdaemons restart as well.
It does not write a termination flag for the master to poll. The signal goes to the process. When the master receives it, it stops working, removes its own record from Redis, tells each supervisor to terminate, waits for them, and exits.
Two details decide how long a deploy waits:
- The wait is bounded. The master waits up to the longest supervisor
timeout, and Horizon force-stops workers that are still running once theirtimeouthas passed since termination began. A 30-minute job does not hold a deploy for 30 minutes unless that supervisor’stimeoutallows it. fast_terminationchanges who waits. With'fast_termination' => trueinconfig/horizon.php, supervisors do not wait for workers to exit unless you runhorizon:terminate --wait. That lets a new master start sooner, but old workers can still be finishing jobs on old code.
Useful checks around a deploy:
# Exit code 0 = running, 1 = paused, 2 = inactive (no master records)
php artisan horizon:status; echo "exit=$?"
# List master supervisors (more than one for an environment means overlap)
php artisan horizon:list
# Are any Horizon processes (master or workers) still alive on this machine?
pgrep -fl "artisan horizon"
horizon:status is not a reliable “has it finished” check. The master removes its record from Redis as soon as it starts terminating, so horizon:status can report inactive while workers are still draining. To know the old code is gone, check the processes.
The Deploy Race Condition
The most dangerous failure pattern is not that horizon:terminate fails to run. It is that the old workers are still alive when new code goes live. A typical deploy script looks like this:
php artisan horizon:terminate
php artisan migrate --force
php artisan config:cache
Nothing waits between the signal and the next step. Usually your process manager (Supervisor, systemd) restarts php artisan horizon once the old master exits, so the new master starts on its own. In between, old workers can run jobs against a schema or config that has already changed.
A safer sequence waits for the old processes to exit before the deploy continues:
php artisan horizon:terminate || { echo "terminate failed"; exit 1; }
# Wait for the old master and workers to exit (give up after 60s)
SECONDS=0
while pgrep -f "artisan horizon" >/dev/null; do
if [ "$SECONDS" -ge 60 ]; then
echo "Horizon still running after 60s; check job timeouts" >&2
exit 1
fi
sleep 1
done
php artisan migrate --force
php artisan config:cache
If your process manager restarts Horizon immediately, the pgrep check can see the new master and loop until the deadline. In that case stop the Horizon program in your process manager before the wait, or only count processes older than the deploy.
The deadline matters. Without it, one long job can stall the deploy indefinitely. Failing loudly beats running two generations of code side by side.
When horizon:terminate Silently Fails
The command can exit with code 0 and still leave the old Horizon running:
- Another server. It signals only masters on the machine it runs on. On a multi-server deployment, run it on every server that runs Horizon, or the other servers keep their old master and workers.
- Nothing to signal. If no master is running, there is nothing to stop. The command still writes the queue-restart timestamp and exits 0.
- A different user. A process can only signal processes it is allowed to. If the deploy runs as
deployand Horizon runs aswww-data, the signal can be refused without the deploy failing. - Redis connectivity. The command looks up masters through Redis. If it cannot connect it throws, but a CI runner that ignores exit codes never shows you the error:
php artisan horizon:terminate || { echo "Terminate failed"; exit 1; }
Monitoring Terminate Across Deploys
Process managers like Supervisor will tell you if php artisan horizon is running. They will not tell you if a terminate signal was sent, ignored, or took 90 seconds to process. This is a gap that requires application-level monitoring.
What to watch for:
- Time between terminate and process exit. If this consistently exceeds 30 seconds, you have long-running jobs that need
--timeoutadjustments or should move to a dedicated queue with shorter limits. - Overlapping master supervisors. More than one entry in
horizon:listafter a deploy means terminate did not finish in time. - Queue depth spikes during deploys. A healthy terminate/restart cycle causes a brief pause in processing. If queue depth grows for more than 2 to 3 minutes post-deploy, something went wrong.
Crontinel monitors Horizon’s heartbeat from outside your infrastructure. If a terminate takes too long and the new master fails to come up, Crontinel catches the gap and alerts you before queue depth becomes a customer-visible problem.
Setting Job Timeouts to Protect Deploys
The single most effective thing you can do to make horizon:terminate reliable is to set aggressive job timeouts. In your config/horizon.php:
'environments' => [
'production' => [
'supervisor-default' => [
'timeout' => 30,
'maxProcesses' => 8,
'queue' => ['default', 'notifications'],
],
'supervisor-long' => [
'timeout' => 300,
'maxProcesses' => 2,
'queue' => ['reports', 'exports'],
],
],
],
Split long-running jobs onto a separate supervisor with fewer workers. The default supervisor terminates quickly (workers are stopped once the 30-second timeout passes), so your deploy is not held up by a single PDF export that takes 4 minutes. The long-running supervisor can take its time shutting down, but it only has 2 workers, limiting the blast radius.
This separation also makes monitoring clearer. When you see the default supervisor terminate in 2 seconds but the long-running supervisor take 90 seconds, that is expected behavior, not an alert. Without the split, a 90-second terminate looks the same whether it is caused by one slow job or a systemic problem.