1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gendoria\CommandQueueRabbitMqDriverBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Config\FileLocator; |
6
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
7
|
|
|
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; |
8
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
9
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
10
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* This is the class that loads and manages your bundle configuration. |
14
|
|
|
* |
15
|
|
|
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html} |
16
|
|
|
*/ |
17
|
|
|
class GendoriaCommandQueueRabbitMqDriverExtension extends Extension implements PrependExtensionInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Get extension alias. |
21
|
|
|
* |
22
|
|
|
* @return string |
23
|
|
|
*/ |
24
|
|
|
public function getAlias() |
25
|
|
|
{ |
26
|
|
|
return 'gendoria_command_queue_rabbit_mq_driver'; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Load extension. |
31
|
|
|
* |
32
|
|
|
* @param array $configs |
33
|
|
|
* @param ContainerBuilder $container |
34
|
|
|
*/ |
35
|
|
|
public function load(array $configs, ContainerBuilder $container) |
36
|
|
|
{ |
37
|
|
|
$configuration = new Configuration($this->getAlias()); |
38
|
|
|
$config = $this->processConfiguration($configuration, $configs); |
39
|
|
|
|
40
|
|
|
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
41
|
|
|
$loader->load('services.yml'); |
42
|
|
|
|
43
|
|
|
foreach ($config['drivers'] as $driverId => $driver) { |
44
|
|
|
$newDriver = clone $container->getDefinition('gendoria_command_queue_rabbit_mq_driver.send_driver'); |
45
|
|
|
$newDriver->replaceArgument( |
46
|
|
|
1, |
47
|
|
|
new Reference(sprintf('old_sound_rabbit_mq.%s_producer', $driver['producer_name'])) |
48
|
|
|
); |
49
|
|
|
$container->setDefinition('gendoria_command_queue_rabbit_mq_driver.driver.'.$driverId, $newDriver); |
50
|
|
|
|
51
|
|
|
$newWorker = clone $container->getDefinition('gendoria_command_queue_rabbit_mq_driver.external_data_worker'); |
52
|
|
|
$newWorker->replaceArgument( |
53
|
|
|
3, |
54
|
|
|
new Reference(sprintf('old_sound_rabbit_mq.%s_producer', $driver['producer_name'].'_reschedule_delayed')) |
55
|
|
|
); |
56
|
|
|
$container->setDefinition('gendoria_command_queue_rabbit_mq_driver.worker.'.$driverId, $newWorker); |
57
|
|
|
} |
58
|
|
|
$container->removeDefinition('gendoria_command_queue_rabbit_mq_driver.send_driver'); |
59
|
|
|
$container->removeDefinition('gendoria_command_queue_rabbit_mq_driver.external_data_worker'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Prepend configuration to OldSoundRabbitMQBundle. |
64
|
|
|
* |
65
|
|
|
* @param ContainerBuilder $container |
66
|
|
|
*/ |
67
|
|
|
public function prepend(ContainerBuilder $container) |
68
|
|
|
{ |
69
|
|
|
$configs = $container->getExtensionConfig($this->getAlias()); |
70
|
|
|
$currentConfig = $this->processConfiguration(new Configuration($this->getAlias()), $configs); |
71
|
|
|
|
72
|
|
|
$rabbitMQConfig = array( |
73
|
|
|
'consumers' => array(), |
74
|
|
|
'producers' => array(), |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
foreach ($currentConfig['drivers'] as $driverId => $driver) { |
78
|
|
|
$exchangeOptions = array( |
79
|
|
|
'name' => $driver['exchange_name'], |
80
|
|
|
'type' => 'topic', |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
$rabbitMQConfig['producers'][$driver['producer_name']] = array( |
84
|
|
|
'connection' => $driver['rabbitmq_connection'], |
85
|
|
|
'exchange_options' => $exchangeOptions, |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
$rabbitMQConfig['consumers'][$driver['consumer_name']] = array( |
89
|
|
|
'connection' => $driver['rabbitmq_connection'], |
90
|
|
|
'exchange_options' => $exchangeOptions, |
91
|
|
|
'queue_options' => array( |
92
|
|
|
'name' => $driver['consumer_queue_name'], |
93
|
|
|
'durable' => true, |
94
|
|
|
'auto_delete' => false, |
95
|
|
|
'routing_keys' => array('*'), |
96
|
|
|
), |
97
|
|
|
'qos_options' => array( |
98
|
|
|
'prefetch_size' => 0, |
99
|
|
|
'prefetch_count' => 1, |
100
|
|
|
'global' => false, |
101
|
|
|
), |
102
|
|
|
'callback' => 'gendoria_command_queue_rabbit_mq_driver.worker.'.$driverId, |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
$rabbitMQConfig['producers'][$driver['producer_name'].'_reschedule_delayed'] = array( |
106
|
|
|
'connection' => $driver['rabbitmq_connection'], |
107
|
|
|
'exchange_options' => array_merge($exchangeOptions, array( |
108
|
|
|
'name' => $driver['exchange_name'].'-reschedule-delayed', |
109
|
|
|
)), |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
$rabbitMQConfig['consumers'][$driver['consumer_name'].'_reschedule_delayed'] = array( |
113
|
|
|
'connection' => $driver['rabbitmq_connection'], |
114
|
|
|
'exchange_options' => $rabbitMQConfig['producers'][$driver['producer_name'].'_reschedule_delayed']['exchange_options'], |
115
|
|
|
'queue_options' => array( |
116
|
|
|
'name' => $driver['consumer_queue_name'].'-reschedule-delayed', |
117
|
|
|
'durable' => true, |
118
|
|
|
'auto_delete' => false, |
119
|
|
|
'routing_keys' => array('*'), |
120
|
|
|
'arguments' => array( |
121
|
|
|
'x-dead-letter-exchange' => array('S', $exchangeOptions['name']), |
122
|
|
|
'x-message-ttl' => array('I', 600000), //10 minutes |
123
|
|
|
), |
124
|
|
|
), |
125
|
|
|
'callback' => 'gendoria_command_queue_rabbit_mq_driver.services_reschedule_worker', |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$container->prependExtensionConfig('old_sound_rabbit_mq', $rabbitMQConfig); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|