Laravel Queue and Jobs - How to re run failed job later on

Viewed 8482

Thank you in advance

My question is simple

For example, I have created one job which is sending an email to the user. however it is failed due to some syntax error or any other exception, so laravel will consider this job as a failed job in the failed_jobs database table, later on, let's say for an example after 2 days I found the issue in the code and I resolved it, now I want to re-run that failed job again so that my task can be complete

What is the way to do that?

4 Answers

You may view all your failed jobs using php artisan queue:failed

If you only want to retry 1 job, you can use the following (replace 5 with the ID of the job you want to retry) php artisan queue:retry 5

If you want to retry multiple jobs, you can use the following (replace the numbers with the IDs of the jobs you want to retry) php artisan queue:retry 5 6 7 8 9 10

If you want to retry a range of jobs with numeric IDs, you can use the following (replace the 5 and 10 with the range you want to retry) php artisan queue:retry --range=5-10

Finally, if you want to retry all failed jobs, you can use the following php artisan queue:retry all

These can be found here, be sure to check the docs next time:)

You can run a job more than 1 time

php artisan queue:work --sleep=1 --tries=5 --timeout=60

OR

You can run all failed jobs

php artisan queue:retry all

It's worth noting here that jobs are serialised when pushed to the queue. Therefore if the syntax error was in the job code itself then that job will always fail.

This is because the same job payload is entered into the failed_jobs table so subsequent retries of that job will result in the same error.

Just in case you need to retry failed jobs on a specific queue, you can check the answer here

Related