Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite5433cd

Browse files
ktheragefabpot
authored andcommitted
[Messenger] Add newmessenger:count command that return a list of transports with their "to be processed" message count.
1 parent18809d8 commite5433cd

File tree

6 files changed

+244
-1
lines changed

6 files changed

+244
-1
lines changed

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
useSymfony\Component\Messenger\Bridge\Amqp\Transport\AmqpTransportFactory;
109109
useSymfony\Component\Messenger\Bridge\Beanstalkd\Transport\BeanstalkdTransportFactory;
110110
useSymfony\Component\Messenger\Bridge\Redis\Transport\RedisTransportFactory;
111+
useSymfony\Component\Messenger\Command\StatsCommand;
111112
useSymfony\Component\Messenger\Handler\BatchHandlerInterface;
112113
useSymfony\Component\Messenger\Handler\MessageHandlerInterface;
113114
useSymfony\Component\Messenger\MessageBus;
@@ -503,6 +504,7 @@ public function load(array $configs, ContainerBuilder $container)
503504
$this->registerMessengerConfiguration($config['messenger'],$container,$loader,$config['validation']);
504505
}else {
505506
$container->removeDefinition('console.command.messenger_consume_messages');
507+
$container->removeDefinition('console.command.messenger_stats');
506508
$container->removeDefinition('console.command.messenger_debug');
507509
$container->removeDefinition('console.command.messenger_stop_workers');
508510
$container->removeDefinition('console.command.messenger_setup_transports');
@@ -1966,6 +1968,10 @@ private function registerMessengerConfiguration(array $config, ContainerBuilder
19661968
thrownewLogicException('Messenger support cannot be enabled as the Messenger component is not installed. Try running "composer require symfony/messenger".');
19671969
}
19681970

1971+
if (!class_exists(StatsCommand::class)) {
1972+
$container->removeDefinition('console.command.messenger_stats');
1973+
}
1974+
19691975
$loader->load('messenger.php');
19701976

19711977
if (!interface_exists(DenormalizerInterface::class)) {

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/console.php‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
useSymfony\Component\Console\EventListener\ErrorListener;
4343
useSymfony\Component\Dotenv\Command\DebugCommandasDotenvDebugCommand;
4444
useSymfony\Component\Messenger\Command\ConsumeMessagesCommand;
45+
useSymfony\Component\Messenger\Command\StatsCommand;
4546
useSymfony\Component\Messenger\Command\DebugCommand;
4647
useSymfony\Component\Messenger\Command\FailedMessagesRemoveCommand;
4748
useSymfony\Component\Messenger\Command\FailedMessagesRetryCommand;
@@ -206,6 +207,13 @@
206207
])
207208
->tag('console.command')
208209

210+
->set('console.command.messenger_stats', StatsCommand::class)
211+
->args([
212+
service('messenger.receiver_locator'),
213+
abstract_arg('Receivers names'),
214+
])
215+
->tag('console.command')
216+
209217
->set('console.command.router_debug', RouterDebugCommand::class)
210218
->args([
211219
service('router'),
@@ -334,5 +342,5 @@
334342
service('secrets.local_vault')->ignoreOnInvalid(),
335343
])
336344
->tag('console.command')
337-
;
345+
;
338346
};

