Skip to content

Commit

Permalink
[8.x] Support maxExceptions option on queued listeners (#36891)
Browse files Browse the repository at this point in the history
* Support maxExceptions option on queued listeners

* Add a test to validate that maxExceptions is propagated when used with retryUntil

* Apply fixes from styleci
  • Loading branch information
xel1045 authored Apr 6, 2021
1 parent ab3596b commit 4cc4189
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Illuminate/Events/CallQueuedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Events/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
33 changes: 33 additions & 0 deletions tests/Events/QueuedEventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,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
Expand Down Expand Up @@ -104,3 +122,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()
{
//
}
}

0 comments on commit 4cc4189

Please sign in to comment.