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