Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Job $this->release() with honoring the backoff#58100

Unanswered
hsleewis asked this question inQ&A
Discussion options

Sometimes I don't want to throw an exception because that ends up being noise in sentry.
Is there a way to manually srelease a job back to the queue while using the backoff?

Right now I do this:

$backoff = method_exists($this, 'backoff') ? $this->backoff() : ($this->backoff ?? 60);$backoff = is_array($backoff) ? $backoff : [$backoff];$idx = min(count($backoff) - 1, max(0, $this->attempts() - 1));$this->release((int) ($backoff[$idx] ?? 60) + random_int(0, 6)); // add small jitter to prevent overloading on race conditions

Is there a better way?

You must be logged in to vote

Replies: 1 comment

Comment options

Short answer: No, Laravel doesn't have a built-in method for this.

When you call$this->release() manually, you need to handle the backoff delay calculation yourself. The automatic backoff handling in Laravel is only triggered when a job fails (throws an exception).

You could:

// In your job classprotectedfunctionreleaseWithBackoff():void{$delay =$this->getBackoffDelay();$this->release($delay);}protectedfunctiongetBackoffDelay():int{$backoff =method_exists($this,'backoff') ?$this->backoff() : [];if (is_array($backoff)) {// Use attempt - 1 since attempts() is 1-indexedreturn$backoff[$this->attempts() -1] ??end($backoff) ?:0;    }return (int)$backoff;}

Or create a reusable trait:

trait ReleasesWithBackoff{protectedfunctionreleaseWithBackoff(?int$jitter =null):void    {$delay =$this->calculateBackoffDelay();if ($jitter) {$delay +=random_int(0,$jitter);        }$this->release($delay);    }protectedfunctioncalculateBackoffDelay():int    {$backoff =method_exists($this,'backoff') ?$this->backoff() : [];$attempt =$this->attempts();if (is_array($backoff)) {return$backoff[$attempt -1] ??end($backoff) ?:0;        }return (int)$backoff;    }}
You must be logged in to vote
0 replies
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
2 participants
@hsleewis@heathdutton

[8]ページ先頭

©2009-2025 Movatter.jp