Prevent queued job from next attempt after specific error

Viewed 20

Our jobs collect data from external APIs. If one of the jobs errors out because we unexpectedly reach the API daily limit (i.e. HTTP status 429) it is pointless to retry the job again, or even process any similar jobs, till next day. Is there a way to prevent the current job to be attempted again after a specific event occurs? Ideally I should be able for example to set a flag in the failed job so I can check it on the next attempt (like suggested here)

Edit: I incorrectly referred to the jobs I didn't want to retry as "failed", however I meant if an error (exception) occurs during the API call. I edited the question.

1 Answers

It turns out the solution I was looking for is obvious: just fail() the job:

public function handle()
{
    try {
        // execute job stuff and throw a custom
        // exception if a certain event occurs
    } catch (MyCustomException $e) {
        $this->fail($e); // it won't be put back in the queue
    }
}
Related