Bash One-Liner: Retry Laravel Failed Jobs, Filtered by Date
We all have those moments when Queue jobs fail. Sometimes it’s a bad deploy,
others it’s an upstream service that’s taken a poop. Sometimes, we need to
retry failed jobs, but can’t just artisan queue retry:all
, because maybe we
haven’t done a cleanup of failed jobs lately.
Oops…
In my case today, I was running some maintenance jobs overnight, when I overworked a legacy database and caused about ~2500 of 1m+ jobs to timeout. Not a big problem, but the jobs needed to be retried.
A colleague watching me do it, informed me that last time they did something like this (I sincerely hope it wasn’t 2500 failed jobs) they typed all the numbers in manually!
So, down to the point, the one-liner:
Let’s break that down
Basics
$( ... )
- Command substitution allows you to insert the results of the contents into your command
|
- The classic bash pipe sends the output of one command to the input of the next
The commands used
php artisan queue:retry $( ... )
- Run
artisan queue:retry
, we’ll end up giving it a list of IDs for the jobs to retry
- Run
php artisan queue:failed
- Get the listing of failed jobs
grep 2017-08-17
- Search for the date we want to filter by in each line
awk '{print $2}'
- awk will by default split on spaces, so print id which is in the second captured group (the
|
from the beginning of the table will be in the first position)
- awk will by default split on spaces, so print id which is in the second captured group (the
Other uses
Change the grep
, and you can filter by a lot more than date.
grep -v
will inverse filter, so you can run everything except a given dategrep <job name>
- filter only specific classesgrep <queue name>
- filter specific queues, or connections