Skip to content
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 @@ -108,6 +108,7 @@
use Symfony\Component\Messenger\Bridge\Amqp\Transport\AmqpTransportFactory;
use Symfony\Component\Messenger\Bridge\Beanstalkd\Transport\BeanstalkdTransportFactory;
use Symfony\Component\Messenger\Bridge\Redis\Transport\RedisTransportFactory;
use Symfony\Component\Messenger\Command\StatsCommand;
use Symfony\Component\Messenger\Handler\BatchHandlerInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
use Symfony\Component\Messenger\MessageBus;
Expand Down Expand Up @@ -503,6 +504,7 @@ public function load(array $configs, ContainerBuilder $container)
$this->registerMessengerConfiguration($config['messenger'], $container, $loader, $config['validation']);
} else {
$container->removeDefinition('console.command.messenger_consume_messages');
$container->removeDefinition('console.command.messenger_stats');
$container->removeDefinition('console.command.messenger_debug');
$container->removeDefinition('console.command.messenger_stop_workers');
$container->removeDefinition('console.command.messenger_setup_transports');
Expand Down Expand Up @@ -1966,6 +1968,10 @@ private function registerMessengerConfiguration(array $config, ContainerBuilder
throw new LogicException('Messenger support cannot be enabled as the Messenger component is not installed. Try running "composer require symfony/messenger".');
}

if (!class_exists(StatsCommand::class)) {
$container->removeDefinition('console.command.messenger_stats');
}

$loader->load('messenger.php');

if (!interface_exists(DenormalizerInterface::class)) {
Expand Down
10 changes: 9 additions & 1 deletion src/Symfony/Bundle/FrameworkBundle/Resources/config/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use Symfony\Component\Console\EventListener\ErrorListener;
use Symfony\Component\Dotenv\Command\DebugCommand as DotenvDebugCommand;
use Symfony\Component\Messenger\Command\ConsumeMessagesCommand;
use Symfony\Component\Messenger\Command\StatsCommand;
use Symfony\Component\Messenger\Command\DebugCommand;
use Symfony\Component\Messenger\Command\FailedMessagesRemoveCommand;
use Symfony\Component\Messenger\Command\FailedMessagesRetryCommand;
Expand Down Expand Up @@ -206,6 +207,13 @@
])
->tag('console.command')

->set('console.command.messenger_stats', StatsCommand::class)
->args([
service('messenger.receiver_locator'),
abstract_arg('Receivers names'),
])
->tag('console.command')

->set('console.command.router_debug', RouterDebugCommand::class)
->args([
service('router'),
Expand Down Expand Up @@ -334,5 +342,5 @@
service('secrets.local_vault')->ignoreOnInvalid(),
])
->tag('console.command')
;
;
};
5 changes: 5 additions & 0 deletions src/Symfony/Component/Messenger/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.2
---

* Add new `messenger:stats` command that return a list of transports with their "to be processed" message count.

6.1
---

Expand Down
98 changes: 98 additions & 0 deletions src/Symfony/Component/Messenger/Command/StatsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Messenger\Command;

use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Messenger\Transport\Receiver\MessageCountAwareInterface;

/**
* @author Kévin Thérage <therage.kevin@gmail.com>
*/
#[AsCommand(name: 'messenger:stats', description: 'Show the message count for one or more transports')]
class StatsCommand extends Command
{
private ContainerInterface $transportLocator;
private array $transportNames;

public function __construct(ContainerInterface $transportLocator, array $transportNames = [])
{
$this->transportLocator = $transportLocator;
$this->transportNames = $transportNames;

parent::__construct();
}

/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->addArgument('transport_names', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'List of transports\' names')
->setHelp(<<<EOF
The <info>%command.name%</info> command counts the messages for all the transports:

<info>php %command.full_name%</info>

Or specific transports only:

<info>php %command.full_name% <transportNames></info>
EOF
)
;
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output);

$transportNames = $this->transportNames;
if ($input->getArgument('transport_names')) {
$transportNames = $input->getArgument('transport_names');
}

$outputTable = [];
$uncountableTransports = [];
foreach ($transportNames as $transportName) {
if (!$this->transportLocator->has($transportName)) {
$io->warning(sprintf('The "%s" transport does not exist.', $transportName));

continue;
}
$transport = $this->transportLocator->get($transportName);
if (!$transport instanceof MessageCountAwareInterface) {
$uncountableTransports[] = $transportName;

continue;
}
$outputTable[] = [$transportName, $transport->getMessageCount()];
}

$io->table(['Transport', 'Count'], $outputTable);

if ($uncountableTransports) {
$io->note(sprintf('Unable to get message count for the following transports: "%s".', implode('", "', $uncountableTransports)));
}

