Skip to content

Commit 4cc4189

Browse files
authored
[8.x] Support maxExceptions option on queued listeners (#36891)
* Support maxExceptions option on queued listeners * Add a test to validate that maxExceptions is propagated when used with retryUntil * Apply fixes from styleci
1 parent ab3596b commit 4cc4189

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

src/Illuminate/Events/CallQueuedListener.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ class CallQueuedListener implements ShouldQueue
4040
*/
4141
public $tries;
4242

43+
/**
44+
* The maximum number of exceptions allowed, regardless of attempts.
45+
*
46+
* @var int
47+
*/
48+
public $maxExceptions;
49+
4350
/**
4451
* The number of seconds to wait before retrying a job that encountered an uncaught exception.
4552
*

src/Illuminate/Events/Dispatcher.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,8 @@ protected function propagateListenerOptions($listener, $job)
591591
return tap($job, function ($job) use ($listener) {
592592
$job->tries = $listener->tries ?? null;
593593

594+
$job->maxExceptions = $listener->maxExceptions ?? null;
595+
594596
$job->backoff = method_exists($listener, 'backoff')
595597
? $listener->backoff() : ($listener->backoff ?? null);
596598

tests/Events/QueuedEventsTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,24 @@ public function testQueueIsSetByGetQueue()
6666

6767
$fakeQueue->assertPushedOn('some_other_queue', CallQueuedListener::class);
6868
}
69+
70+
public function testQueuePropagateRetryUntilAndMaxExceptions()
71+
{
72+
$d = new Dispatcher;
73+
74+
$fakeQueue = new QueueFake(new Container);
75+
76+
$d->setQueueResolver(function () use ($fakeQueue) {
77+
return $fakeQueue;
78+
});
79+
80+
$d->listen('some.event', TestDispatcherOptions::class.'@handle');
81+
$d->dispatch('some.event', ['foo', 'bar']);
82+
83+
$fakeQueue->assertPushed(CallQueuedListener::class, function ($job) {
84+
return $job->maxExceptions === 1 && $job->retryUntil !== null;
85+
});
86+
}
6987
}
7088

7189
class TestDispatcherQueuedHandler implements ShouldQueue
@@ -104,3 +122,18 @@ public function viaQueue()
104122
return 'some_other_queue';
105123
}
106124
}
125+
126+
class TestDispatcherOptions implements ShouldQueue
127+
{
128+
public $maxExceptions = 1;
129+
130+
public function retryUntil()
131+
{
132+
return now()->addHour(1);
133+
}
134+
135+
public function handle()
136+
{
137+
//
138+
}
139+
}

0 commit comments

Comments
 (0)