|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Gendoria\CommandQueueRabbitMqDriverBundle\DependencyInjection; |
|
4
|
|
|
|
|
5
|
|
|
use Gendoria\CommandQueueBundle\DependencyInjection\Pass\WorkerRunnersPass; |
|
6
|
|
|
use Symfony\Component\Config\FileLocator; |
|
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
8
|
|
|
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; |
|
9
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
|
10
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
11
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* This is the class that loads and manages your bundle configuration. |
|
15
|
|
|
* |
|
16
|
|
|
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html} |
|
17
|
|
|
*/ |
|
18
|
|
|
class GendoriaCommandQueueRabbitMqDriverExtension extends Extension implements PrependExtensionInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Get extension alias. |
|
22
|
|
|
* |
|
23
|
|
|
* @return string |
|
24
|
|
|
*/ |
|
25
|
3 |
|
public function getAlias() |
|
26
|
|
|
{ |
|
27
|
3 |
|
return 'gendoria_command_queue_rabbit_mq_driver'; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Load extension. |
|
32
|
|
|
* |
|
33
|
|
|
* @param array $configs |
|
34
|
|
|
* @param ContainerBuilder $container |
|
35
|
|
|
*/ |
|
36
|
2 |
|
public function load(array $configs, ContainerBuilder $container) |
|
37
|
|
|
{ |
|
38
|
2 |
|
$configuration = new Configuration($this->getAlias()); |
|
39
|
2 |
|
$config = $this->processConfiguration($configuration, $configs); |
|
40
|
|
|
|
|
41
|
2 |
|
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
42
|
2 |
|
$loader->load('services.yml'); |
|
43
|
|
|
|
|
44
|
2 |
|
$this->loadDrivers($config, $container); |
|
45
|
2 |
|
$container->removeDefinition('gendoria_command_queue_rabbit_mq_driver.send_driver'); |
|
46
|
2 |
|
$container->removeDefinition('gendoria_command_queue_rabbit_mq_driver.external_data_worker'); |
|
47
|
2 |
|
if (!$config['clear_entity_managers_listener_enabled']) { |
|
48
|
1 |
|
$container->removeDefinition('gendoria_command_queue_rabbit_mq_driver.listener.clear_entity_managers'); |
|
49
|
1 |
|
} |
|
50
|
2 |
|
} |
|
51
|
|
|
|
|
52
|
2 |
|
private function loadDrivers(array $config, ContainerBuilder $container) |
|
53
|
|
|
{ |
|
54
|
2 |
|
$serializer = substr($config['serializer'], 1); |
|
55
|
2 |
|
$workerRunner = $container->getDefinition('gendoria_command_queue_rabbit_mq_driver.worker_runner'); |
|
56
|
2 |
|
foreach ($config['drivers'] as $driverId => $driver) { |
|
57
|
2 |
|
$producerName = sprintf('old_sound_rabbit_mq.%s_producer', $driver['producer_name']); |
|
58
|
2 |
|
$delayedProducerName = sprintf('old_sound_rabbit_mq.%s_reschedule_delayed_producer', $driver['producer_name']); |
|
59
|
|
|
|
|
60
|
2 |
|
$newDriver = clone $container->getDefinition('gendoria_command_queue_rabbit_mq_driver.send_driver'); |
|
61
|
2 |
|
$newDriver->replaceArgument(0, new Reference($serializer)); |
|
62
|
2 |
|
$newDriver->replaceArgument(1, new Reference($producerName)); |
|
63
|
2 |
|
$container->setDefinition('gendoria_command_queue_rabbit_mq_driver.driver.'.$driverId, $newDriver); |
|
64
|
|
|
|
|
65
|
2 |
|
$newWorker = clone $container->getDefinition('gendoria_command_queue_rabbit_mq_driver.external_data_worker'); |
|
66
|
2 |
|
$newWorker->replaceArgument(2, new Reference($serializer)); |
|
67
|
2 |
|
$newWorker->replaceArgument(3, new Reference($delayedProducerName)); |
|
68
|
2 |
|
$workerRunner->addTag(WorkerRunnersPass::WORKER_RUNNER_TAG, array('name' => $driverId, 'options' => json_encode($driver))); |
|
69
|
2 |
|
$container->setDefinition('gendoria_command_queue_rabbit_mq_driver.worker.'.$driverId, $newWorker); |
|
70
|
2 |
|
} |
|
71
|
2 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Prepend configuration. |
|
75
|
|
|
* |
|
76
|
|
|
* @param ContainerBuilder $container |
|
77
|
|
|
*/ |
|
78
|
2 |
|
public function prepend(ContainerBuilder $container) |
|
79
|
|
|
{ |
|
80
|
2 |
|
$this->disableDoctrineListener($container); |
|
81
|
2 |
|
$this->prependRabbitMq($container); |
|
82
|
2 |
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Prepend configuration to OldSoundRabbitMQBundle. |
|
86
|
|
|
* |
|
87
|
|
|
* @param ContainerBuilder $container |
|
88
|
|
|
*/ |
|
89
|
2 |
|
private function prependRabbitMq(ContainerBuilder $container) { |
|
90
|
2 |
|
$configs = $container->getExtensionConfig($this->getAlias()); |
|
91
|
2 |
|
$currentConfig = $this->processConfiguration(new Configuration($this->getAlias()), $configs); |
|
92
|
|
|
|
|
93
|
|
|
$rabbitMQConfig = array( |
|
94
|
2 |
|
'consumers' => array(), |
|
95
|
2 |
|
'producers' => array(), |
|
96
|
2 |
|
); |
|
97
|
|
|
|
|
98
|
2 |
|
foreach ($currentConfig['drivers'] as $driverId => $driver) { |
|
99
|
|
|
$exchangeOptions = array( |
|
100
|
2 |
|
'name' => $driver['exchange_name'], |
|
101
|
2 |
|
'type' => 'topic', |
|
102
|
2 |
|
); |
|
103
|
|
|
|
|
104
|
2 |
|
$rabbitMQConfig['producers'][$driver['producer_name']] = array( |
|
105
|
2 |
|
'connection' => $driver['rabbitmq_connection'], |
|
106
|
2 |
|
'exchange_options' => $exchangeOptions, |
|
107
|
|
|
); |
|
108
|
|
|
|
|
109
|
2 |
|
$rabbitMQConfig['consumers'][$driver['consumer_name']] = array( |
|
110
|
2 |
|
'connection' => $driver['rabbitmq_connection'], |
|
111
|
2 |
|
'exchange_options' => $exchangeOptions, |
|
112
|
|
|
'queue_options' => array( |
|
113
|
2 |
|
'name' => $driver['consumer_queue_name'], |
|
114
|
2 |
|
'durable' => true, |
|
115
|
2 |
|
'auto_delete' => false, |
|
116
|
2 |
|
'routing_keys' => array('*'), |
|
117
|
2 |
|
), |
|
118
|
|
|
'qos_options' => array( |
|
119
|
2 |
|
'prefetch_size' => 0, |
|
120
|
2 |
|
'prefetch_count' => 1, |
|
121
|
2 |
|
'global' => false, |
|
122
|
2 |
|
), |
|
123
|
2 |
|
'callback' => 'gendoria_command_queue_rabbit_mq_driver.worker.'.$driverId, |
|
124
|
|
|
); |
|
125
|
|
|
|
|
126
|
2 |
|
$rabbitMQConfig['producers'][$driver['producer_name'].'_reschedule_delayed'] = array( |
|
127
|
2 |
|
'connection' => $driver['rabbitmq_connection'], |
|
128
|
2 |
|
'exchange_options' => array_merge($exchangeOptions, array( |
|
129
|
2 |
|
'name' => $driver['exchange_name'].'-reschedule-delayed', |
|
130
|
2 |
|
)), |
|
131
|
|
|
); |
|
132
|
|
|
|
|
133
|
2 |
|
$rabbitMQConfig['consumers'][$driver['consumer_name'].'_reschedule_delayed'] = array( |
|
134
|
2 |
|
'connection' => $driver['rabbitmq_connection'], |
|
135
|
2 |
|
'exchange_options' => $rabbitMQConfig['producers'][$driver['producer_name'].'_reschedule_delayed']['exchange_options'], |
|
136
|
|
|
'queue_options' => array( |
|
137
|
2 |
|
'name' => $driver['consumer_queue_name'].'-reschedule-delayed', |
|
138
|
2 |
|
'durable' => true, |
|
139
|
2 |
|
'auto_delete' => false, |
|
140
|
2 |
|
'routing_keys' => array('*'), |
|
141
|
|
|
'arguments' => array( |
|
142
|
2 |
|
'x-dead-letter-exchange' => array('S', $exchangeOptions['name']), |
|
143
|
2 |
|
'x-message-ttl' => array('I', 600000), //10 minutes |
|
144
|
2 |
|
), |
|
145
|
2 |
|
), |
|
146
|
2 |
|
'callback' => 'gendoria_command_queue_rabbit_mq_driver.services_reschedule_worker', |
|
147
|
|
|
); |
|
148
|
2 |
|
} |
|
149
|
|
|
|
|
150
|
2 |
|
$container->prependExtensionConfig('old_sound_rabbit_mq', $rabbitMQConfig); |
|
151
|
2 |
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Disable clear entity managers listener, if no doctrine bundle is installed. |
|
155
|
|
|
* |
|
156
|
|
|
* @param ContainerBuilder $container |
|
157
|
|
|
* @return void |
|
158
|
|
|
*/ |
|
159
|
2 |
|
private function disableDoctrineListener(ContainerBuilder $container) |
|
160
|
|
|
{ |
|
161
|
2 |
|
if (!$container->hasParameter('kernel.bundles')) { |
|
162
|
1 |
|
return; |
|
163
|
|
|
} |
|
164
|
1 |
|
$bundles = $container->getParameter('kernel.bundles'); |
|
165
|
1 |
|
if (!isset($bundles['DoctrineBundle'])) { |
|
166
|
1 |
|
$container->prependExtensionConfig($this->getAlias(), array( |
|
167
|
1 |
|
'clear_entity_managers_listener_enabled' => false, |
|
168
|
1 |
|
)); |
|
169
|
1 |
|
} |
|
170
|
1 |
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|