From 74b522ef84218fe2050f70ad0cbd5fd497360afc Mon Sep 17 00:00:00 2001 From: llupa Date: Mon, 8 Jul 2019 18:09:39 +0200 Subject: [PATCH] Change DelayStrategy as a configuration option to be assigned to AmqpConnectionFactory MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Grégoire Paris --- docs/reference/advanced_configuration.rst | 5 ++++- src/Backend/AMQPBackendDispatcher.php | 16 +++++++++++++--- src/DependencyInjection/Configuration.php | 5 +++++ .../SonataNotificationExtension.php | 4 ++++ src/Resources/config/backend.xml | 3 +++ 5 files changed, 29 insertions(+), 4 deletions(-) diff --git a/docs/reference/advanced_configuration.rst b/docs/reference/advanced_configuration.rst index e1394acb..48431f57 100644 --- a/docs/reference/advanced_configuration.rst +++ b/docs/reference/advanced_configuration.rst @@ -81,6 +81,10 @@ Full configuration options: vhost: guest console_url: 'http://localhost:55672/api' factory_class: \Enqueue\AmqpLib\AmqpConnectionFactory + + # Use this option to specify the delay strategy + # If you want to completely ignore this you can set it to null + delay_strategy: null consumers: # If set to true, SwiftMailerConsumer and LoggerConsumer will be registered as services @@ -141,4 +145,3 @@ run `composer require enqueue/amqp-ext:^0.8` and change `factory_class` option i .. _`enqueue/amqp-lib`: https://github.com/php-enqueue/enqueue-dev/blob/master/docs/transport/amqp_lib.md .. _`enqueue/amqp-ext`: https://github.com/php-enqueue/enqueue-dev/blob/master/docs/transport/amqp_ext.md .. _`enqueue/amqp-bunny`: https://github.com/php-enqueue/enqueue-dev/blob/master/docs/transport/amqp_bunny.md - diff --git a/src/Backend/AMQPBackendDispatcher.php b/src/Backend/AMQPBackendDispatcher.php index 38f83f6b..3af35cef 100644 --- a/src/Backend/AMQPBackendDispatcher.php +++ b/src/Backend/AMQPBackendDispatcher.php @@ -13,8 +13,8 @@ namespace Sonata\NotificationBundle\Backend; +use Enqueue\AmqpTools\DelayStrategy; use Enqueue\AmqpTools\DelayStrategyAware; -use Enqueue\AmqpTools\RabbitMqDlxDelayStrategy; use Guzzle\Http\Client as GuzzleClient; use Interop\Amqp\AmqpConnectionFactory; use Interop\Amqp\AmqpContext; @@ -62,6 +62,11 @@ class AMQPBackendDispatcher extends QueueBackendDispatcher */ private $context; + /** + * @var DelayStrategy + */ + private $delayStrategy = null; + /** * @param string $defaultQueue */ @@ -129,8 +134,8 @@ final public function getContext() 'vhost' => $this->settings['vhost'], ]); - if ($factory instanceof DelayStrategyAware) { - $factory->setDelayStrategy(new RabbitMqDlxDelayStrategy()); + if ($factory instanceof DelayStrategyAware && $this->delayStrategy instanceof DelayStrategy) { + $factory->setDelayStrategy($this->delayStrategy); } $this->context = $factory->createContext(); @@ -269,6 +274,11 @@ public function initialize() { } + public function setDelayStrategy(DelayStrategy $delayStrategy): void + { + $this->delayStrategy = $delayStrategy; + } + /** * Calls the rabbitmq management api /api//queues endpoint to list the available queues. * diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 3ff44fda..3c3741d9 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -127,6 +127,11 @@ public function getConfigTreeBuilder() ->defaultValue(AmqpConnectionFactory::class) ->info('This option defines an AMQP connection factory to be used to establish a connection with RabbitMQ.') ->end() + ->scalarNode('delay_strategy') + ->isRequired() + ->defaultNull() + ->info('This option defines the delay strategy to use in case an AMQP connection factory is of DelayStrategyAware type. Defaults to null if should be ignored.') + ->end() ->end() ->end() ->end() diff --git a/src/DependencyInjection/SonataNotificationExtension.php b/src/DependencyInjection/SonataNotificationExtension.php index 40ab994e..5f570c86 100644 --- a/src/DependencyInjection/SonataNotificationExtension.php +++ b/src/DependencyInjection/SonataNotificationExtension.php @@ -383,6 +383,10 @@ protected function configureRabbitmq(ContainerBuilder $container, array $config) throw new \RuntimeException('You need to specify a valid default queue for the rabbitmq backend!'); } + if (null !== $connection['delay_strategy'] && $container->has($connection['delay_strategy'])) { + $container->setAlias('sonata.notification.backend.rabbitmq.delay_strategy', $connection['delay_strategy']); + } + $container->getDefinition('sonata.notification.backend.rabbitmq') ->replaceArgument(0, $connection) ->replaceArgument(1, $queues) diff --git a/src/Resources/config/backend.xml b/src/Resources/config/backend.xml index 504c4f2b..5830a273 100644 --- a/src/Resources/config/backend.xml +++ b/src/Resources/config/backend.xml @@ -20,6 +20,9 @@ + + +