return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,11 @@ private function registerReceivers(ContainerBuilder $container, array $busIds)
->replaceArgument(1, array_values($receiverNames));
}

if ($container->hasDefinition('console.command.messenger_stats')) {
$container->getDefinition('console.command.messenger_stats')
->replaceArgument(1, array_values($receiverNames));
}

$container->getDefinition('messenger.receiver_locator')->replaceArgument(0, $receiverMapping);

$failureTransportsLocator = ServiceLocatorTagPass::register($container, $failureTransportsMap);
Expand Down
121 changes: 121 additions & 0 deletions src/Symfony/Component/Messenger/Tests/Command/StatsCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Messenger\Tests\Command;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\DependencyInjection\ServiceLocator;
use Symfony\Component\Messenger\Command\StatsCommand;
use Symfony\Component\Messenger\Transport\Receiver\MessageCountAwareInterface;
use Symfony\Component\Messenger\Transport\TransportInterface;

/**
* @author Kévin Thérage <therage.kevin@gmail.com>
*/
class StatsCommandTest extends TestCase
{
private StatsCommand $command;

public function setUp(): void
{
$messageCountableTransport = $this->createMock(MessageCountAwareInterface::class);
$messageCountableTransport->method('getMessageCount')->willReturn(6);

$simpleTransport = $this->createMock(TransportInterface::class);

// mock a service locator
/** @var MockObject&ServiceLocator $serviceLocator */
$serviceLocator = $this->createMock(ServiceLocator::class);
$serviceLocator
->method('get')
->willReturnCallback(function (string $transportName) use ($messageCountableTransport, $simpleTransport) {
if (\in_array($transportName, ['message_countable', 'another_message_countable'], true)) {
return $messageCountableTransport;
}

return $simpleTransport;
});
$serviceLocator
->method('has')
->willReturnCallback(function (string $transportName) {
return \in_array($transportName, ['message_countable', 'simple', 'another_message_countable'], true);
})
;

$this->command = new StatsCommand($serviceLocator, [
'message_countable',
'simple',
'another_message_countable',
'unexisting'
]);
}

public function testWithoutArgument(): void
{
$tester = new CommandTester($this->command);
$tester->execute([]);
$display = $tester->getDisplay();

$this->assertStringContainsString('[WARNING] The "unexisting" transport does not exist.', $display);
$this->assertStringContainsString('message_countable 6', $display);
$this->assertStringContainsString('another_message_countable 6', $display);
$this->assertStringContainsString('! [NOTE] Unable to get message count for the following transports: "simple".', $display);
}

public function testWithOneExistingMessageCountableTransport(): void
{
$tester = new CommandTester($this->command);
$tester->execute(['transport_names' => ['message_countable']]);
$display = $tester->getDisplay();

$this->assertStringNotContainsString('[WARNING] The "unexisting" transport does not exist.', $display);
$this->assertStringContainsString('message_countable 6', $display);
$this->assertStringNotContainsString('another_message_countable', $display);
$this->assertStringNotContainsString(' ! [NOTE] Unable to get message count for the following transports: "simple".', $display);
}

public function testWithMultipleExistingMessageCountableTransport(): void
{
$tester = new CommandTester($this->command);
$tester->execute(['transport_names' => ['message_countable', 'another_message_countable']]);
$display = $tester->getDisplay();

$this->assertStringNotContainsString('[WARNING] The "unexisting" transport does not exist.', $display);
$this->assertStringContainsString('message_countable 6', $display);
$this->assertStringContainsString('another_message_countable 6', $display);
$this->assertStringNotContainsString('! [NOTE] Unable to get message count for the following transports: "simple".', $display);
}

public function testWithNotMessageCountableTransport(): void
{
$tester = new CommandTester($this->command);
$tester->execute(['transport_names' => ['simple']]);
$display = $tester->getDisplay();

$this->assertStringNotContainsString('[WARNING] The "unexisting" transport does not exist.', $display);
$this->assertStringNotContainsString('message_countable', $display);
$this->assertStringNotContainsString('another_message_countable', $display);
$this->assertStringContainsString('! [NOTE] Unable to get message count for the following transports: "simple".', $display);
}

public function testWithNotExistingTransport(): void
{
$tester = new CommandTester($this->command);
$tester->execute(['transport_names' => ['unexisting']]);
$display = $tester->getDisplay();

$this->assertStringContainsString('[WARNING] The "unexisting" transport does not exist.', $display);
$this->assertStringNotContainsString('message_countable', $display);
$this->assertStringNotContainsString('another_message_countable', $display);
$this->assertStringNotContainsString('! [NOTE] Unable to get message count for the following transports: "simple".', $display);
}
}