Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2f873f1

Browse files
author
Robin Chalas
committed
[Messenger] Add a --bus option to the messenger:consume-messages command
1 parent f96753b commit 2f873f1

File tree

5 files changed

+57
-19
lines changed

5 files changed

+57
-19
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/console.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,11 @@
7070
</service>
7171

7272
<service id="console.command.messenger_consume_messages" class="Symfony\Component\Messenger\Command\ConsumeMessagesCommand">
73-
<argument type="service" id="message_bus" />
73+
<argument /> <!-- Message bus locator -->
7474
<argument type="service" id="messenger.receiver_locator" />
7575
<argument type="service" id="logger" on-invalid="null" />
7676
<argument>null</argument> <!-- Default receiver name -->
77+
<argument type="collection" /> <!-- Message buses names -->
7778

7879
<tag name="console.command" command="messenger:consume-messages" />
7980
</service>

src/Symfony/Component/Messenger/Command/ConsumeMessagesCommand.php

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use Symfony\Component\Console\Input\InputOption;
2121
use Symfony\Component\Console\Output\OutputInterface;
2222
use Symfony\Component\Console\Style\SymfonyStyle;
23-
use Symfony\Component\Messenger\MessageBusInterface;
2423
use Symfony\Component\Messenger\Transport\Enhancers\StopWhenMemoryUsageIsExceededReceiver;
2524
use Symfony\Component\Messenger\Transport\Enhancers\StopWhenMessageCountIsExceededReceiver;
2625
use Symfony\Component\Messenger\Transport\Enhancers\StopWhenTimeLimitIsReachedReceiver;
@@ -35,17 +34,19 @@ class ConsumeMessagesCommand extends Command
3534
{
3635
protected static $defaultName = 'messenger:consume-messages';
3736

38-
private $bus;
37+
private $busLocator;
3938
private $receiverLocator;
4039
private $logger;
4140
private $receiverNames;
41+
private $busNames;
4242

43-
public function __construct(MessageBusInterface $bus, ContainerInterface $receiverLocator, LoggerInterface $logger = null, array $receiverNames = array())
43+
public function __construct(ContainerInterface $busLocator, ContainerInterface $receiverLocator, LoggerInterface $logger = null, array $receiverNames = array(), array $busNames = array())
4444
{
45-
$this->bus = $bus;
45+
$this->busLocator = $busLocator;
4646
$this->receiverLocator = $receiverLocator;
4747
$this->logger = $logger;
4848
$this->receiverNames = $receiverNames;
49+
$this->busNames = $busNames;
4950

5051
parent::__construct();
5152
}
@@ -63,6 +64,7 @@ protected function configure(): void
6364
new InputOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Limit the number of received messages'),
6465
new InputOption('memory-limit', 'm', InputOption::VALUE_REQUIRED, 'The memory limit the worker can consume'),
6566
new InputOption('time-limit', 't', InputOption::VALUE_REQUIRED, 'The time limit in seconds the worker can run'),
67+
new InputOption('bus', 'b', InputOption::VALUE_REQUIRED, 'Name of the bus to which received messages should be dispatched', 'message_bus'),
6668
))
6769
->setDescription('Consumes messages')
6870
->setHelp(<<<'EOF'
@@ -91,18 +93,34 @@ protected function configure(): void
9193
*/
9294
protected function interact(InputInterface $input, OutputInterface $output)
9395
{
94-
if (!$this->receiverNames || $this->receiverLocator->has($receiverName = $input->getArgument('receiver'))) {
95-
return;
96+
$style = new SymfonyStyle($input, $output);
97+
98+
if ($this->receiverNames && !$this->receiverLocator->has($receiverName = $input->getArgument('receiver'))) {
99+
if (null === $receiverName) {
100+
$style->block('Missing receiver argument.', null, 'error', ' ', true);
101+
$input->setArgument('receiver', $style->choice('Select one of the available receivers', $this->receiverNames));
102+
} elseif ($alternatives = $this->findAlternatives($receiverName, $this->receiverNames)) {
103+
$style->block(sprintf('Receiver "%s" is not defined.', $receiverName), null, 'error', ' ', true);
104+
if ($style->confirm(sprintf('Do you want to receive from "%s" instead? ', $alternatives[0]), false)) {
105+
$input->setArgument('receiver', $alternatives[0]);
106+
}
107+
}
96108
}
97109

98-
$style = new SymfonyStyle($input, $output);
99-
if (null === $receiverName) {
100-
$style->block('Missing receiver argument.', null, 'error', ' ', true);
101-
$input->setArgument('receiver', $style->choice('Select one of the available receivers', $this->receiverNames));
102-
} elseif ($alternatives = $this->findAlternatives($receiverName, $this->receiverNames)) {
103-
$style->block(sprintf('Receiver "%s" is not defined.', $receiverName), null, 'error', ' ', true);
104-
if ($style->confirm(sprintf('Do you want to receive from "%s" instead? ', $alternatives[0]), false)) {
105-
$input->setArgument('receiver', $alternatives[0]);
110+
111+
$busName = $input->getOption('bus');
112+
113+
if ($busName && $this->busNames && !$this->busLocator->has($busName) && $alternatives = $this->findAlternatives($busName, $this->busNames)) {
114+
if ($alternatives = $this->findAlternatives($busName, $this->busNames)) {
115+
$style->block(sprintf('Bus "%s" is not defined.', $busName), null, 'error', ' ', true);
116+
117+
if (1 === \count($alternatives)) {
118+
if ($style->confirm(sprintf('Do you want to dispatch to "%s" instead? ', $alternatives[0]), true)) {
119+
$input->setOption('bus', $alternatives[0]);
120+
}
121+
} else {
122+
$input->setOption('bus', $style->choice('Did you mean one of the following buses instead?', $alternatives, $alternatives[0]));
123+
}
106124
}
107125
}
108126
}
@@ -116,7 +134,12 @@ protected function execute(InputInterface $input, OutputInterface $output): void
116134
throw new RuntimeException(sprintf('Receiver "%s" does not exist.', $receiverName));
117135
}
118136

137+
if (!$this->busLocator->has($busName = $input->getOption('bus'))) {
138+
throw new RuntimeException(sprintf('Bus "%s" does not exist.', $receiverName));
139+
}
140+
119141
$receiver = $this->receiverLocator->get($receiverName);
142+
$bus = $this->busLocator->get($busName);
120143

121144
if ($limit = $input->getOption('limit')) {
122145
$receiver = new StopWhenMessageCountIsExceededReceiver($receiver, $limit, $this->logger);
@@ -130,7 +153,7 @@ protected function execute(InputInterface $input, OutputInterface $output): void
130153
$receiver = new StopWhenTimeLimitIsReachedReceiver($receiver, $timeLimit, $this->logger);
131154
}
132155

133-
$worker = new Worker($receiver, $this->bus);
156+
$worker = new Worker($receiver, $bus);
134157
$worker->run();
135158
}
136159

src/Symfony/Component/Messenger/DependencyInjection/MessengerPass.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,18 @@ private function registerHandlers(ContainerBuilder $container, array $busIds)
184184
}
185185
$container->getDefinition('console.command.messenger_debug')->replaceArgument(0, $debugCommandMapping);
186186
}
187+
188+
if ($container->hasDefinition('console.command.messenger_consume_messages')) {
189+
$buses = array();
190+
foreach ($busIds as $busId) {
191+
$buses[$busId] = new Reference($busId);
192+
}
193+
$container
194+
->getDefinition('console.command.messenger_consume_messages')
195+
->replaceArgument(0, ServiceLocatorTagPass::register($container, $buses))
196+
->replaceArgument(4, $busIds);
197+
}
198+
187199
}
188200

