Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Allow dynamically customizing connection for queued event listener #38005

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/Illuminate/Events/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -549,9 +549,11 @@ protected function queueHandler($class, $method, $arguments)
{
[$listener, $job] = $this->createListenerAndJob($class, $method, $arguments);

$connection = $this->resolveQueue()->connection(
$listener->connection ?? null
);
$connectionName = method_exists($listener, 'viaConnection')
? $listener->viaConnection()
: $listener->connection ?? null;

$connection = $this->resolveQueue()->connection($connectionName);

$queue = method_exists($listener, 'viaQueue')
? $listener->viaQueue()
Expand Down
32 changes: 32 additions & 0 deletions tests/Events/QueuedEventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,23 @@ public function testQueueIsSetByGetQueue()
$fakeQueue->assertPushedOn('some_other_queue', CallQueuedListener::class);
}

public function testQueueIsSetByGetConnection()
{
$d = new Dispatcher;
$queue = m::mock(Queue::class);

$queue->shouldReceive('connection')->once()->with('some_other_connection')->andReturnSelf();

$queue->shouldReceive('pushOn')->once()->with(null, m::type(CallQueuedListener::class));

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

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

public function testQueuePropagateRetryUntilAndMaxExceptions()
{
$d = new Dispatcher;
Expand Down Expand Up @@ -123,6 +140,21 @@ public function viaQueue()
}
}

class TestDispatcherGetConnection implements ShouldQueue
{
public $connection = 'my_connection';

public function handle()
{
//
}

public function viaConnection()
{
return 'some_other_connection';
}
}

class TestDispatcherOptions implements ShouldQueue
{
public $maxExceptions = 1;
Expand Down