From 5f03bafdba637704e93bccf60d1387a99b9143e1 Mon Sep 17 00:00:00 2001 From: Alexandre D'Eschambeault Date: Tue, 6 Apr 2021 13:09:09 -0400 Subject: [PATCH 1/3] Support maxExceptions option on queued listeners --- src/Illuminate/Events/CallQueuedListener.php | 7 +++++++ src/Illuminate/Events/Dispatcher.php | 2 ++ 2 files changed, 9 insertions(+) diff --git a/src/Illuminate/Events/CallQueuedListener.php b/src/Illuminate/Events/CallQueuedListener.php index 9201d858613b..6a39008520c8 100644 --- a/src/Illuminate/Events/CallQueuedListener.php +++ b/src/Illuminate/Events/CallQueuedListener.php @@ -40,6 +40,13 @@ class CallQueuedListener implements ShouldQueue */ public $tries; + /** + * The maximum number of exceptions allowed, regardless of attempts. + * + * @var int + */ + public $maxExceptions; + /** * The number of seconds to wait before retrying a job that encountered an uncaught exception. * diff --git a/src/Illuminate/Events/Dispatcher.php b/src/Illuminate/Events/Dispatcher.php index 96f08f01b514..ca8d86f78732 100755 --- a/src/Illuminate/Events/Dispatcher.php +++ b/src/Illuminate/Events/Dispatcher.php @@ -591,6 +591,8 @@ protected function propagateListenerOptions($listener, $job) return tap($job, function ($job) use ($listener) { $job->tries = $listener->tries ?? null; + $job->maxExceptions = $listener->maxExceptions ?? null; + $job->backoff = method_exists($listener, 'backoff') ? $listener->backoff() : ($listener->backoff ?? null); From d8799f1c6c5fc7e32ed8267e0df5a03610715ede Mon Sep 17 00:00:00 2001 From: Alexandre D'Eschambeault Date: Tue, 6 Apr 2021 13:34:02 -0400 Subject: [PATCH 2/3] Add a test to validate that maxExceptions is propagated when used with retryUntil --- tests/Events/QueuedEventsTest.php | 34 +++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests/Events/QueuedEventsTest.php b/tests/Events/QueuedEventsTest.php index 134432c291c0..b3cbe77469e2 100644 --- a/tests/Events/QueuedEventsTest.php +++ b/tests/Events/QueuedEventsTest.php @@ -7,6 +7,7 @@ use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Events\CallQueuedListener; use Illuminate\Events\Dispatcher; +use Illuminate\Support\Carbon; use Illuminate\Support\Testing\Fakes\QueueFake; use Mockery as m; use PHPUnit\Framework\TestCase; @@ -66,6 +67,24 @@ public function testQueueIsSetByGetQueue() $fakeQueue->assertPushedOn('some_other_queue', CallQueuedListener::class); } + + public function testQueuePropagateRetryUntilAndMaxExceptions() + { + $d = new Dispatcher; + + $fakeQueue = new QueueFake(new Container); + + $d->setQueueResolver(function () use ($fakeQueue) { + return $fakeQueue; + }); + + $d->listen('some.event', TestDispatcherOptions::class.'@handle'); + $d->dispatch('some.event', ['foo', 'bar']); + + $fakeQueue->assertPushed(CallQueuedListener::class, function ($job) { + return $job->maxExceptions === 1 && $job->retryUntil !== null; + }); + } } class TestDispatcherQueuedHandler implements ShouldQueue @@ -104,3 +123,18 @@ public function viaQueue() return 'some_other_queue'; } } + +class TestDispatcherOptions implements ShouldQueue +{ + public $maxExceptions = 1; + + public function retryUntil() + { + return now()->addHour(1); + } + + public function handle() + { + // + } +} From 50a91226cb3e8132972d6257a3ad5c9022b813b7 Mon Sep 17 00:00:00 2001 From: Alexandre D'Eschambeault Date: Tue, 6 Apr 2021 13:37:00 -0400 Subject: [PATCH 3/3] Apply fixes from styleci --- tests/Events/QueuedEventsTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Events/QueuedEventsTest.php b/tests/Events/QueuedEventsTest.php index b3cbe77469e2..c34f7cb006d9 100644 --- a/tests/Events/QueuedEventsTest.php +++ b/tests/Events/QueuedEventsTest.php @@ -7,7 +7,6 @@ use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Events\CallQueuedListener; use Illuminate\Events\Dispatcher; -use Illuminate\Support\Carbon; use Illuminate\Support\Testing\Fakes\QueueFake; use Mockery as m; use PHPUnit\Framework\TestCase;