šŸ”„ Retry All Failed Jobs in Laravel Horizon

Kodeas
2 min readMar 13, 2021

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.

Explosion in your queues ;p

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. šŸ˜±

Retry, Retry, Retryā€¦

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!

--

--