Skip to content

[Messenger] Be able to start a worker for multiple queues with custom consumption priorities #46334

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

Open
wants to merge 3 commits into
base: 7.4
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/Symfony/Component/Messenger/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CHANGELOG
* Add `SerializedMessageStamp` to avoid serializing a message when a retry occurs
* Automatically resolve handled message type when method different from `__invoke` is used as handler
* Allow `#[AsMessageHandler]` attribute on methods
* Customization of the receivers priorities thanks to a specification in the command argument `bin/console messenger:consume async_redis^2 async_doctrine^1`

6.0
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ protected function interact(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
$receivers = [];
foreach ($receiverNames = $input->getArgument('receivers') as $receiverName) {
$receiverNames = $this->reorderReceivers($input->getArgument('receivers'));
foreach ($receiverNames as $receiverName) {
if (!$this->receiverLocator->has($receiverName)) {
$message = sprintf('The receiver "%s" does not exist.', $receiverName);
if ($this->receiverNames) {
Expand Down Expand Up @@ -262,4 +263,26 @@ private function convertToBytes(string $memoryLimit): int

return $max;
}

private function reorderReceivers(array $receiversArgument): array
{
$receiverPriorities = [];
$receiverDefault = [];

foreach ($receiversArgument as $receiverInput) {
$split = explode('^', $receiverInput);
if (2 === \count($split)) {
$receiverPriorities[(int) ($split[1])][] = $split[0];
} else {
$receiverDefault[] = $receiverInput;
}
}

ksort($receiverPriorities);
$receiverPriorities = array_reduce($receiverPriorities, function ($carry, $array) {
return array_merge($carry, $array);
}, []);

return array_merge($receiverPriorities, $receiverDefault);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,38 @@ public function testComplete(array $input, array $expectedSuggestions)
$this->assertSame($expectedSuggestions, $suggestions);
}

public function testBasicRunWithPriority()
{
$envelope = new Envelope(new \stdClass(), [new BusNameStamp('dummy-bus')]);

$receiver = $this->createMock(ReceiverInterface::class);
$receiver->expects($this->once())->method('get')->willReturn([$envelope]);

$receiverLocator = $this->createMock(ContainerInterface::class);
$receiverLocator->expects($this->once())->method('has')->with('dummy-receiver')->willReturn(true);
$receiverLocator->expects($this->once())->method('get')->with('dummy-receiver')->willReturn($receiver);

$bus = $this->createMock(MessageBusInterface::class);
$bus->expects($this->once())->method('dispatch');

$busLocator = $this->createMock(ContainerInterface::class);
$busLocator->expects($this->once())->method('has')->with('dummy-bus')->willReturn(true);
$busLocator->expects($this->once())->method('get')->with('dummy-bus')->willReturn($bus);

$command = new ConsumeMessagesCommand(new RoutableMessageBus($busLocator), $receiverLocator, new EventDispatcher());

$application = new Application();
$application->add($command);
$tester = new CommandTester($application->get('messenger:consume'));
$tester->execute([
'receivers' => ['dummy-receiver^1'],
'--limit' => 1,
]);

$tester->assertCommandIsSuccessful();
$this->assertStringContainsString('[OK] Consuming messages from transports "dummy-receiver"', $tester->getDisplay());
}

public function provideCompletionSuggestions()
{
yield 'receiver' => [[''], ['async', 'async_high', 'failed']];
Expand Down