Skip to content

[Messenger] add a transport that processes messages on kernel.terminate #28746

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 1 commit into from
Closed
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 @@ -39,6 +39,7 @@
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Definition;
Expand Down Expand Up @@ -1542,7 +1543,9 @@ private function registerMessengerConfiguration(array $config, ContainerBuilder
'before' => array(array('id' => 'logging')),
'after' => array(array('id' => 'send_message'), array('id' => 'handle_message')),
);
$buses = array();
foreach ($config['buses'] as $busId => $bus) {
$buses[$busId] = new Reference($busId);
$middleware = $bus['middleware'];

if ($bus['default_middleware']) {
Expand Down Expand Up @@ -1575,6 +1578,10 @@ private function registerMessengerConfiguration(array $config, ContainerBuilder
}
}

$container->getDefinition('messenger.transport.kernel_terminate.factory')
->replaceArgument(0, ServiceLocatorTagPass::register($container, $buses))
;

$senderAliases = array();
foreach ($config['transports'] as $name => $transport) {
if (0 === strpos($transport['dsn'], 'amqp://') && !$container->hasDefinition('messenger.transport.amqp.factory')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,11 @@
<argument type="service" id="messenger.transport.serializer" />
<argument>%kernel.debug%</argument>
</service>

<service id="messenger.transport.kernel_terminate.factory" class="Symfony\Component\Messenger\Transport\KernelTerminateTransportFactory">
<argument /> <!-- Message bus locator -->
<argument type="service" id="event_dispatcher" />
<tag name="messenger.transport_factory" />
</service>
</services>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

$container->loadFromExtension('framework', array(
'messenger' => array(
'serializer' => false,
'buses' => array(
'a_bus' => null,
'another_bus' => null,
),
'transports' => array(
'kernel_terminate' => array(
'dsn' => 'symfony://kernel.terminate',
),
),
),
));
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

$container->loadFromExtension('framework', array(
'messenger' => array(
'serializer' => false,
'buses' => array(
'a_bus' => null,
),
'transports' => array(
'kernel_terminate' => array(
'dsn' => 'symfony://kernel.terminate',
),
),
),
));
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

$container->loadFromExtension('framework', array(
'messenger' => array(
'serializer' => false,
'buses' => array(
'a_bus' => null,
'another_bus' => null,
),
'transports' => array(
'kernel_terminate' => array(
'dsn' => 'symfony://kernel.terminate',
'options' => array('bus' => 'a_bus'),
),
),
),
));
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

$container->loadFromExtension('framework', array(
'messenger' => array(
'serializer' => false,
'transports' => array(
'kernel_terminate' => array(
'dsn' => 'symfony://kernel.terminate',
'options' => array('an_option' => 'an_option_value'),
),
),
),
));
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

$container->loadFromExtension('framework', array(
'messenger' => array(
'serializer' => false,
'transports' => array(
'kernel_terminate' => array(
'dsn' => 'symfony://kernel.terminate',
'options' => array('bus' => 'messenger.bus.commands'),
),
),
),
));
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

<framework:config>
<framework:messenger>
<framework:serializer id="null" />
<framework:transport name="kernel_terminate" dsn="symfony://kernel.terminate" />
<framework:bus name="a_bus" />
<framework:bus name="another_bus" />
</framework:messenger>
</framework:config>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

<framework:config>
<framework:messenger>
<framework:serializer id="null" />
<framework:transport name="kernel_terminate" dsn="symfony://kernel.terminate" />
<framework:bus name="a_bus" />
</framework:messenger>
</framework:config>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

<framework:config>
<framework:messenger>
<framework:serializer id="null" />
<framework:transport name="kernel_terminate" dsn="symfony://kernel.terminate">
<framework:options>
<framework:bus>a_bus</framework:bus>
</framework:options>
</framework:transport>
<framework:bus name="a_bus" />
<framework:bus name="another_bus" />
</framework:messenger>
</framework:config>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

<framework:config>
<framework:messenger>
<framework:serializer id="null" />
<framework:transport name="kernel_terminate" dsn="symfony://kernel.terminate">
<framework:options>
<framework:an_option>an_option_value</framework:an_option>
</framework:options>
</framework:transport>
</framework:messenger>
</framework:config>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

<framework:config>
<framework:messenger>
<framework:serializer id="null" />
<framework:transport name="kernel_terminate" dsn="symfony://kernel.terminate">
<framework:options>
<framework:bus>messenger.bus.commands</framework:bus>
</framework:options>
</framework:transport>
</framework:messenger>
</framework:config>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
framework:
messenger:
serializer: false
buses:
a_bus: ~
another_bus: ~
transports:
kernel_terminate:
dsn: 'symfony://kernel.terminate'
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
framework:
messenger:
serializer: false
buses:
a_bus: ~
transports:
kernel_terminate:
dsn: 'symfony://kernel.terminate'
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
framework:
messenger:
serializer: false
buses:
a_bus: ~
another_bus: ~
transports:
kernel_terminate:
dsn: 'symfony://kernel.terminate'
options:
bus: a_bus
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
framework:
messenger:
serializer: false
transports:
kernel_terminate:
dsn: 'symfony://kernel.terminate'
options:
an_option: an_option_value
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
framework:
messenger:
serializer: false
transports:
kernel_terminate:
dsn: 'symfony://kernel.terminate'
options:
bus: messenger.bus.commands
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,55 @@ public function testMessengerMiddlewareFactoryErroneousFormat()
$this->createContainerFromFile('messenger_middleware_factory_erroneous_format');
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage No bus with id "messenger.bus.commands" was found in framework.messenger.buses config for transport "kernel_terminate". Known buses are ["messenger.bus.default"].
*/
public function testMessengerKernelTerminateMissingBus()
{
$this->createContainerFromFile('messenger_kernel_terminate_transport_missing_bus');
}

public function testMessengerKernelTerminateNoBusInOptionsAndMoreThanOneBusConfigured()
{
$container = $this->createContainerFromFile('messenger_kernel_terminate_no_default_bus');

$this->assertKernelExceptionTags($container);

$arguments = $container->getDefinition('messenger.transport.kernel_terminate')->getArguments();
$this->assertEquals(array('symfony://kernel.terminate', array()), $arguments);
}

public function testMessengerKernelTerminateNoBusInOptionsAndOneBusConfigured()
{
$container = $this->createContainerFromFile('messenger_kernel_terminate_one_bus');

$this->assertKernelExceptionTags($container);

$arguments = $container->getDefinition('messenger.transport.kernel_terminate')->getArguments();
$this->assertEquals(array('symfony://kernel.terminate', array('bus' => 'a_bus')), $arguments);
}

public function testMessengerKernelTerminateNoBusInOptionsDefaultBus()
{
$container = $this->createContainerFromFile('messenger_kernel_terminate_transport_default_bus');

$this->assertKernelExceptionTags($container);

$arguments = $container->getDefinition('messenger.transport.kernel_terminate')->getArguments();
$this->assertEquals(array('symfony://kernel.terminate', array('bus' => 'messenger.bus.default', 'an_option' => 'an_option_value')), $arguments);
}

public function testMessengerKernelTerminateBusInOptions()
{
$container = $this->createContainerFromFile('messenger_kernel_terminate_transport_bus');

$this->assertKernelExceptionTags($container);

$arguments = $container->getDefinition('messenger.transport.kernel_terminate')->getArguments();
$this->assertEquals(array('symfony://kernel.terminate', array('bus' => 'a_bus')), $arguments);
}

public function testTranslator()
{
$container = $this->createContainerFromFile('full');
Expand Down Expand Up @@ -1457,6 +1506,19 @@ private function assertCachePoolServiceDefinitionIsCreated(ContainerBuilder $con
$this->fail('Unresolved adapter: '.$adapter);
}
}

private function assertKernelExceptionTags(ContainerInterface $container)
{
$tags = $container->getDefinition('messenger.transport.kernel_terminate')->getTags();

$this->assertEquals(
array('messenger.receiver' => array(array('alias' => 'kernel_terminate')),
'kernel.event_listener' => array(
array('event' => 'kernel.terminate', 'method' => 'flush'),
array('event' => 'kernel.exception', 'method' => 'stop'),
),
), $tags);
}
}

/**
Expand Down
Loading