0

We are using Laravel Jobs + Horizon, and have built a framework to retry jobs with exponential backoff using the hook for failed() as well as Job middleware to deal with log consolidation, but we've not been able to solve cleanly the following problem:

The subclass job implementation experiences and throws a specific error, let's say a guzzle 429 error - in these cases, we simply want the job be started over, with delay - so we don't want the job to fail, we don't want it to complete, we just want to redispatch it - but the only way we detect this is in failed, which appears to be too late.

Is there a mechanism to catch these earlier than the failed hook, or a mechanism to unfail them?

1 Answer 1

0

You could just catch the error when it happens and intentionally release for retry. Note this will increment the attempts though.

public function handle(): void
{
    // ... some business logic ...
    try{
        $this->someMethod();
    }catch(GuzzleException $e){
        throw_if($e->statusCode() !== 429, $e);

        $this->release($this->releaseDelay);
        return;
    }
    // ... some business logic ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

So the issue with this approach is it would be have to be implemented in every single job class as opposed to the parent job class - unless there is a way to override handle() ?
You could use a intermediatary class to extend the base job and implement a handle() like the above that catches some ReleasableException and extends children's ->someMethod() to do the actual logic. Possiblty something spicy with Annotations too but that's a bit needlessly complex for my taste.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.