Laravel Horizon failed jobs without a browser
Find out why a queued job failed, retry it or forget it from the terminal, when Horizon's dashboard is behind a gate, a VPN or not deployed at all.
Horizon’s dashboard is the nicest way to look at a Laravel queue, but on production it is often out of reach. Outside the local environment /horizon is open only to users the viewHorizon gate lets in. A worker-only server may not serve the web app at all, and an API may have no login to pass the gate with. Yet when jobs start failing at night, you want the exception and a retry button, not a login form.
Everything the dashboard does for failed jobs can be done from the terminal.
Status and control
Inside the app’s container (docker exec -it <container> sh, or prefixed to each command):
php artisan horizon:status # running, paused or inactivephp artisan horizon:pause # stop taking new jobs, finish the current onesphp artisan horizon:continuephp artisan horizon:terminate # exit after the current jobs; your process manager starts it againhorizon:terminate is also what a deploy should run, so the workers load the new code. Workers keep the old code in memory until they restart.
Failed jobs
Failed jobs are kept by Laravel’s failed job provider, the failed_jobs table by default, and Artisan reads them whatever the queue driver:
php artisan queue:failed # ID, connection, queue, class, when it failedphp artisan queue:retry 5c5d0a9e-... # push one back onto its queuephp artisan queue:retry allphp artisan queue:forget 5c5d0a9e-... # delete onephp artisan queue:flush # delete all of themqueue:failed shows which job failed but not why. The exception is in the table, so ask for it, for the newest failure:
php artisan tinker --execute="echo DB::table('failed_jobs')->latest('failed_at')->value('exception');"queue:failed lists one row per failure, so a job that broke for 400 users is 400 rows. To see which jobs fail the most, count them by class, which the payload names as displayName:
php artisan tinker --execute="DB::table('failed_jobs')->pluck('payload')->map(fn (\$p) => json_decode(\$p)->displayName)->countBy()->sortDesc()->dump();"It reads every payload, which is fine for hundreds of rows and slow for hundreds of thousands. The \$ keeps your shell from expanding $p.
What Horizon keeps on its own
Horizon also keeps its own lists in Redis: recent, pending, completed and failed jobs, with tags. They are trimmed after the times set under trim in config/horizon.php. Failed jobs stay for 10080 minutes, a week, by default. That is what the dashboard’s Failed tab shows. The Artisan commands above work on the failed job provider instead, which keeps rows until you delete them.
With laraport
laraport shows Horizon’s dashboard in the terminal: the Queues item on a container’s Laravel tab opens it when Horizon is installed.
▲ laraport › Production › shop-app-1 › Horizon ● ssh shop.example.com 1 Dashboard 2 Pending 44 3 Completed 10920 4 Failed 3 5 Silenced ● live · updated 12:45:59 Failed jobs · 3 job queue tags failed runtime exception ChargeCard payments order:50117 Nov 3 17:02 RuntimeExcep… SendInvoice emails order:50112 Nov 3 16:47 Swift_Transp… SyncStock default Nov 3 15:55 GuzzleHttp\E… 3 rows · row 1 · col 1/6 job LARAPORT Production shop-app-1 tab/1-6 tabs ↑↓ move enter job ? help
The Failed tab lists the newest failed jobs with the first line of their exception. enter opens one with its payload, the full exception, context, tags and attempts. Then:
| Key | Action |
|---|---|
r |
Retry the job |
D |
Forget it |
A |
Retry all |
F |
Forget all |
The Dashboard tab has the rest of what the web page shows: jobs per minute, wait times, workload per queue and the supervisors, updated every 3 seconds. p pauses Horizon and T terminates it so your process manager restarts it with the current code. Everything that changes something asks first, except retrying a single job and continuing Horizon.
No gate, no browser and no access to Redis are needed: laraport reads Horizon through Horizon’s own classes inside the app, over the same SSH connection you would use for docker exec.
Without Horizon, the same screen shows how many jobs wait in each queue and the failed jobs with their exceptions, and runs the queue:* commands above for you, so it works with Redis, the database or SQS alike.
More in the Queues and Horizon docs.