Skip to content

Commit

Permalink
[8.x] Support job middleware on queued listeners (#38128)
Browse files Browse the repository at this point in the history
* Support job middleware on event listeners

* formatting

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
gdebrauwer and taylorotwell authored Jul 26, 2021
1 parent 0005bdc commit cf26e13
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 13 deletions.
22 changes: 9 additions & 13 deletions src/Illuminate/Events/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -589,22 +589,18 @@ protected function createListenerAndJob($class, $method, $arguments)
protected function propagateListenerOptions($listener, $job)
{
return tap($job, function ($job) use ($listener) {
$job->tries = $listener->tries ?? null;

$job->afterCommit = property_exists($listener, 'afterCommit') ? $listener->afterCommit : null;
$job->backoff = method_exists($listener, 'backoff') ? $listener->backoff() : ($listener->backoff ?? null);
$job->maxExceptions = $listener->maxExceptions ?? null;

$job->backoff = method_exists($listener, 'backoff')
? $listener->backoff() : ($listener->backoff ?? null);

$job->retryUntil = method_exists($listener, 'retryUntil') ? $listener->retryUntil() : null;
$job->shouldBeEncrypted = $listener instanceof ShouldBeEncrypted;
$job->timeout = $listener->timeout ?? null;
$job->tries = $listener->tries ?? null;

$job->afterCommit = property_exists($listener, 'afterCommit')
? $listener->afterCommit : null;

$job->retryUntil = method_exists($listener, 'retryUntil')
? $listener->retryUntil() : null;

$job->shouldBeEncrypted = $listener instanceof ShouldBeEncrypted;
$job->through(array_merge(
method_exists($listener, 'middleware') ? $listener->middleware() : [],
$listener->middleware ?? []
));
});
}

Expand Down
39 changes: 39 additions & 0 deletions tests/Events/QueuedEventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,24 @@ public function testQueuePropagateRetryUntilAndMaxExceptions()
return $job->maxExceptions === 1 && $job->retryUntil !== null;
});
}

public function testQueuePropagateMiddleware()
{
$d = new Dispatcher;

$fakeQueue = new QueueFake(new Container);

$d->setQueueResolver(function () use ($fakeQueue) {
return $fakeQueue;
});

$d->listen('some.event', TestDispatcherMiddleware::class.'@handle');
$d->dispatch('some.event', ['foo', 'bar']);

$fakeQueue->assertPushed(CallQueuedListener::class, function ($job) {
return count($job->middleware) === 1 && $job->middleware[0] instanceof TestMiddleware;
});
}
}

class TestDispatcherQueuedHandler implements ShouldQueue
Expand Down Expand Up @@ -169,3 +187,24 @@ public function handle()
//
}
}

class TestDispatcherMiddleware implements ShouldQueue
{
public function middleware()
{
return [new TestMiddleware()];
}

public function handle()
{
//
}
}

class TestMiddleware
{
public function handle($job, $next)
{
$next($job);
}
}

0 comments on commit cf26e13

Please sign in to comment.