8000 Merge pull request #13 from chalasr/amqp-worker-commands · lyrixx/symfony@0e70c06 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0e70c06

Browse files
authored
Merge pull request #13 from chalasr/amqp-worker-commands
Make commands lazy
2 parents 67bd47d + c3eadeb commit 0e70c06

File tree

4 files changed

+27
-23
lines changed

4 files changed

+27
-23
lines changed

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,9 @@
77
<services>
88
<defaults public="false" />
99

10-
<service id="amqp.broker_locator" class="Symfony\Component\DependencyInjection\ServiceLocator">
11-
<tag name="container.service_locator" />
12-
<argument type="collection">
13-
<argument key="Symfony\Component\Amqp\Broker" type="service" id="amqp.broker" />
14-
</argument>
15-
</service>
16-
1710
<service id="amqp.command.move" class="Symfony\Component\Amqp\Command\AmqpMoveCommand">
18-
<tag name="console.command" />
19-
<argument type="service" id="amqp.broker_locator" />
11+
<tag name="console.command" command="amqp:move" />
12+
<argument type="service" id="amqp.broker" />
2013
<argument type="service" id="logger" on-invalid="null" />
2114
</service>
2215

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
</service>
1414

1515
<service id="worker.command.list" class="Symfony\Component\Worker\Command\WorkerListCommand">
16-
<tag name="console.command" />
16+
<tag name="console.command" command="worker:list" />
1717
<argument /> <!-- worker names -->
1818
</service>
1919

2020
<service id="worker.command.run" class="Symfony\Component\Worker\Command\WorkerRunCommand">
21-
<tag name="console.command" />
21+
<tag name="console.command" command="worker:run" />
2222
<argument type="service" id="worker.worker_locator" />
2323
<argument /> <!-- process title prefix -->
2424
<argument /> <!-- worker names -->

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
3131
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
3232
use Symfony\Component\DependencyInjection\Reference;
33+
use Symfony\Component\DependencyInjection\TypedReference;
3334
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
3435
use Symfony\Component\PropertyAccess\PropertyAccessor;
3536
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
@@ -42,6 +43,7 @@
4243
use Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer;
4344
use Symfony\Component\Translation\DependencyInjection\TranslatorPass;
4445
use Symfony\Component\Validator\DependencyInjection\AddConstraintValidatorsPass;
46+
use Symfony\Component\Worker\Loop\Loop;
4547

4648
abstract class FrameworkExtensionTest extends TestCase
4749
{
@@ -981,6 +983,7 @@ public function testAmqpEmpty()
981983

982984
$this->assertTrue($container->hasAlias('amqp.broker'));
983985
$this->assertSame('amqp.broker.default', (string) $container->getAlias('amqp.broker'));
986+
$this->assertEquals(new Reference('amqp.broker'), $container->getDefinition('amqp.command.move')->getArgument(0));
984987
}
985988

986989
public function testAmqpFull()
@@ -1034,11 +1037,14 @@ public function testAmqpFull()
10341037

10351038
$this->assertTrue($container->hasAlias('amqp.broker'));
10361039
$this->assertSame('amqp.broker.queue_prod', (string) $container->getAlias('amqp.broker'));
1040+
$this->assertEquals(new Reference('amqp.broker'), $container->getDefinition('amqp.command.move')->getArgument(0));
10371041
}
10381042

10391043
public function testWorkerEmpty()
10401044
{
10411045
$container = $this->createContainerFromFile('worker_empty');
1046+
1047+
$this->assertSame(array(), $container->getDefinition('worker.command.list')->getArgument(0));
10421048
}
10431049

10441050
public function testWorkerFull()
@@ -1134,6 +1140,17 @@ public function testWorkerFull()
11341140
$this->assertInstanceOf(Reference::class, $worker->getArgument(2));
11351141
$this->assertSame('logger', (string) $worker->getArgument(2));
11361142
$this->assertSame('worker_service_a', $worker->getArgument(3));
1143+
$workerLocator = $container->getDefinition('worker.worker_locator');
1144+
$this->assertEquals(array('worker_d' => new TypedReference('worker.worker.worker_d', Loop::class), 'worker_service_a' => new TypedReference('worker.worker.worker_service_a', Loop::class)), $workerLocator->getArgument(0));
1145+
1146+
/* worker:list command */
1147+
$this->assertSame(array('worker_d', 'worker_service_a'), $container->getDefinition('worker.command.list')->getArgument(0));
1148+
1149+
/* worker:run command */
1150+
$workerRunCommand = $container->getDefinition('worker.command.run');
1151+
$this->assertEquals(new Reference('worker.worker_locator'), $workerRunCommand->getArgument(0));
1152+
$this->assertEquals('foobar', $workerRunCommand->getArgument(1), 'worker:run expects the "worker.cli_title_prefix" config value as 2nd argument');
1153+
$this->assertSame(array('worker_d', 'worker_service_a'), $workerRunCommand->getArgument(2));
11371154
}
11381155

11391156
protected function createContainer(array $data = array())

src/Symfony/Component/Amqp/Command/AmqpMoveCommand.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,14 @@
1717
*/
1818
class AmqpMoveCommand extends Command
1919
{
20-
private $container;
20+
private $broker;
2121
private $logger;
2222

23-
/**
24-
* @param ContainerInterface $container A PSR11 container from which to load the Broker service
25-
* @param LoggerInterface|null $logger
26-
*/
27-
public function __construct(ContainerInterface $container, LoggerInterface $logger = null)
23+
public function __construct(Broker $broker, LoggerInterface $logger = null)
2824
{
2925
parent::__construct();
3026

31-
$this->container = $container;
27+
$this->broker = $broker;
3228
$this->logger = $logger;
3329
}
3430

@@ -46,13 +42,11 @@ protected function configure()
4642

4743
protected function execute(InputInterface $input, OutputInterface $output)
4844
{
49-
/** @var Broker $broker */
50-
$broker = $this->container->get(Broker::class);
5145
$io = new SymfonyStyle($input, $output);
5246
$from = $input->getArgument('from');
5347
$to = $input->getArgument('to');
5448

55-
while (false !== $message = $broker->get($from)) {
49+
while (false !== $message = $this->broker->get($from)) {
5650
$io->comment("Moving a message from $from to $to...");
5751

5852
if (null !== $this->logger) {
@@ -62,8 +56,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
6256
));
6357
}
6458

65-
$broker->move($message, $to);
66-
$broker->ack($message);
59+
$this->broker->move($message, $to);
60+
$this->broker->ack($message);
6761

6862
if ($output->isDebug()) {
6963
$io->comment("...message moved from $from to $to.");

0 commit comments

Comments
 (0)
0