1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SfCod\QueueBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Psr\Log\LoggerInterface; |
6
|
|
|
use SfCod\QueueBundle\Command\RetryCommand; |
7
|
|
|
use SfCod\QueueBundle\Command\RunJobCommand; |
8
|
|
|
use SfCod\QueueBundle\Command\WorkCommand; |
9
|
|
|
use SfCod\QueueBundle\Failer\MongoFailedJobProvider; |
10
|
|
|
use SfCod\QueueBundle\Handler\ExceptionHandler; |
11
|
|
|
use SfCod\QueueBundle\Handler\ExceptionHandlerInterface; |
12
|
|
|
use SfCod\QueueBundle\JobProcess; |
13
|
|
|
use SfCod\QueueBundle\Service\JobQueue; |
14
|
|
|
use SfCod\QueueBundle\Service\MongoDriver; |
15
|
|
|
use SfCod\QueueBundle\Service\MongoDriverInterface; |
16
|
|
|
use SfCod\QueueBundle\Worker; |
17
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
18
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
19
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
20
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
21
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
22
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class SfcodQueueExtension |
26
|
|
|
* |
27
|
|
|
* @author Alexey Orlov <[email protected]> |
28
|
|
|
* @author Virchenko Maksim <[email protected]> |
29
|
|
|
* |
30
|
|
|
* @package SfCod\QueueBundle\DependencyInjection |
31
|
|
|
*/ |
32
|
|
|
class QueueExtension extends Extension |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* Loads a specific configuration. |
36
|
|
|
* |
37
|
|
|
* @param array $config |
38
|
|
|
* @param ContainerBuilder $container |
39
|
|
|
* |
40
|
|
|
* @throws \ReflectionException |
41
|
|
|
*/ |
42
|
|
|
public function load(array $config, ContainerBuilder $container) |
43
|
|
|
{ |
44
|
|
|
$configuration = new QueueConfiguration(); |
45
|
|
|
|
46
|
|
|
$config = $this->processConfiguration($configuration, $config); |
47
|
|
|
|
48
|
|
|
$jobs = $this->grabJobs($config, $container); |
49
|
|
|
|
50
|
|
|
foreach ($jobs as $job) { |
51
|
|
|
$definition = new Definition($job); |
52
|
|
|
$definition |
53
|
|
|
->setAutowired(true) |
54
|
|
|
->setAutoconfigured(true) |
55
|
|
|
->setPublic(true); |
56
|
|
|
$container->setDefinition($job, $definition); |
57
|
|
|
} |
58
|
|
|
$this->createDriver($config, $container); |
59
|
|
|
$this->createJobQueue($config, $container); |
60
|
|
|
$this->createWorker($config, $container); |
61
|
|
|
$this->createJobProcess($config, $container); |
62
|
|
|
$this->createCommands($config, $container); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get extension alias |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
public function getAlias() |
71
|
|
|
{ |
72
|
|
|
return 'sfcod_queue'; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param array $config |
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
private function grabJobs(array $config, ContainerBuilder $container): array |
81
|
|
|
{ |
82
|
|
|
$jobs = []; |
83
|
|
|
foreach ($config['namespaces'] as $key => $namespace) { |
84
|
|
|
$alias = $container->getParameter('kernel.root_dir') . '/../' . str_replace('\\', DIRECTORY_SEPARATOR, trim($namespace, '\\')); |
85
|
|
|
|
86
|
|
|
foreach (glob(sprintf('%s/**.php', $alias)) as $file) { |
87
|
|
|
$className = sprintf('%s\%s', $namespace, basename($file, '.php')); |
88
|
|
|
if (method_exists($className, 'fire')) { |
89
|
|
|
$jobs[] = $className; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $jobs; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Create command |
99
|
|
|
* |
100
|
|
|
* @param array $config |
101
|
|
|
* @param ContainerBuilder $container |
102
|
|
|
*/ |
103
|
|
|
private function createCommands(array $config, ContainerBuilder $container) |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
$work = new Definition(WorkCommand::class); |
106
|
|
|
$work->setArguments([ |
107
|
|
|
new Reference(Worker::class), |
108
|
|
|
]); |
109
|
|
|
$work->addTag('console.command'); |
110
|
|
|
|
111
|
|
|
$retry = new Definition(RetryCommand::class); |
112
|
|
|
$retry->setArguments([ |
113
|
|
|
new Reference(JobQueue::class), |
114
|
|
|
new Reference(MongoFailedJobProvider::class), |
115
|
|
|
]); |
116
|
|
|
$retry->addTag('console.command'); |
117
|
|
|
|
118
|
|
|
$runJob = new Definition(RunJobCommand::class); |
119
|
|
|
$runJob->setArguments([ |
120
|
|
|
new Reference(Worker::class), |
121
|
|
|
]); |
122
|
|
|
$runJob->addTag('console.command'); |
123
|
|
|
|
124
|
|
|
$container->addDefinitions([ |
125
|
|
|
WorkCommand::class => $work, |
126
|
|
|
RetryCommand::class => $retry, |
127
|
|
|
RunJobCommand::class => $runJob, |
128
|
|
|
]); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Create driver |
133
|
|
|
* |
134
|
|
|
* @param array $config |
135
|
|
|
* @param ContainerBuilder $container |
136
|
|
|
*/ |
137
|
|
|
private function createDriver(array $config, ContainerBuilder $container) |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
$mongo = new Definition(MongoDriver::class); |
140
|
|
|
$mongo->setPublic(true); |
141
|
|
|
$mongo->addMethodCall('setCredentials', [ |
142
|
|
|
getenv('MONGODB_URL'), |
143
|
|
|
]); |
144
|
|
|
$mongo->addMethodCall('setDbname', [ |
145
|
|
|
getenv('MONGODB_DB'), |
146
|
|
|
]); |
147
|
|
|
|
148
|
|
|
$container->setDefinition(MongoDriverInterface::class, $mongo); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Create job queue |
153
|
|
|
* |
154
|
|
|
* @param array $config |
155
|
|
|
* @param ContainerBuilder $container |
156
|
|
|
*/ |
157
|
|
|
private function createJobQueue(array $config, ContainerBuilder $container) |
158
|
|
|
{ |
159
|
|
|
$jobQueue = new Definition(JobQueue::class); |
160
|
|
|
$jobQueue->setPublic(true); |
161
|
|
|
$jobQueue->setArguments([ |
162
|
|
|
new Reference(ContainerInterface::class), |
163
|
|
|
$config['connections'], |
164
|
|
|
]); |
165
|
|
|
|
166
|
|
|
$container->setDefinition(JobQueue::class, $jobQueue); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Create worker |
171
|
|
|
* |
172
|
|
|
* @param array $config |
173
|
|
|
* @param ContainerBuilder $container |
174
|
|
|
*/ |
175
|
|
|
private function createWorker(array $config, ContainerBuilder $container) |
|
|
|
|
176
|
|
|
{ |
177
|
|
|
$worker = new Definition(Worker::class); |
178
|
|
|
$worker |
179
|
|
|
->setArguments([ |
180
|
|
|
new Reference(JobQueue::class), |
181
|
|
|
new Reference(JobProcess::class), |
182
|
|
|
new Reference(MongoFailedJobProvider::class), |
183
|
|
|
new Reference(ExceptionHandlerInterface::class), |
184
|
|
|
new Reference(EventDispatcherInterface::class), |
185
|
|
|
]); |
186
|
|
|
|
187
|
|
|
$failedProvider = new Definition(MongoFailedJobProvider::class); |
188
|
|
|
$failedProvider |
189
|
|
|
->setArguments([ |
190
|
|
|
new Reference(MongoDriverInterface::class), |
191
|
|
|
'queue_jobs_failed', |
192
|
|
|
]); |
193
|
|
|
|
194
|
|
|
$exceptionHandler = new Definition(ExceptionHandlerInterface::class); |
195
|
|
|
$exceptionHandler |
196
|
|
|
->setClass(ExceptionHandler::class) |
197
|
|
|
->setArguments([ |
198
|
|
|
new Reference(LoggerInterface::class), |
199
|
|
|
]); |
200
|
|
|
|
201
|
|
|
$container->addDefinitions([ |
202
|
|
|
Worker::class => $worker, |
203
|
|
|
MongoFailedJobProvider::class => $failedProvider, |
204
|
|
|
ExceptionHandlerInterface::class => $exceptionHandler, |
205
|
|
|
]); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Create job process |
210
|
|
|
* |
211
|
|
|
* @param array $config |
212
|
|
|
* @param ContainerBuilder $container |
213
|
|
|
*/ |
214
|
|
|
private function createJobProcess(array $config, ContainerBuilder $container) |
|
|
|
|
215
|
|
|
{ |
216
|
|
|
$jobProcess = new Definition(JobProcess::class); |
217
|
|
|
$jobProcess->setPublic(true); |
218
|
|
|
$jobProcess->setArguments([ |
219
|
|
|
'console', |
220
|
|
|
sprintf('%s/bin', $container->getParameter('kernel.project_dir')), |
221
|
|
|
]); |
222
|
|
|
|
223
|
|
|
$container->setDefinition(JobProcess::class, $jobProcess); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.