189201
private function guessHandledClasses(\ReflectionClass $handlerClass, string $serviceId): iterable

src/Symfony/Component/Messenger/Tests/Command/ConsumeMessagesCommandTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ class ConsumeMessagesCommandTest extends TestCase
2020
{
2121
public function testConfigurationWithDefaultReceiver()
2222
{
23-
$command = new ConsumeMessagesCommand($this->createMock(MessageBus::class), $this->createMock(ServiceLocator::class), null, array('amqp'));
23+
$command = new ConsumeMessagesCommand($this->createMock(ServiceLocator::class), $this->createMock(ServiceLocator::class), null, array('amqp'));
2424
$inputArgument = $command->getDefinition()->getArgument('receiver');
2525
$this->assertFalse($inputArgument->isRequired());
2626
$this->assertSame('amqp', $inputArgument->getDefault());
2727
}
2828

2929
public function testConfigurationWithoutDefaultReceiver()
3030
{
31-
$command = new ConsumeMessagesCommand($this->createMock(MessageBus::class), $this->createMock(ServiceLocator::class), null, array('amqp', 'dummy'));
31+
$command = new ConsumeMessagesCommand($this->createMock(ServiceLocator::class), $this->createMock(ServiceLocator::class), null, array('amqp', 'dummy'));
3232
$inputArgument = $command->getDefinition()->getArgument('receiver');
3333
$this->assertTrue($inputArgument->isRequired());
3434
$this->assertNull($inputArgument->getDefault());

src/Symfony/Component/Messenger/Tests/DependencyInjection/MessengerPassTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,11 @@ public function testItRegistersMultipleReceiversAndSetsTheReceiverNamesOnTheComm
241241
{
242242
$container = $this->getContainerBuilder();
243243
$container->register('console.command.messenger_consume_messages', ConsumeMessagesCommand::class)->setArguments(array(
244-
new Reference('message_bus'),
244+
null,
245245
new Reference('messenger.receiver_locator'),
246246
null,
247247
null,
248+
null
248249
));
249250

250251
$container->register(AmqpReceiver::class, AmqpReceiver::class)->addTag('messenger.receiver', array('alias' => 'amqp'));
@@ -253,6 +254,7 @@ public function testItRegistersMultipleReceiversAndSetsTheReceiverNamesOnTheComm
253254
(new MessengerPass())->process($container);
254255

255256
$this->assertSame(array('amqp', 'dummy'), $container->getDefinition('console.command.messenger_consume_messages')->getArgument(3));
257+
$this->assertSame(array('message_bus'), $container->getDefinition('console.command.messenger_consume_messages')->getArgument(4));
256258
}
257259

258260
public function testItRegistersSenders()

0 commit comments

Comments
 (0)