-
Notifications
You must be signed in to change notification settings - Fork 11.7k
[12.x] Introduce FailOnException job middleware
#56037
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b93121d
introduce RetryIf middleware
cosmastech 0cf5e11
test
cosmastech 3e789be
test
cosmastech a3fcdcb
test
cosmastech e59c1bf
yoooo this testing setup is more complicated than it needs to be
cosmastech 5f5e3b3
one more shot
cosmastech df50336
better name
cosmastech 435fb4d
ok, this is it
cosmastech ad72acb
move namespace
cosmastech 8bb5fe2
another method
cosmastech de43bf3
add job test
cosmastech afa7ef7
comments
cosmastech 298b9d3
rename
cosmastech b03952f
Update FailOnException.php
cosmastech 783fcc3
formatting
taylorotwell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| <?php | ||
|
|
||
| namespace Illuminate\Queue\Middleware; | ||
|
|
||
| use Closure; | ||
| use Throwable; | ||
|
|
||
| class RetryIf | ||
| { | ||
| /** | ||
| * @param \Closure(\Throwable, ?mixed): bool $retryIf The condition of the failure that will retry the job. | ||
| */ | ||
| public function __construct(protected Closure $retryIf) | ||
| { | ||
| } | ||
|
|
||
| /** | ||
| * @param class-string<\Throwable> ...$exceptions | ||
| * @return static | ||
| */ | ||
| public static function failureIsNot(...$exceptions): static | ||
| { | ||
| return new static(static function (Throwable $throwable) use ($exceptions) { | ||
| foreach ($exceptions as $exception) { | ||
| if ($throwable instanceof $exception) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * @param mixed $job | ||
| * @param callable $next | ||
| * @return mixed | ||
| * | ||
| * @throws Throwable | ||
| */ | ||
| public function handle($job, callable $next) | ||
| { | ||
| try { | ||
| return $next($job); | ||
| } catch (Throwable $e) { | ||
| if (call_user_func($this->retryIf, $e, $job) !== true) { | ||
| $job->fail($e); | ||
| } | ||
|
|
||
| throw $e; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| <?php | ||
|
|
||
| namespace Illuminate\Tests\Integration\Queue; | ||
|
|
||
| use Illuminate\Bus\Queueable; | ||
| use Illuminate\Contracts\Queue\ShouldQueue; | ||
| use Illuminate\Foundation\Bus\Dispatchable; | ||
| use Illuminate\Foundation\Testing\DatabaseMigrations; | ||
| use Illuminate\Queue\InteractsWithQueue; | ||
| use Illuminate\Queue\Middleware\RetryIf; | ||
| use Illuminate\Support\Facades\Queue; | ||
| use InvalidArgumentException; | ||
| use LogicException; | ||
| use Orchestra\Testbench\Attributes\WithConfig; | ||
| use Orchestra\Testbench\TestCase; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
|
|
||
| #[WithConfig('queue.default', 'database')] | ||
| class RetryIfMiddlewareTest extends TestCase | ||
| { | ||
| use DatabaseMigrations; | ||
|
|
||
| public static function markFailedForRetryIfDataProvider(): array | ||
| { | ||
| return [ | ||
| 'middleware fails on thrown exception' => [ | ||
| InvalidArgumentException::class, | ||
| 1, | ||
| 1, | ||
| ], | ||
| 'middleware retries if exception does not match' => [ | ||
| LogicException::class, | ||
| 2, | ||
| 1, | ||
| ], | ||
| ]; | ||
| } | ||
|
|
||
| #[DataProvider('markFailedForRetryIfDataProvider')] | ||
| public function test_retry_if_middleware( | ||
| $throws, | ||
| int $expectedExceptions, | ||
| int $expectedFails | ||
| ) { | ||
| $this->markTestSkippedWhen(config('queue.default') === 'sync', 'Does not run when sync.'); | ||
|
|
||
| RetryIfMiddlewareJob::dispatch($throws)->onQueue('default')->onConnection('database'); | ||
|
|
||
| $failsCalled = $exceptionsOccurred = 0; | ||
| Queue::exceptionOccurred(function () use (&$exceptionsOccurred) { | ||
| $exceptionsOccurred++; | ||
| }); | ||
| Queue::failing(function () use (&$failsCalled) { | ||
| $failsCalled++; | ||
| }); | ||
|
|
||
| for ($i = 0; $i < 2; $i++) { | ||
| $this->artisan('queue:work', [ | ||
| '--memory' => 1024, | ||
| '--stop-when-empty' => true, | ||
| '--sleep' => 1, | ||
| ])->assertSuccessful(); | ||
| } | ||
|
|
||
| $this->assertEquals($expectedExceptions, $exceptionsOccurred); | ||
| $this->assertEquals($expectedFails, $failsCalled); | ||
| } | ||
| } | ||
|
|
||
| class RetryIfMiddlewareJob implements ShouldQueue | ||
| { | ||
| use InteractsWithQueue; | ||
| use Queueable; | ||
| use Dispatchable; | ||
|
|
||
| public int $tries = 2; | ||
|
|
||
| public function __construct(private $throws) | ||
| { | ||
| } | ||
|
|
||
| public function handle() | ||
| { | ||
| if ($this->throws === null) { | ||
| return; // success | ||
| } | ||
|
|
||
| throw new ($this->throws); | ||
| } | ||
|
|
||
| public function middleware(): array | ||
| { | ||
| return [RetryIf::failureIsNot(InvalidArgumentException::class)]; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we have both
failureIsNotandfailureIs?In a project, I want to retry only if the exception is related to the HTTP client, but not on other errors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
I had a very similar middleware on this particular project, but your implementation is cleaner, and I already refactored it. Thanks again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was writing one this morning for a project I'm working on 😅 Good to see it's useful for more than just me.