Skip to content

Commit b75d2ce

Browse files
committed
delay strategy
1 parent 7b6a9cb commit b75d2ce

4 files changed

Lines changed: 89 additions & 7 deletions

File tree

‎pkg/amqp-bunny/Symfony/RabbitMqAmqpBunnyTransportFactory.php‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Enqueue\AmqpBunny\Symfony;
44

5+
use Enqueue\AmqpTools\DelayStrategyTransportFactoryTrait;
56
use Enqueue\Client\Amqp\RabbitMqDriver;
67
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
78
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -10,6 +11,8 @@
1011

1112
class RabbitMqAmqpBunnyTransportFactory extends AmqpBunnyTransportFactory
1213
{
14+
use DelayStrategyTransportFactoryTrait;
15+
1316
/**
1417
* @param string $name
1518
*/
@@ -27,13 +30,25 @@ public function addConfiguration(ArrayNodeDefinition $builder)
2730

2831
$builder
2932
->children()
30-
->booleanNode('delay_plugin_installed')
33+
->booleanNode('delay_strategy')
3134
->defaultFalse()
32-
->info('The option tells whether RabbitMQ broker has delay plugin installed or not')
35+
->info('The delay strategy to be used. Possible values are "dlx", "delayed_message_plugin" or service id')
3336
->end()
3437
;
3538
}
3639

40+
/**
41+
* {@inheritdoc}
42+
*/
43+
public function createConnectionFactory(ContainerBuilder $container, array $config)
44+
{
45+
$factoryId = parent::createConnectionFactory($container, $config);
46+
47+
$this->registerDelayStrategy($container, $config, $factoryId, $this->getName());
48+
49+
return $factoryId;
50+
}
51+
3752
/**
3853
* {@inheritdoc}
3954
*/

‎pkg/amqp-ext/Symfony/RabbitMqAmqpTransportFactory.php‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Enqueue\AmqpExt\Symfony;
44

5+
use Enqueue\AmqpTools\DelayStrategyTransportFactoryTrait;
56
use Enqueue\Client\Amqp\RabbitMqDriver;
67
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
78
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -10,6 +11,8 @@
1011

1112
class RabbitMqAmqpTransportFactory extends AmqpTransportFactory
1213
{
14+
use DelayStrategyTransportFactoryTrait;
15+
1316
/**
1417
* @param string $name
1518
*/
@@ -27,13 +30,25 @@ public function addConfiguration(ArrayNodeDefinition $builder)
2730

2831
$builder
2932
->children()
30-
->booleanNode('delay_plugin_installed')
33+
->booleanNode('delay_strategy')
3134
->defaultFalse()
32-
->info('The option tells whether RabbitMQ broker has delay plugin installed or not')
35+
->info('The delay strategy to be used. Possible values are "dlx", "delayed_message_plugin" or service id')
3336
->end()
3437
;
3538
}
3639

40+
/**
41+
* {@inheritdoc}
42+
*/
43+
public function createConnectionFactory(ContainerBuilder $container, array $config)
44+
{
45+
$factoryId = parent::createConnectionFactory($container, $config);
46+
47+
$this->registerDelayStrategy($container, $config, $factoryId, $this->getName());
48+
49+
return $factoryId;
50+
}
51+
3752
/**
3853
* {@inheritdoc}
3954
*/

‎pkg/amqp-lib/Symfony/RabbitMqAmqpLibTransportFactory.php‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Enqueue\AmqpLib\Symfony;
44

5+
use Enqueue\AmqpTools\DelayStrategyTransportFactoryTrait;
56
use Enqueue\Client\Amqp\RabbitMqDriver;
67
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
78
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -10,6 +11,8 @@
1011

1112
class RabbitMqAmqpLibTransportFactory extends AmqpLibTransportFactory
1213
{
14+
use DelayStrategyTransportFactoryTrait;
15+
1316
/**
1417
* @param string $name
1518
*/
@@ -27,13 +30,25 @@ public function addConfiguration(ArrayNodeDefinition $builder)
2730

2831
$builder
2932
->children()
30-
->booleanNode('delay_plugin_installed')
31-
->defaultFalse()
32-
->info('The option tells whether RabbitMQ broker has delay plugin installed or not')
33+
->scalarNode('delay_strategy')
34+
->defaultNull()
35+
->info('The delay strategy to be used. Possible values are "dlx", "delayed_message_plugin" or service id')
3336
->end()
3437
;
3538
}
3639

40+
/**
41+
* {@inheritdoc}
42+
*/
43+
public function createConnectionFactory(ContainerBuilder $container, array $config)
44+
{
45+
$factoryId = parent::createConnectionFactory($container, $config);
46+
47+
$this->registerDelayStrategy($container, $config, $factoryId, $this->getName());
48+
49+
return $factoryId;
50+
}
51+
3752
/**
3853
* {@inheritdoc}
3954
*/
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Enqueue\AmqpTools;
4+
5+
use Symfony\Component\DependencyInjection\ContainerBuilder;
6+
use Symfony\Component\DependencyInjection\Reference;
7+
8+
trait DelayStrategyTransportFactoryTrait
9+
{
10+
/**
11+
* {@inheritdoc}
12+
*/
13+
public function registerDelayStrategy(ContainerBuilder $container, array $config, $factoryId, $factoryName)
14+
{
15+
if ($config['delay_strategy']) {
16+
$factory = $container->getDefinition($factoryId);
17+
18+
if (false == is_a($factory->getClass(), DelayStrategyAware::class, true)) {
19+
throw new \LogicException('Connection factory does not support delays');
20+
}
21+
22+
if (strtolower($config['delay_strategy']) === 'dlx') {
23+
$delayId = sprintf('enqueue.client.%s.delay_strategy', $factoryName);
24+
$container->register($delayId, RabbitMqDlxDelayStrategy::class);
25+
26+
$factory->addMethodCall('setDelayStrategy', [new Reference($delayId)]);
27+
} elseif (strtolower($config['delay_strategy']) === 'delayed_message_plugin') {
28+
$delayId = sprintf('enqueue.client.%s.delay_strategy', $factoryName);
29+
$container->register($delayId, RabbitMqDelayPluginDelayStrategy::class);
30+
31+
$factory->addMethodCall('setDelayStrategy', [new Reference($delayId)]);
32+
} else {
33+
$factory->addMethodCall('setDelayStrategy', [new Reference($config['delay_strategy'])]);
34+
}
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)