1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Gearman Bundle for Symfony2 / Symfony3 |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
* |
9
|
|
|
* Feel free to edit as you please, and have fun. |
10
|
|
|
* |
11
|
|
|
* @author Marc Morera <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Mkk\GearmanBundle\Module; |
15
|
|
|
|
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
17
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
18
|
|
|
use Mkk\GearmanBundle\Driver\Gearman\Job as JobAnnotation; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Job class |
22
|
|
|
* |
23
|
|
|
* This class provide all worker definition. |
24
|
|
|
*/ |
25
|
|
|
class JobClass implements ContainerAwareInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
* |
30
|
|
|
* Default description when is not defined |
31
|
|
|
*/ |
32
|
|
|
const DEFAULT_DESCRIPTION = 'No description is defined'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
* |
37
|
|
|
* Callable name for this job |
38
|
|
|
* If is setted on annotations, this value will be used |
39
|
|
|
* otherwise, natural method name will be used. |
40
|
|
|
*/ |
41
|
|
|
private $callableName; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
* |
46
|
|
|
* Method name |
47
|
|
|
*/ |
48
|
|
|
private $methodName; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var string |
52
|
|
|
* |
53
|
|
|
* RealCallable name for this job without the job prefix |
54
|
|
|
* |
55
|
|
|
*/ |
56
|
|
|
private $realCallableNameNoPrefix; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var string |
60
|
|
|
* |
61
|
|
|
* RealCallable name for this job |
62
|
|
|
* natural method name will be used. |
63
|
|
|
*/ |
64
|
|
|
private $realCallableName; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var string |
68
|
|
|
* |
69
|
|
|
* Description of Job |
70
|
|
|
*/ |
71
|
|
|
private $description; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var integer |
75
|
|
|
* |
76
|
|
|
* Number of iterations this job will be alive before die |
77
|
|
|
*/ |
78
|
|
|
private $iterations; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @var string |
82
|
|
|
* |
83
|
|
|
* Default method this job will be call into Gearman client |
84
|
|
|
*/ |
85
|
|
|
private $defaultMethod; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @var int |
89
|
|
|
* |
90
|
|
|
* Job minimum execution time |
91
|
|
|
*/ |
92
|
|
|
private $minimumExecutionTime; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @var int |
96
|
|
|
* |
97
|
|
|
* Timeout for idle worker |
98
|
|
|
*/ |
99
|
|
|
private $timeout; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @var array |
103
|
|
|
* |
104
|
|
|
* Collection of servers to connect |
105
|
|
|
*/ |
106
|
|
|
private $servers; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @var string |
110
|
|
|
* |
111
|
|
|
* The prefix to be prepended to all job callable names. |
112
|
|
|
*/ |
113
|
|
|
private $jobPrefix; |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @var ContainerInterface |
117
|
|
|
*/ |
118
|
|
|
protected $container; |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Construct method |
122
|
|
|
* |
123
|
|
|
* @param JobAnnotation $jobAnnotation JobAnnotation class |
124
|
|
|
* @param \ReflectionMethod $reflectionMethod ReflextionMethod class |
125
|
|
|
* @param string $callableNameClass Callable name class |
126
|
|
|
* @param array $servers Array of servers defined for Worker |
127
|
|
|
* @param array $defaultSettings Default settings for Worker |
128
|
|
|
*/ |
129
|
6 |
|
public function __construct( |
130
|
|
|
JobAnnotation $jobAnnotation, |
131
|
|
|
\ReflectionMethod $reflectionMethod, |
132
|
|
|
$callableNameClass, |
133
|
|
|
array $servers, |
134
|
|
|
array $defaultSettings |
135
|
|
|
) |
136
|
|
|
{ |
137
|
|
|
|
138
|
6 |
|
$this->callableName = is_null($jobAnnotation->name) |
139
|
5 |
|
? $reflectionMethod->getName() |
140
|
1 |
|
: $jobAnnotation->name; |
141
|
|
|
|
142
|
6 |
|
$this->methodName = $reflectionMethod->getName(); |
|
|
|
|
143
|
6 |
|
$this->realCallableNameNoPrefix = is_null($jobAnnotation->realCallableName) |
144
|
4 |
|
? str_replace('\\', '', $callableNameClass . '~' . $this->callableName) |
145
|
2 |
|
: $jobAnnotation->realCallableName; |
146
|
|
|
|
147
|
6 |
|
$this->jobPrefix = isset($defaultSettings['jobPrefix']) |
148
|
2 |
|
? $defaultSettings['jobPrefix'] |
149
|
4 |
|
: null; |
150
|
|
|
|
151
|
6 |
|
$this->realCallableName = $this->jobPrefix . $this->realCallableNameNoPrefix; |
152
|
6 |
|
$this->description = is_null($jobAnnotation->description) |
|
|
|
|
153
|
5 |
|
? self::DEFAULT_DESCRIPTION |
154
|
1 |
|
: $jobAnnotation->description; |
155
|
|
|
|
156
|
6 |
|
$this->servers = $this->loadServers($jobAnnotation, $servers); |
|
|
|
|
157
|
6 |
|
$this->iterations = $this->loadIterations($jobAnnotation, $defaultSettings); |
|
|
|
|
158
|
6 |
|
$this->minimumExecutionTime = $this->loadMinimumExecutionTime($jobAnnotation, $defaultSettings); |
159
|
6 |
|
$this->timeout = $this->loadTimeout($jobAnnotation, $defaultSettings); |
|
|
|
|
160
|
6 |
|
$this->defaultMethod = $this->loadDefaultMethod($jobAnnotation, $defaultSettings); |
|
|
|
|
161
|
6 |
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Load servers |
165
|
|
|
* |
166
|
|
|
* If any server is defined in JobAnnotation, this one is used. |
167
|
|
|
* Otherwise is used servers set in Class |
168
|
|
|
* |
169
|
|
|
* @param JobAnnotation $jobAnnotation JobAnnotation class |
170
|
|
|
* @param array $servers Array of servers defined for Worker |
171
|
|
|
* |
172
|
|
|
* @return array Servers |
173
|
|
|
*/ |
174
|
6 |
|
private function loadServers(JobAnnotation $jobAnnotation, array $servers) |
175
|
|
|
{ |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* If is configured some servers definition in the worker, overwrites |
179
|
|
|
*/ |
180
|
6 |
|
if ($jobAnnotation->servers) { |
181
|
|
|
|
182
|
3 |
|
$servers = (is_array($jobAnnotation->servers) && !isset($jobAnnotation->servers['host'])) |
|
|
|
|
183
|
1 |
|
? $jobAnnotation->servers |
184
|
3 |
|
: array($jobAnnotation->servers); |
185
|
|
|
} |
186
|
|
|
|
187
|
6 |
|
return $servers; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Load iterations |
192
|
|
|
* |
193
|
|
|
* If iterations is defined in JobAnnotation, this one is used. |
194
|
|
|
* Otherwise is used set in Class |
195
|
|
|
* |
196
|
|
|
* @param JobAnnotation $jobAnnotation JobAnnotation class |
197
|
|
|
* @param array $defaultSettings Default settings for Worker |
198
|
|
|
* |
199
|
|
|
* @return integer Iteration |
200
|
|
|
*/ |
201
|
6 |
|
private function loadIterations(JobAnnotation $jobAnnotation, array $defaultSettings) |
202
|
|
|
{ |
203
|
6 |
|
return is_null($jobAnnotation->iterations) |
204
|
5 |
|
? (int) $defaultSettings['iterations'] |
205
|
6 |
|
: (int) $jobAnnotation->iterations; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Load defaultMethod |
210
|
|
|
* |
211
|
|
|
* If defaultMethod is defined in JobAnnotation, this one is used. |
212
|
|
|
* Otherwise is used set in Class |
213
|
|
|
* |
214
|
|
|
* @param JobAnnotation $jobAnnotation JobAnnotation class |
215
|
|
|
* @param array $defaultSettings Default settings for Worker |
216
|
|
|
* |
217
|
|
|
* @return string Default method |
218
|
|
|
*/ |
219
|
6 |
|
private function loadDefaultMethod(JobAnnotation $jobAnnotation, array $defaultSettings) |
220
|
|
|
{ |
221
|
6 |
|
return is_null($jobAnnotation->defaultMethod) |
222
|
5 |
|
? $defaultSettings['method'] |
223
|
6 |
|
: $jobAnnotation->defaultMethod; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Load minimumExecutionTime |
228
|
|
|
* |
229
|
|
|
* If minimumExecutionTime is defined in JobAnnotation, this one is used. |
230
|
|
|
* Otherwise is used set in Class |
231
|
|
|
* |
232
|
|
|
* @param JobAnnotation $jobAnnotation |
233
|
|
|
* @param array $defaultSettings |
234
|
|
|
* |
235
|
|
|
* @return int |
236
|
|
|
*/ |
237
|
6 |
|
private function loadMinimumExecutionTime(JobAnnotation $jobAnnotation, array $defaultSettings) |
238
|
|
|
{ |
239
|
6 |
|
return is_null($jobAnnotation->minimumExecutionTime) |
240
|
6 |
|
? (int) $defaultSettings['minimumExecutionTime'] |
241
|
6 |
|
: (int) $jobAnnotation->minimumExecutionTime; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Load timeout |
246
|
|
|
* |
247
|
|
|
* If timeout is defined in JobAnnotation, this one is used. |
248
|
|
|
* Otherwise is used set in Class |
249
|
|
|
* |
250
|
|
|
* @param JobAnnotation $jobAnnotation |
251
|
|
|
* @param array $defaultSettings |
252
|
|
|
* |
253
|
|
|
* @return int |
254
|
|
|
*/ |
255
|
6 |
|
private function loadTimeout(JobAnnotation $jobAnnotation, array $defaultSettings) |
256
|
|
|
{ |
257
|
6 |
|
return is_null($jobAnnotation->timeout) |
258
|
6 |
|
? (int) $defaultSettings['timeout'] |
259
|
6 |
|
: (int) $jobAnnotation->timeout; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Retrieve all Job data in cache format |
264
|
|
|
* |
265
|
|
|
* @return array |
|
|
|
|
266
|
|
|
*/ |
267
|
6 |
|
public function toArray() |
268
|
|
|
{ |
269
|
|
|
return array( |
270
|
|
|
|
271
|
6 |
|
'callableName' => $this->callableName, |
272
|
6 |
|
'methodName' => $this->methodName, |
273
|
6 |
|
'realCallableName' => $this->realCallableName, |
274
|
6 |
|
'jobPrefix' => $this->jobPrefix, |
275
|
6 |
|
'realCallableNameNoPrefix' => $this->realCallableNameNoPrefix, |
276
|
6 |
|
'description' => $this->description, |
277
|
6 |
|
'iterations' => $this->iterations, |
278
|
6 |
|
'minimumExecutionTime' => $this->minimumExecutionTime, |
279
|
6 |
|
'timeout' => $this->timeout, |
280
|
6 |
|
'servers' => $this->servers, |
281
|
6 |
|
'defaultMethod' => $this->defaultMethod, |
282
|
|
|
); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Sets the container. |
287
|
|
|
* |
288
|
|
|
* @param ContainerInterface|null $container A ContainerInterface instance or null |
289
|
|
|
*/ |
290
|
|
|
public function setContainer(ContainerInterface $container = null) |
291
|
|
|
{ |
292
|
|
|
$this->container = $container; |
293
|
|
|
} |
294
|
|
|
} |
|
|
|
|
295
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.