1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dtc\QueueBundle\DependencyInjection\Compiler; |
4
|
|
|
|
5
|
|
|
use Dtc\GridBundle\DependencyInjection\Compiler\GridSourceCompilerPass; |
6
|
|
|
use Dtc\QueueBundle\Model\Job; |
7
|
|
|
use Dtc\QueueBundle\Model\JobTiming; |
8
|
|
|
use Dtc\QueueBundle\Model\Run; |
9
|
|
|
use Dtc\QueueBundle\Exception\ClassNotFoundException; |
10
|
|
|
use Dtc\QueueBundle\Exception\ClassNotSubclassException; |
11
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
12
|
|
|
use Symfony\Component\DependencyInjection\Alias; |
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
14
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
15
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
16
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
17
|
|
|
|
18
|
|
|
class WorkerCompilerPass implements CompilerPassInterface |
19
|
|
|
{ |
20
|
4 |
|
public function process(ContainerBuilder $container) |
21
|
|
|
{ |
22
|
4 |
|
if (false === $container->hasDefinition('dtc_queue.manager.worker')) { |
23
|
4 |
|
return; |
24
|
|
|
} |
25
|
|
|
|
26
|
4 |
|
$this->setupAliases($container); |
27
|
|
|
|
28
|
4 |
|
$definition = $container->getDefinition('dtc_queue.manager.worker'); |
29
|
|
|
|
30
|
4 |
|
$jobClass = $this->getJobClass($container); |
31
|
3 |
|
$jobArchiveClass = $this->getJobClassArchive($container); |
32
|
3 |
|
$container->setParameter('dtc_queue.class.job', $jobClass); |
33
|
3 |
|
$container->setParameter('dtc_queue.class.job_archive', $jobArchiveClass); |
34
|
|
|
|
35
|
3 |
|
$managerType = $this->getRunManagerType($container); |
36
|
3 |
|
$jobTimingManagerType = $this->getJobTimingManagerType($container); |
37
|
3 |
|
$container->setParameter('dtc_queue.class.job_timing', $this->getClass( |
38
|
3 |
|
$container, |
39
|
3 |
|
$jobTimingManagerType, |
40
|
3 |
|
'job_timing', |
41
|
3 |
|
'JobTiming', |
42
|
3 |
|
JobTiming::class |
43
|
|
|
)); |
44
|
3 |
|
$container->setParameter('dtc_queue.class.run', $this->getClass($container, $managerType, 'run', 'Run', Run::class)); |
45
|
3 |
|
$container->setParameter('dtc_queue.class.run_archive', $this->getClass($container, $managerType, 'run_archive', 'RunArchive', Run::class)); |
46
|
|
|
|
47
|
3 |
|
$this->setupTaggedServices($container, $definition); |
48
|
2 |
|
$eventDispatcher = $container->getDefinition('dtc_queue.event_dispatcher'); |
49
|
2 |
|
foreach ($container->findTaggedServiceIds('dtc_queue.event_subscriber') as $id => $attributes) { |
50
|
|
|
$eventSubscriber = $container->getDefinition($id); |
51
|
|
|
$eventDispatcher->addMethodCall('addSubscriber', [$eventSubscriber]); |
52
|
|
|
} |
53
|
2 |
|
$this->setupDoctrineManagers($container); |
54
|
2 |
|
$this->addLiveJobs($container); |
55
|
2 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Add any extra method calls needed. |
59
|
|
|
* |
60
|
|
|
* @param ContainerBuilder $container |
61
|
|
|
* @param string $defaultManagerType |
62
|
|
|
*/ |
63
|
4 |
|
protected function addMethodCalls(ContainerBuilder $container, $defaultManagerType) |
64
|
|
|
{ |
65
|
4 |
|
if ('orm' === $defaultManagerType) { |
66
|
1 |
|
$doctrine = $container->getDefinition('doctrine'); |
67
|
1 |
|
$container->getDefinition('dtc_queue.doctrine_listener')->addMethodCall('setRegistry', [$doctrine]); |
68
|
1 |
|
$container->getDefinition('dtc_queue.manager.job.orm')->addMethodCall('setRegistry', [$doctrine]); |
69
|
1 |
|
$container->getDefinition('dtc_queue.manager.run.orm')->addMethodCall('setRegistry', [$doctrine]); |
70
|
1 |
|
$container->getDefinition('dtc_queue.manager.job_timing.orm')->addMethodCall('setRegistry', [$doctrine]); |
71
|
|
|
} |
72
|
4 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $type |
76
|
|
|
*/ |
77
|
4 |
|
protected function setupAlias(ContainerBuilder $container, $defaultManagerType, $type) |
78
|
|
|
{ |
79
|
4 |
|
$definitionName = 'dtc_queue.'.$type.'.'.$defaultManagerType; |
80
|
4 |
|
if (!$container->hasDefinition($definitionName) && !$container->hasAlias($definitionName)) { |
81
|
|
|
throw new InvalidConfigurationException("No $type manager found for dtc_queue.$type.$defaultManagerType"); |
82
|
|
|
} |
83
|
4 |
|
if ($container->hasDefinition($definitionName)) { |
84
|
4 |
|
$alias = new Alias('dtc_queue.'.$type.'.'.$defaultManagerType); |
85
|
4 |
|
$alias->setPublic(true); |
86
|
4 |
|
$container->setAlias('dtc_queue.'.$type, $alias); |
87
|
|
|
|
88
|
4 |
|
return; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$container->getAlias($definitionName)->setPublic(true); |
92
|
|
|
$container->setAlias('dtc_queue.'.$type, $container->getAlias($definitionName)); |
93
|
|
|
} |
94
|
|
|
|
95
|
4 |
|
protected function setupAliases(ContainerBuilder $container) |
96
|
|
|
{ |
97
|
4 |
|
$defaultManagerType = $container->getParameter('dtc_queue.manager.job'); |
98
|
4 |
|
$this->addMethodCalls($container, $defaultManagerType); |
99
|
4 |
|
$this->setupAlias($container, $defaultManagerType, 'manager.job'); |
100
|
4 |
|
$runManagerType = $container->getParameter($this->getRunManagerType($container)); |
101
|
4 |
|
$this->addMethodCalls($container, $runManagerType); |
102
|
4 |
|
$this->setupAlias($container, $runManagerType, 'manager.run'); |
103
|
4 |
|
$jobTimingManagerType = $container->getParameter($this->getJobTimingManagerType($container)); |
104
|
4 |
|
$this->addMethodCalls($container, $jobTimingManagerType); |
105
|
4 |
|
$this->setupAlias($container, $jobTimingManagerType, 'manager.job_timing'); |
106
|
4 |
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param ContainerBuilder $container |
110
|
|
|
* @param Definition $definition |
111
|
|
|
*/ |
112
|
3 |
|
protected function setupTaggedServices(ContainerBuilder $container, Definition $definition) |
113
|
|
|
{ |
114
|
3 |
|
$jobManagerRef = array(new Reference('dtc_queue.manager.job')); |
115
|
|
|
// Add each worker to workerManager, make sure each worker has instance to work |
116
|
3 |
|
foreach ($container->findTaggedServiceIds('dtc_queue.worker') as $id => $attributes) { |
117
|
2 |
|
$worker = $container->getDefinition($id); |
118
|
2 |
|
$class = $container->getDefinition($id)->getClass(); |
119
|
|
|
|
120
|
2 |
|
$refClass = new \ReflectionClass($class); |
121
|
2 |
|
$workerClass = 'Dtc\QueueBundle\Model\Worker'; |
122
|
2 |
|
if (!$refClass->isSubclassOf($workerClass)) { |
123
|
1 |
|
throw new \InvalidArgumentException(sprintf('Service "%s" must extend class "%s".', $id, $workerClass)); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
// Give each worker access to job manager |
127
|
1 |
|
$worker->addMethodCall('setJobManager', $jobManagerRef); |
128
|
1 |
|
$definition->addMethodCall('addWorker', array(new Reference($id))); |
129
|
|
|
} |
130
|
2 |
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param ContainerBuilder $container |
134
|
|
|
*/ |
135
|
2 |
|
protected function setupDoctrineManagers(ContainerBuilder $container) |
136
|
|
|
{ |
137
|
2 |
|
$documentManager = $container->getParameter('dtc_queue.odm.document_manager'); |
138
|
|
|
|
139
|
2 |
|
$odmManager = "doctrine_mongodb.odm.{$documentManager}_document_manager"; |
140
|
2 |
|
if ($container->has($odmManager)) { |
141
|
|
|
$container->setAlias('dtc_queue.document_manager', $odmManager); |
142
|
|
|
} |
143
|
|
|
|
144
|
2 |
|
$entityManager = $container->getParameter('dtc_queue.orm.entity_manager'); |
145
|
|
|
|
146
|
2 |
|
$ormManager = "doctrine.orm.{$entityManager}_entity_manager"; |
147
|
2 |
|
if ($container->has($ormManager)) { |
148
|
|
|
$container->setAlias('dtc_queue.entity_manager', $ormManager); |
149
|
|
|
} |
150
|
2 |
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param ContainerBuilder $container |
154
|
|
|
*/ |
155
|
2 |
|
protected function addLiveJobs(ContainerBuilder $container) |
156
|
|
|
{ |
157
|
2 |
|
$jobReflection = new \ReflectionClass($container->getParameter('dtc_queue.class.job')); |
158
|
2 |
|
if ($jobReflection->isSubclassOf(\Dtc\QueueBundle\Document\BaseJob::class)) { |
159
|
2 |
|
GridSourceCompilerPass::addGridSource($container, 'dtc_queue.grid_source.jobs_waiting.odm'); |
160
|
2 |
|
GridSourceCompilerPass::addGridSource($container, 'dtc_queue.grid_source.jobs_running.odm'); |
161
|
|
|
} |
162
|
2 |
|
if ($jobReflection->isSubclassOf(\Dtc\QueueBundle\Entity\BaseJob::class)) { |
163
|
1 |
|
GridSourceCompilerPass::addGridSource($container, 'dtc_queue.grid_source.jobs_waiting.orm'); |
164
|
1 |
|
GridSourceCompilerPass::addGridSource($container, 'dtc_queue.grid_source.jobs_running.orm'); |
165
|
|
|
} |
166
|
2 |
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param $managerType |
170
|
|
|
* |
171
|
|
|
* @return null|string |
172
|
|
|
*/ |
173
|
4 |
|
protected function getDirectory($managerType) |
174
|
|
|
{ |
175
|
|
|
switch ($managerType) { |
176
|
4 |
|
case 'odm': |
177
|
3 |
|
return 'Document'; |
178
|
2 |
|
case 'beanstalkd': |
179
|
1 |
|
return 'Beanstalkd'; |
180
|
2 |
|
case 'rabbit_mq': |
181
|
1 |
|
return 'RabbitMQ'; |
182
|
2 |
|
case 'orm': |
183
|
1 |
|
return 'Entity'; |
184
|
2 |
|
case 'redis': |
185
|
1 |
|
return 'Redis'; |
186
|
|
|
} |
187
|
|
|
|
188
|
1 |
|
return null; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Determines the job class based on the queue manager type. |
193
|
|
|
* |
194
|
|
|
* @param ContainerBuilder $container |
195
|
|
|
* |
196
|
|
|
* @return mixed|string |
197
|
|
|
* |
198
|
|
|
* @throws InvalidConfigurationException |
199
|
|
|
*/ |
200
|
4 |
|
protected function getJobClass(ContainerBuilder $container) |
201
|
|
|
{ |
202
|
4 |
|
$jobClass = $container->getParameter('dtc_queue.class.job'); |
203
|
4 |
|
if (!$jobClass) { |
204
|
4 |
|
if ($directory = $this->getDirectory($managerType = $container->getParameter('dtc_queue.manager.job'))) { |
205
|
3 |
|
$jobClass = 'Dtc\QueueBundle\\'.$directory.'\Job'; |
206
|
|
|
} else { |
207
|
1 |
|
throw new InvalidConfigurationException('Unknown manager.job type '.$managerType.' - please specify a Job class in the \'class\' configuration parameter'); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
3 |
|
$this->testClass($jobClass, Job::class); |
212
|
|
|
|
213
|
3 |
|
return $jobClass; |
214
|
|
|
} |
215
|
|
|
|
216
|
4 |
|
protected function getRunManagerType(ContainerBuilder $container) |
217
|
|
|
{ |
218
|
4 |
|
$managerType = 'dtc_queue.manager.job'; |
219
|
4 |
|
if ($container->hasParameter('dtc_queue.manager.run')) { |
220
|
4 |
|
$managerType = 'dtc_queue.manager.run'; |
221
|
|
|
} |
222
|
|
|
|
223
|
4 |
|
return $managerType; |
224
|
|
|
} |
225
|
|
|
|
226
|
4 |
|
protected function getJobTimingManagerType(ContainerBuilder $container) |
227
|
|
|
{ |
228
|
4 |
|
$managerType = $this->getRunManagerType($container); |
229
|
4 |
|
if ($container->hasParameter('dtc_queue.manager.job_timing')) { |
230
|
4 |
|
$managerType = 'dtc_queue.manager.job_timing'; |
231
|
|
|
} |
232
|
|
|
|
233
|
4 |
|
return $managerType; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @param string $managerType |
238
|
|
|
* @param string $type |
239
|
|
|
* @param string $className |
240
|
|
|
*/ |
241
|
3 |
|
protected function getClass(ContainerBuilder $container, $managerType, $type, $className, $baseClass) |
242
|
|
|
{ |
243
|
3 |
|
$runClass = $container->hasParameter('dtc_queue.class.'.$type) ? $container->getParameter('dtc_queue.class.'.$type) : null; |
244
|
3 |
|
if (!$runClass) { |
245
|
3 |
|
switch ($container->getParameter($managerType)) { |
246
|
3 |
|
case 'odm': |
247
|
3 |
|
$runClass = 'Dtc\\QueueBundle\\Document\\'.$className; |
248
|
3 |
|
break; |
249
|
1 |
|
case 'orm': |
250
|
1 |
|
$runClass = 'Dtc\\QueueBundle\\Entity\\'.$className; |
251
|
1 |
|
break; |
252
|
|
|
default: |
253
|
|
|
$runClass = $baseClass; |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
257
|
3 |
|
$this->testClass($runClass, $baseClass); |
258
|
|
|
|
259
|
3 |
|
return $runClass; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @throws ClassNotFoundException |
264
|
|
|
* @throws ClassNotSubclassException |
265
|
|
|
*/ |
266
|
3 |
|
protected function testClass($className, $parent) |
267
|
|
|
{ |
268
|
3 |
|
if (!class_exists($className)) { |
269
|
|
|
throw new ClassNotFoundException("Can't find class $className"); |
270
|
|
|
} |
271
|
|
|
|
272
|
3 |
|
$test = new $className(); |
273
|
3 |
|
if (!$test instanceof $className) { |
274
|
|
|
throw new ClassNotSubclassException("$className must be instance of (or derived from) $parent"); |
275
|
|
|
} |
276
|
3 |
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Determines the job class based on the queue manager type. |
280
|
|
|
* |
281
|
|
|
* @param ContainerBuilder $container |
282
|
|
|
* |
283
|
|
|
* @return mixed|string |
284
|
|
|
* |
285
|
|
|
* @throws ClassNotFoundException |
286
|
|
|
* @throws ClassNotSubclassException |
287
|
|
|
*/ |
288
|
3 |
|
protected function getJobClassArchive(ContainerBuilder $container) |
289
|
|
|
{ |
290
|
3 |
|
$jobArchiveClass = $container->getParameter('dtc_queue.class.job_archive'); |
291
|
3 |
|
if (!$jobArchiveClass) { |
292
|
3 |
|
switch ($container->getParameter('dtc_queue.manager.job')) { |
293
|
3 |
|
case 'odm': |
294
|
3 |
|
$jobArchiveClass = 'Dtc\\QueueBundle\\Document\\JobArchive'; |
295
|
3 |
|
break; |
296
|
1 |
|
case 'orm': |
297
|
1 |
|
$jobArchiveClass = 'Dtc\\QueueBundle\\Entity\\JobArchive'; |
298
|
1 |
|
break; |
299
|
|
|
} |
300
|
|
|
} |
301
|
3 |
|
if (null !== $jobArchiveClass) { |
302
|
3 |
|
$this->testClass($jobArchiveClass, Job::class); |
303
|
|
|
} |
304
|
|
|
|
305
|
3 |
|
return $jobArchiveClass; |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
|