🔥 Retry All Failed Jobs in Laravel Horizon

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

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!

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:

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!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store