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):

Terminal window
php artisan horizon:status # running, paused or inactive
php artisan horizon:pause # stop taking new jobs, finish the current ones
php artisan horizon:continue
php artisan horizon:terminate # exit after the current jobs; your process manager starts it again

horizon: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:

Terminal window
php artisan queue:failed # ID, connection, queue, class, when it failed
php artisan queue:retry 5c5d0a9e-... # push one back onto its queue
php artisan queue:retry all
php artisan queue:forget 5c5d0a9e-... # delete one
php artisan queue:flush # delete all of them

queue:failed shows which job failed but not why. The exception is in the table, so ask for it, for the newest failure:

Terminal window
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:

Terminal window
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.

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.