8000 Added two new components: AMQP and Worker by lyrixx · Pull Request #23315 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Added two new components: AMQP and Worker #23315

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
11 changes: 2 additions & 9 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/amqp.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,9 @@
<services>
<defaults public="false" />

<service id="amqp.broker_locator" class="Symfony\Component\DependencyInjection\ServiceLocator">
<tag name="container.service_locator" />
<argument type="collection">
<argument key="Symfony\Component\Amqp\Broker" type="service" id="amqp.broker" />
</argument>
</service>

<service id="amqp.command.move" class="Symfony\Component\Amqp\Command\AmqpMoveCommand">
<tag name="console.command" />
<argument type="service" id="amqp.broker_locator" />
<tag name="console.command" command="amqp:move" />
<argument type="service" id="amqp.broker" />
<argument type="service" id="logger" on-invalid="null" />
</service>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
</service>

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

<service id="worker.command.run" class="Symfony\Component\Worker\Command\WorkerRunCommand">
<tag name="console.command" />
<tag name="console.command" command="worker:run" />
<argument type="service" id="worker.worker_locator" />
<argument /> <!-- process title prefix -->
<argument /> <!-- worker names -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\TypedReference;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\PropertyAccess\PropertyAccessor;
use Symfony\Component\S 8000 erializer\Mapping\Loader\AnnotationLoader;
Expand All @@ -42,6 +43,7 @@
use Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer;
use Symfony\Component\Translation\DependencyInjection\TranslatorPass;
use Symfony\Component\Validator\DependencyInjection\AddConstraintValidatorsPass;
use Symfony\Component\Worker\Loop\Loop;

abstract class FrameworkExtensionTest extends TestCase
{
Expand Down Expand Up @@ -981,6 +983,7 @@ public function testAmqpEmpty()

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

public function testAmqpFull()
Expand Down Expand Up @@ -1034,11 +1037,14 @@ public function testAmqpFull()

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

public function testWorkerEmpty()
{
$container = $this->createContainerFromFile('worker_empty');

$this->assertSame(array(), $container->getDefinition('worker.command.list')->getArgument(0));
}

public function testWorkerFull()
Expand Down Expand Up @@ -1134,6 +1140,17 @@ public function testWorkerFull()
$this-&g 8000 t;assertInstanceOf(Reference::class, $worker->getArgument(2));
$this->assertSame('logger', (string) $worker->getArgument(2));
$this->assertSame('worker_service_a', $worker->getArgument(3));
$workerLocator = $container->getDefinition('worker.worker_locator');
$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));

/* worker:list command */
$this->assertSame(array('worker_d', 'worker_service_a'), $container->getDefinition('worker.command.list')->getArgument(0));

/* worker:run command */
$workerRunCommand = $container->getDefinition('worker.command.run');
$this->assertEquals(new Reference('worker.worker_locator'), $workerRunCommand->getArgument(0));
$this->assertEquals('foobar', $workerRunCommand->getArgument(1), 'worker:run expects the "worker.cli_title_prefix" config value as 2nd argument');
$this->assertSame(array('worker_d', 'worker_service_a'), $workerRunCommand->getArgument(2));
}

protected function createContainer(array $data = array())
Expand Down
18 changes: 6 additions & 12 deletions src/Symfony/Component/Amqp/Command/AmqpMoveCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,14 @@
*/
class AmqpMoveCommand extends Command
{
private $container;
private $broker;
private $logger;

/**
* @param ContainerInterface $container A PSR11 container from which to load the Broker service
* @param LoggerInterface|null $logger
*/
public function __construct(ContainerInterface $container, LoggerInterface $logger = null)
public function __construct(Broker $broker, LoggerInterface $logger = null)
{
parent::__construct();

$this->container = $container;
$this->broker = $broker;
$this->logger = $logger;
}

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

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

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

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

$broker->move($message, $to);
$broker->ack($message);
$this->broker->move($message, $to);
$this->broker->ack($message);

if ($output->isDebug()) {
$io->comment("...message moved from $from to $to.");
Expand Down
0