简体   繁体   中英

How to retry job programmatically in laravel queue?

How can I check if a job fails to finish?
And how can I tell the job to retry inside job's handle function?
Or how can I force a job to be considered as a failed job?

I figured out that the code needs to fail to run (or throw an exception) to be considered as failed job.
I just put a throw new Exception() where I needed.
Then laravel behaves it as a failed job.

You can implement the failed function in your Job.

<?php

namespace App\Jobs;

.......

    public function failed(\Exception $exception)
    {
        \Log::info('job failed');
    }
}

And how can I tell the job to retry inside job's handle function?

Not sure if applicable to other connection types but if you are using Redis, job can be released back to the queue right from the handle() body by command:

$this->release(10);

Where 10 is delay in seconds, after which it should be returned back to the queue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM