Laravel Horizon is a fantastic tool, which I use in all my projects for my queues.
If you are using Horizon, Iām sure youāve noticed that there are 2 separate places for the failed jobs: in database `failed_jobs_table` and and in Redis. The failed jobs you see in your Horizon dashboard are the ones in Redis.
When something goes wrong
Itās inevitable to have everything in perfect harmony, so it happens! Jobs do fail in your queues.
Letās say a 3rd party api in our job went down. Now we have 100 failed jobs in our Horizon dashboard as well as in failed_jobs_table in database.
Long story short, now we need to retry them.
Option one is to click the retry button in Horizon dashboard one-by-one. š±
However, there is a nicer way!
If you try running this command, it gives you failed jobs in your Redis (so Horizon)
use use Laravel\Horizon\Contracts\JobRepository;$failedJobs = app(JobRepository::class)->getFailed();
Nice! Now that we got failed jobs, we can just retry them through Horizonās RetryFailedJob class:
use Laravel\Horizon\Jobs\RetryFailedJob;dispatch(new RetryFailedJob($uuid));
And easy enough, you can delete your retried jobs in your database for having redis & database failed jobs in sync;
\DB::table('failed_jobs')->where('uuid', $uuid)->delete();
Sweet! Now we have all the parts we need of the puzzle.
Letās bring them together:
I love to create an artisan command for this task
php artisan make:command RetryFailedJobsViaHorizon
Thatās it really; and you can expand it as you wish!
Two ways I improved it are: 1) Added ājob-nameā argument to run only specific jobs; 2) Added ādateā argument to run failed jobs on specific date.
Hope you enjoyed this post. If you did, please give me a š and if you want to see more of these kind of posts, donāt forget to follow me š.
See you!