‎src/Symfony/Component/Messenger/CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.2
5+
---
6+
7+
* Add new`messenger:stats` command that return a list of transports with their "to be processed" message count.
8+
49
6.1
510
---
611

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\Messenger\Command;
13+
14+
usePsr\Container\ContainerInterface;
15+
useSymfony\Component\Console\Attribute\AsCommand;
16+
useSymfony\Component\Console\Command\Command;
17+
useSymfony\Component\Console\Input\InputArgument;
18+
useSymfony\Component\Console\Input\InputInterface;
19+
useSymfony\Component\Console\Output\ConsoleOutputInterface;
20+
useSymfony\Component\Console\Output\OutputInterface;
21+
useSymfony\Component\Console\Style\SymfonyStyle;
22+
useSymfony\Component\Messenger\Transport\Receiver\MessageCountAwareInterface;
23+
24+
/**
25+
* @author Kévin Thérage <therage.kevin@gmail.com>
26+
*/
27+
#[AsCommand(name:'messenger:stats', description:'Show the message count for one or more transports')]
28+
class StatsCommandextends Command
29+
{
30+
privateContainerInterface$transportLocator;
31+
privatearray$transportNames;
32+
33+
publicfunction__construct(ContainerInterface$transportLocator,array$transportNames = [])
34+
{
35+
$this->transportLocator =$transportLocator;
36+
$this->transportNames =$transportNames;
37+
38+
parent::__construct();
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
protectedfunctionconfigure()
45+
{
46+
$this
47+
->addArgument('transport_names', InputArgument::IS_ARRAY | InputArgument::OPTIONAL,'List of transports\' names')
48+
->setHelp(<<<EOF
49+
The <info>%command.name%</info> command counts the messages for all the transports:
50+
51+
<info>php %command.full_name%</info>
52+
53+
Or specific transports only:
54+
55+
<info>php %command.full_name% <transportNames></info>
56+
EOF
57+
)
58+
;
59+
}
60+
61+
/**
62+
* {@inheritdoc}
63+
*/
64+
protectedfunctionexecute(InputInterface$input,OutputInterface$output):int
65+
{
66+
$io =newSymfonyStyle($input,$outputinstanceof ConsoleOutputInterface ?$output->getErrorOutput() :$output);
67+
68+
$transportNames =$this->transportNames;
69+
if ($input->getArgument('transport_names')) {
70+
$transportNames =$input->getArgument('transport_names');
71+
}
72+
73+
$outputTable = [];
74+
$uncountableTransports = [];
75+
foreach ($transportNamesas$transportName) {
76+
if (!$this->transportLocator->has($transportName)) {
77+
$io->warning(sprintf('The "%s" transport does not exist.',$transportName));
78+
79+
continue;
80+
}
81+
$transport =$this->transportLocator->get($transportName);
82+
if (!$transportinstanceof MessageCountAwareInterface) {
83+
$uncountableTransports[] =$transportName;
84+
85+
continue;
86+
}
87+
$outputTable[] = [$transportName,$transport->getMessageCount()];
88+
}
89+
90+
$io->table(['Transport','Count'],$outputTable);
91+
92+
if ($uncountableTransports) {
93+
$io->note(sprintf('Unable to get message count for the following transports: "%s".',implode('", "',$uncountableTransports)));
94+
}
95+
96+
return0;
97+
}
98+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,11 @@ private function registerReceivers(ContainerBuilder $container, array $busIds)
311311
->replaceArgument(1,array_values($receiverNames));
312312
}
313313

314+
if ($container->hasDefinition('console.command.messenger_stats')) {
315+
$container->getDefinition('console.command.messenger_stats')
316+
->replaceArgument(1,array_values($receiverNames));
317+
}
318+
314319
$container->getDefinition('messenger.receiver_locator')->replaceArgument(0,$receiverMapping);
315320

316321
$failureTransportsLocator = ServiceLocatorTagPass::register($container,$failureTransportsMap);
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\Messenger\Tests\Command;
13+
14+
usePHPUnit\Framework\TestCase;
15+
useSymfony\Component\Console\Tester\CommandTester;
16+
useSymfony\Component\DependencyInjection\ServiceLocator;
17+
useSymfony\Component\Messenger\Command\StatsCommand;
18+
useSymfony\Component\Messenger\Transport\Receiver\MessageCountAwareInterface;
19+
useSymfony\Component\Messenger\Transport\TransportInterface;
20+
21+
/**
22+
* @author Kévin Thérage <therage.kevin@gmail.com>
23+
*/
24+
class StatsCommandTestextends TestCase
25+
{
26+
privateStatsCommand$command;
27+
28+
publicfunctionsetUp():void
29+
{
30+
$messageCountableTransport =$this->createMock(MessageCountAwareInterface::class);
31+
$messageCountableTransport->method('getMessageCount')->willReturn(6);
32+
33+
$simpleTransport =$this->createMock(TransportInterface::class);
34+
35+
// mock a service locator
36+
/** @var MockObject&ServiceLocator $serviceLocator */
37+
$serviceLocator =$this->createMock(ServiceLocator::class);
38+
$serviceLocator
39+
->method('get')
40+
->willReturnCallback(function (string$transportName)use ($messageCountableTransport,$simpleTransport) {
41+
if (\in_array($transportName, ['message_countable','another_message_countable'],true)) {
42+
return$messageCountableTransport;
43+
}
44+
45+
return$simpleTransport;
46+
});
47+
$serviceLocator
48+
->method('has')
49+
->willReturnCallback(function (string$transportName) {
50+
return\in_array($transportName, ['message_countable','simple','another_message_countable'],true);
51+
})
52+
;
53+
54+
$this->command =newStatsCommand($serviceLocator, [
55+
'message_countable',
56+
'simple',
57+
'another_message_countable',
58+
'unexisting'
59+
]);
60+
}
61+
62+
publicfunctiontestWithoutArgument():void
63+
{
64+
$tester =newCommandTester($this->command);
65+
$tester->execute([]);
66+
$display =$tester->getDisplay();
67+
68+
$this->assertStringContainsString('[WARNING] The "unexisting" transport does not exist.',$display);
69+
$this->assertStringContainsString('message_countable 6',$display);
70+
$this->assertStringContainsString('another_message_countable 6',$display);
71+
$this->assertStringContainsString('! [NOTE] Unable to get message count for the following transports: "simple".',$display);
72+
}
73+
74+
publicfunctiontestWithOneExistingMessageCountableTransport():void
75+
{
76+
$tester =newCommandTester($this->command);
77+
$tester->execute(['transport_names' => ['message_countable']]);
78+
$display =$tester->getDisplay();
79+
80+
$this->assertStringNotContainsString('[WARNING] The "unexisting" transport does not exist.',$display);
81+
$this->assertStringContainsString('message_countable 6',$display);
82+
$this->assertStringNotContainsString('another_message_countable',$display);
83+
$this->assertStringNotContainsString(' ! [NOTE] Unable to get message count for the following transports: "simple".',$display);
84+
}
85+
86+
publicfunctiontestWithMultipleExistingMessageCountableTransport():void
87+
{
88+
$tester =newCommandTester($this->command);
89+
$tester->execute(['transport_names' => ['message_countable','another_message_countable']]);
90+
$display =$tester->getDisplay();
91+
92+
$this->assertStringNotContainsString('[WARNING] The "unexisting" transport does not exist.',$display);
93+
$this->assertStringContainsString('message_countable 6',$display);
94+
$this->assertStringContainsString('another_message_countable 6',$display);
95+
$this->assertStringNotContainsString('! [NOTE] Unable to get message count for the following transports: "simple".',$display);
96+
}
97+
98+
publicfunctiontestWithNotMessageCountableTransport():void
99+
{
100+
$tester =newCommandTester($this->command);
101+
$tester->execute(['transport_names' => ['simple']]);
102+
$display =$tester->getDisplay();
103+
104+
$this->assertStringNotContainsString('[WARNING] The "unexisting" transport does not exist.',$display);
105+
$this->assertStringNotContainsString('message_countable',$display);
106+
$this->assertStringNotContainsString('another_message_countable',$display);
107+
$this->assertStringContainsString('! [NOTE] Unable to get message count for the following transports: "simple".',$display);
108+
}
109+
110+
publicfunctiontestWithNotExistingTransport():void
111+
{
112+
$tester =newCommandTester($this->command);
113+
$tester->execute(['transport_names' => ['unexisting']]);
114+
$display =$tester->getDisplay();
115+
116+
$this->assertStringContainsString('[WARNING] The "unexisting" transport does not exist.',$display);
117+
$this->assertStringNotContainsString('message_countable',$display);
118+
$this->assertStringNotContainsString('another_message_countable',$display);
119+
$this->assertStringNotContainsString('! [NOTE] Unable to get message count for the following transports: "simple".',$display);
120+
}
121+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp