PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, March 1, 2022

[FIXED] How can I create delays between failed Queued Job attempts in Laravel?

 March 01, 2022     laravel, laravel-5, laravel-5.1     No comments   

Issue

I have a queued job in Laravel that fails from time to time because of an external API failing due to high load. The problem is that my choices appear to be to have the Laravel Queue continue to hammer the API with requests until it succeeds or tell it to stop after X number of requests.

Is there any way for me to, based on how the job fails, tell it to try again in 5 minutes instead of continuing to hammer away?

I want to use the built in queue handler, but the retry functionality doesn't appear to be built to handle real life scenarios of failure. I would think that many reasons for failing a job wouldn't be solved by immediately trying again.


Solution

What you can do is something like this:

// app/Jobs/ExampleJob.php
namespace App\Jobs;

class ExampleJob extends Job
{
    use \Illuminate\Queue\InteractsWithQueue;

    public function handle()
    {
        try {
            // Do stuff that might fail
        } catch(AnException $e) {
            // Example where you might want to retry

            if ($this->attempts() < 3) {
                $delayInSeconds = 5 * 60;
                $this->release($delayInSeconds);
            }
        } catch(AnotherException $e) {
            // Example where you don't want to retry
            $this->delete();
        }
    }
}

Please note that you do not have to do this with exceptions, you can also just check the result from your actions and decide from there.



Answered By - Roj Vroemen
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing