Skip to content

[Messenger] Filter out non-consumable receivers when registering ConsumeMessagesCommand #59198

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

Merged
merged 1 commit into from
Feb 26, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -2282,13 +2282,17 @@ private function registerMessengerConfiguration(array $config, ContainerBuilder
$transportRateLimiterReferences = [];
foreach ($config['transports'] as $name => $transport) {
$serializerId = $transport['serializer'] ?? 'messenger.default_serializer';
$tags = [
'alias' => $name,
'is_failure_transport' => \in_array($name, $failureTransports),
];
if (str_starts_with($transport['dsn'], 'sync://')) {
$tags['is_consumable'] = false;
}
$transportDefinition = (new Definition(TransportInterface::class))
->setFactory([new Reference('messenger.transport_factory'), 'createTransport'])
->setArguments([$transport['dsn'], $transport['options'] + ['transport_name' => $name], new Reference($serializerId)])
->addTag('messenger.receiver', [
'alias' => $name,
'is_failure_transport' => \in_array($name, $failureTransports),
])
->addTag('messenger.receiver', $tags)
;
$container->setDefinition($transportId = 'messenger.transport.'.$name, $transportDefinition);
$senderAliases[$name] = $transportId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ protected function interact(InputInterface $input, OutputInterface $output)
$io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output);

if ($this->receiverNames && !$input->getArgument('receivers')) {
if (1 === \count($this->receiverNames)) {
$input->setArgument('receivers', $this->receiverNames);

return;
}

$io->block('Which transports/receivers do you want to consume?', null, 'fg=white;bg=blue', ' ', true);

$io->writeln('Choose which receivers you want to consume messages from in order of priority.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ private function registerReceivers(ContainerBuilder $container, array $busIds):
}
}

$consumableReceiverNames = [];
foreach ($container->findTaggedServiceIds('messenger.receiver') as $id => $tags) {
$receiverClass = $this->getServiceClass($container, $id);
if (!is_subclass_of($receiverClass, ReceiverInterface::class)) {
Expand All @@ -289,6 +290,9 @@ private function registerReceivers(ContainerBuilder $container, array $busIds):
$failureTransportsMap[$tag['alias']] = $receiverMapping[$id];
}
}
if (!isset($tag['is_consumable']) || $tag['is_consumable'] !== false) {
$consumableReceiverNames[] = $tag['alias'] ?? $id;
}
}
}

Expand All @@ -314,7 +318,7 @@ private function registerReceivers(ContainerBuilder $container, array $busIds):
$consumeCommandDefinition->replaceArgument(0, new Reference('messenger.routable_message_bus'));
}

$consumeCommandDefinition->replaceArgument(4, array_values($receiverNames));
$consumeCommandDefinition->replaceArgument(4, $consumableReceiverNames);
try {
$consumeCommandDefinition->replaceArgument(6, $busIds);
} catch (OutOfBoundsException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\AttributeAutoconfigurationPass;
use Symfony\Component\DependencyInjection\Compiler\ResolveChildDefinitionsPass;
Expand Down Expand Up @@ -506,6 +507,34 @@ public function testItSetsTheReceiverNamesOnTheSetupTransportsCommand()
$this->assertSame(['amqp', 'dummy'], $container->getDefinition('console.command.messenger_setup_transports')->getArgument(1));
}

public function testOnlyConsumableTransportsAreAddedToConsumeCommand()
{
$container = new ContainerBuilder();

$container->register('messenger.transport.async', DummyReceiver::class)
->addTag('messenger.receiver', ['alias' => 'async']);
$container->register('messenger.transport.sync', DummyReceiver::class)
->addTag('messenger.receiver', ['alias' => 'sync', 'is_consumable' => false]);
$container->register('messenger.receiver_locator', ServiceLocator::class)
->setArguments([[]]);

$container->register('console.command.messenger_consume_messages', Command::class)
->setArguments([
null,
null,
null,
null,
[],
]);

(new MessengerPass())->process($container);

$this->assertSame(
['async'],
$container->getDefinition('console.command.messenger_consume_messages')->getArgument(4)
);
}

/**
* @group legacy
*/
Expand Down
Loading