laravel queue (retry jobs 5 times) & mark job as failed "manually"

1.3k Views Asked by At

I've got a queue running some callback requests to endpoint of my users.

Here's the code of my queue.

    public function handle()
{
    //send webrequest here....

//check the response of user backend
    if ($res->getStatusCode() != 200 || $res->getBody()->getContents() != "*received*")
        throw new Exception('callback url not reachable');
}

public function failed(Exception $exception)
{
    //check tries and try again if needed

//check if job failed for 5 times
//if not ->retry again in 5 minutes, increment the times tried
//if yes ->disable API access, send email

    Log::info("user email send,  callback disabled!");
}

How to let the job fail (my current exception makes the whole job end), when webrequest answer is != "received" and check if the certain job failed 5 times? If a job failed it should be tried again in 5 minutes.

I don't understand the doc regarding these points.

1

There are 1 best solutions below

0
On

I know it's too late but yesterday I had the same problem with the queue and when I throw a new exception the queue run it again and again until my VM hanged up. actually, you did right the only thing you have to add to your code is define a tries variable to the public area of your job class and it should run only for example 5 times and pushed to failed job ;)

class NewCustomer implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $tries = 5;

. . .