Skip to content

Commit

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

* formatting

Co-authored-by: Taylor Otwell <taylorotwell@gmail.com>
  • Loading branch information
2 people authored and chu121su12 committed Jul 27, 2021
1 parent d4f680a commit cb897b3
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 @@ -610,22 +610,18 @@ protected function createListenerAndJob($class, $method, $arguments)
protected function propagateListenerOptions($listener, $job)
{
return tap($job, function ($job) use ($listener) {
$job->tries = isset($listener->tries) ? $listener->tries : null;

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

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

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

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

$job->afterCommit = property_exists($listener, 'afterCommit')
? $listener->afterCommit : 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 cb897b3

Please sign in to comment.