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