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\Service; |
15
|
|
|
|
16
|
|
|
use Mkk\GearmanBundle\Dispatcher\GearmanCallbacksDispatcher; |
17
|
|
|
use Mkk\GearmanBundle\GearmanMethods; |
18
|
|
|
use Mkk\GearmanBundle\Generator\UniqueJobIdentifierGenerator; |
19
|
|
|
use Mkk\GearmanBundle\Module\JobStatus; |
20
|
|
|
use Mkk\GearmanBundle\Service\Abstracts\AbstractGearmanService; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* GearmanClient. Implementation of AbstractGearmanService |
24
|
|
|
*/ |
25
|
|
|
class GearmanClient extends AbstractGearmanService |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var \GearmanClient |
29
|
|
|
* |
30
|
|
|
* Gearman native client instance |
31
|
|
|
*/ |
32
|
|
|
protected $gearmanClient; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var GearmanCallbacksDispatcher |
36
|
|
|
* |
37
|
|
|
* Gearman callbacks dispatcher |
38
|
|
|
*/ |
39
|
|
|
protected $gearmanCallbacksDispatcher; |
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var array |
43
|
|
|
* |
44
|
|
|
* Server set to define in what server must connect to |
45
|
|
|
*/ |
46
|
|
|
protected $servers = array(); |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var array |
50
|
|
|
* |
51
|
|
|
* task structure to store all about called tasks |
52
|
|
|
*/ |
53
|
|
|
protected $taskStructure = array(); |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var array |
57
|
|
|
* |
58
|
|
|
* Set default servers |
59
|
|
|
*/ |
60
|
|
|
protected $defaultServers; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var array |
64
|
|
|
* |
65
|
|
|
* Set default settings |
66
|
|
|
*/ |
67
|
|
|
protected $settings; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var UniqueJobIdentifierGenerator |
71
|
|
|
* |
72
|
|
|
* Unique Job Intefier Generator |
73
|
|
|
*/ |
74
|
|
|
protected $uniqueJobIdentifierGenerator; |
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var int |
78
|
|
|
* |
79
|
|
|
* Return code from internal client. |
80
|
|
|
*/ |
81
|
|
|
protected $returnCode; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return \GearmanClient |
85
|
|
|
*/ |
86
|
|
|
public function getNativeClient(){ |
87
|
|
|
if ($this->gearmanClient === null){ |
88
|
|
|
$this->gearmanClient = new \GearmanClient(); |
89
|
|
|
} |
90
|
|
|
return $this->gearmanClient; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Init tasks structure |
95
|
|
|
* |
96
|
|
|
* @return GearmanClient self Object |
97
|
|
|
*/ |
98
|
2 |
|
public function initTaskStructure() |
99
|
|
|
{ |
100
|
2 |
|
$this->taskStructure = array(); |
101
|
|
|
|
102
|
2 |
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Set default servers |
107
|
|
|
* |
108
|
|
|
* @param array $defaultServers Default servers |
109
|
|
|
* |
110
|
|
|
* @return GearmanClient self Object |
111
|
|
|
*/ |
112
|
2 |
|
public function setDefaultServers(array $defaultServers) |
113
|
|
|
{ |
114
|
2 |
|
$this->defaultServers = $defaultServers; |
115
|
|
|
|
116
|
2 |
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Set UniqueJobIdentifierGenerator object |
121
|
|
|
* |
122
|
|
|
* @param UniqueJobIdentifierGenerator $uniqueJobIdentifierGenerator Unique Job Intefier Generator |
123
|
|
|
* |
124
|
|
|
* @return GearmanClient self Object |
125
|
|
|
*/ |
126
|
2 |
|
public function setUniqueJobIdentifierGenerator(UniqueJobIdentifierGenerator $uniqueJobIdentifierGenerator) |
|
|
|
|
127
|
|
|
{ |
128
|
2 |
|
$this->uniqueJobIdentifierGenerator = $uniqueJobIdentifierGenerator; |
129
|
|
|
|
130
|
2 |
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Set gearman callbacks |
135
|
|
|
* |
136
|
|
|
* @param GearmanCallbacksDispatcher $gearmanCallbacksDispatcher Gearman callbacks dispatcher |
137
|
|
|
* |
138
|
|
|
* @return GearmanClient self Object |
139
|
|
|
*/ |
140
|
2 |
|
public function setGearmanCallbacksDispatcher(GearmanCallbacksDispatcher $gearmanCallbacksDispatcher) |
|
|
|
|
141
|
|
|
{ |
142
|
2 |
|
$this->gearmanCallbacksDispatcher = $gearmanCallbacksDispatcher; |
143
|
|
|
|
144
|
2 |
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Set default settings |
149
|
|
|
* |
150
|
|
|
* @param array $settings |
151
|
|
|
* |
152
|
|
|
* @return GearmanClient self Object |
153
|
|
|
*/ |
154
|
2 |
|
public function setDefaultSettings($settings) |
155
|
|
|
{ |
156
|
2 |
|
$this->settings = $settings; |
157
|
|
|
|
158
|
2 |
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Set server to client. Empty all servers and set this one |
163
|
|
|
* |
164
|
|
|
* @param string $servername Server name (must be ip) |
165
|
|
|
* @param int $port Port of server. By default 4730 |
166
|
|
|
* |
167
|
|
|
* @return GearmanClient Returns self object |
168
|
|
|
*/ |
169
|
|
|
public function setServer($servername, $port = 4730) |
170
|
|
|
{ |
171
|
|
|
$this |
172
|
|
|
->clearServers() |
173
|
|
|
->addServer($servername, $port); |
174
|
|
|
|
175
|
|
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Add server to client |
180
|
|
|
* |
181
|
|
|
* @param string $servername Server name (must be ip) |
182
|
|
|
* @param int $port Port of server. By default 4730 |
183
|
|
|
* |
184
|
|
|
* @return GearmanClient Returns self object |
185
|
|
|
*/ |
186
|
|
|
public function addServer($servername, $port = 4730) |
187
|
|
|
{ |
188
|
|
|
$this->servers[] = array( |
189
|
|
|
'host' => $servername, |
190
|
|
|
'port' => $port, |
191
|
|
|
); |
192
|
|
|
|
193
|
|
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Clear server list |
198
|
|
|
* |
199
|
|
|
* @return GearmanClient Returns self object |
200
|
|
|
*/ |
201
|
|
|
public function clearServers() |
202
|
|
|
{ |
203
|
|
|
$this->servers = array(); |
204
|
|
|
|
205
|
|
|
return $this; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Get real worker from job name and enqueues the action given one |
210
|
|
|
* method. |
211
|
|
|
* |
212
|
|
|
* @param string $jobName A GearmanBundle registered function the worker is to execute |
213
|
|
|
* @param string $params Parameters to send to job as string |
214
|
|
|
* @param string $method Method to execute |
215
|
|
|
* @param string $unique A unique ID used to identify a particular task |
216
|
|
|
* |
217
|
|
|
* @return mixed Return result of the call. If worker is not valid, return false |
218
|
|
|
*/ |
219
|
|
|
protected function enqueue($jobName, $params, $method, $unique) |
220
|
|
|
{ |
221
|
|
|
$worker = $this->getJob($jobName); |
222
|
|
|
|
223
|
|
|
$unique = $this |
|
|
|
|
224
|
|
|
->uniqueJobIdentifierGenerator |
225
|
|
|
->generateUniqueKey($jobName, $params, $unique, $method); |
226
|
|
|
|
227
|
|
|
return $worker |
228
|
|
|
? $this->doEnqueue($worker, $params, $method, $unique) |
229
|
|
|
: false; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Execute a GearmanClient call given a worker, params and a method. |
234
|
|
|
* |
235
|
|
|
* If he GarmanClient call is asyncronous, result value will be a handler. |
236
|
|
|
* Otherwise, will return job result. |
237
|
|
|
* |
238
|
|
|
* @param array $worker Worker definition |
239
|
|
|
* @param string $params Parameters to send to job as string |
240
|
|
|
* @param string $method Method to execute |
241
|
|
|
* @param string $unique A unique ID used to identify a particular task |
242
|
|
|
* |
243
|
|
|
* @return mixed Return result of the GearmanClient call |
244
|
|
|
*/ |
245
|
|
|
protected function doEnqueue(array $worker, $params, $method, $unique) |
246
|
|
|
{ |
247
|
|
|
$gearmanClient = $this->getNativeClient(); |
248
|
|
|
$this->assignServers($gearmanClient); |
249
|
|
|
|
250
|
|
|
$result = $gearmanClient->$method($worker['job']['realCallableName'], $params, $unique); |
251
|
|
|
$this->returnCode = $gearmanClient->returnCode(); |
252
|
|
|
|
253
|
|
|
$this->gearmanClient = null; |
254
|
|
|
|
255
|
|
|
return $result; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Given a GearmanClient, set all included servers |
260
|
|
|
* |
261
|
|
|
* @param \GearmanClient $gearmanClient Object to include servers |
262
|
|
|
* |
263
|
|
|
* @return GearmanClient Returns self object |
264
|
|
|
*/ |
265
|
|
|
protected function assignServers(\GearmanClient $gearmanClient) |
266
|
|
|
{ |
267
|
|
|
$servers = $this->defaultServers; |
268
|
|
|
|
269
|
|
|
if (!empty($this->servers)) { |
270
|
|
|
|
271
|
|
|
$servers = $this->servers; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* We include each server into gearman client |
276
|
|
|
*/ |
277
|
|
|
foreach ($servers as $server) { |
278
|
|
|
|
279
|
|
|
$gearmanClient->addServer($server['host'], $server['port']); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
return $this; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Job methods |
287
|
|
|
*/ |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Runs a single task and returns some result, depending of method called. |
291
|
|
|
* Method called depends of default callable method setted on gearman |
292
|
|
|
* settings or overwritted on work or job annotations |
293
|
|
|
* |
294
|
|
|
* @param string $name A GearmanBundle registered function the worker is to execute |
295
|
|
|
* @param string $params Parameters to send to job as string |
296
|
|
|
* @param string $unique A unique ID used to identify a particular task |
|
|
|
|
297
|
|
|
* |
298
|
|
|
* @return mixed result depending of method called. |
299
|
|
|
*/ |
300
|
|
|
public function callJob($name, $params = '', $unique = null) |
301
|
|
|
{ |
302
|
|
|
$worker = $this->getJob($name); |
303
|
|
|
$methodCallable = $worker['job']['defaultMethod']; |
304
|
|
|
|
305
|
|
|
return $this->enqueue($name, $params, $methodCallable, $unique); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Runs a single task and returns a string representation of the result. |
310
|
|
|
* It is up to the GearmanClient and GearmanWorker to agree on the format of |
311
|
|
|
* the result. |
312
|
|
|
* |
313
|
|
|
* The GearmanClient::do() method is deprecated as of pecl/gearman 1.0.0. |
314
|
|
|
* Use GearmanClient::doNormal(). |
315
|
|
|
* |
316
|
|
|
* @param string $name A GearmanBundle registered function the worker is to execute |
317
|
|
|
* @param string $params Parameters to send to job as string |
318
|
|
|
* @param string $unique A unique ID used to identify a particular task |
|
|
|
|
319
|
|
|
* |
320
|
|
|
* @return string A string representing the results of running a task. |
321
|
|
|
* @deprecated |
322
|
|
|
*/ |
323
|
|
|
public function doJob($name, $params = '', $unique = null) |
324
|
|
|
{ |
325
|
|
|
return $this->enqueue($name, $params, GearmanMethods::GEARMAN_METHOD_DONORMAL, $unique); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Runs a single task and returns a string representation of the result. |
330
|
|
|
* It is up to the GearmanClient and GearmanWorker to agree on the format of |
331
|
|
|
* the result. |
332
|
|
|
* |
333
|
|
|
* @param string $name A GearmanBundle registered function the worker is to execute |
334
|
|
|
* @param string $params Parameters to send to job as string |
335
|
|
|
* @param string $unique A unique ID used to identify a particular task |
|
|
|
|
336
|
|
|
* |
337
|
|
|
* @return string A string representing the results of running a task. |
338
|
|
|
*/ |
339
|
|
|
public function doNormalJob($name, $params = '', $unique = null) |
340
|
|
|
{ |
341
|
|
|
return $this->enqueue($name, $params, GearmanMethods::GEARMAN_METHOD_DONORMAL, $unique); |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* Runs a task in the background, returning a job handle which can be used |
346
|
|
|
* to get the status of the running task. |
347
|
|
|
* |
348
|
|
|
* @param string $name A GearmanBundle registered function the worker is to execute |
349
|
|
|
* @param string $params Parameters to send to job as string |
350
|
|
|
* @param string $unique A unique ID used to identify a particular task |
|
|
|
|
351
|
|
|
* |
352
|
|
|
* @return string Job handle for the submitted task. |
353
|
|
|
*/ |
354
|
|
|
public function doBackgroundJob($name, $params = '', $unique = null) |
355
|
|
|
{ |
356
|
|
|
return $this->enqueue($name, $params, GearmanMethods::GEARMAN_METHOD_DOBACKGROUND, $unique); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* Runs a single high priority task and returns a string representation of |
361
|
|
|
* the result. |
362
|
|
|
* |
363
|
|
|
* It is up to the GearmanClient and GearmanWorker to agree on the format of |
364
|
|
|
* the result. |
365
|
|
|
* |
366
|
|
|
* High priority tasks will get precedence over normal and low priority |
367
|
|
|
* tasks in the job queue. |
368
|
|
|
* |
369
|
|
|
* @param string $name A GearmanBundle registered function the worker is to execute |
370
|
|
|
* @param string $params Parameters to send to job as string |
371
|
|
|
* @param string $unique A unique ID used to identify a particular task |
|
|
|
|
372
|
|
|
* |
373
|
|
|
* @return string A string representing the results of running a task. |
374
|
|
|
*/ |
375
|
|
|
public function doHighJob($name, $params = '', $unique = null) |
376
|
|
|
{ |
377
|
|
|
return $this->enqueue($name, $params, GearmanMethods::GEARMAN_METHOD_DOHIGH, $unique); |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* Runs a high priority task in the background, returning a job handle which |
382
|
|
|
* can be used to get the status of the running task. |
383
|
|
|
* |
384
|
|
|
* High priority tasks take precedence over normal and low priority tasks in |
385
|
|
|
* the job queue. |
386
|
|
|
* |
387
|
|
|
* @param string $name A GearmanBundle registered function the worker is to execute |
388
|
|
|
* @param string $params Parameters to send to job as string |
389
|
|
|
* @param string $unique A unique ID used to identify a particular task |
|
|
|
|
390
|
|
|
* |
391
|
|
|
* @return string The job handle for the submitted task. |
392
|
|
|
*/ |
393
|
|
|
public function doHighBackgroundJob($name, $params = '', $unique = null) |
394
|
|
|
{ |
395
|
|
|
return $this->enqueue($name, $params, GearmanMethods::GEARMAN_METHOD_DOHIGHBACKGROUND, $unique); |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* Runs a single low priority task and returns a string representation of |
400
|
|
|
* the result. |
401
|
|
|
* |
402
|
|
|
* It is up to the GearmanClient and GearmanWorker to agree on the format of |
403
|
|
|
* the result. |
404
|
|
|
* |
405
|
|
|
* Normal and high priority tasks will get precedence over low priority |
406
|
|
|
* tasks in the job queue. |
407
|
|
|
* |
408
|
|
|
* @param string $name A GearmanBundle registered function the worker is to execute |
409
|
|
|
* @param string $params Parameters to send to job as string |
410
|
|
|
* @param string $unique A unique ID used to identify a particular task |
|
|
|
|
411
|
|
|
* |
412
|
|
|
* @return string A string representing the results of running a task. |
413
|
|
|
*/ |
414
|
|
|
public function doLowJob($name, $params = '', $unique = null) |
415
|
|
|
{ |
416
|
|
|
return $this->enqueue($name, $params, GearmanMethods::GEARMAN_METHOD_DOLOW, $unique); |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* Runs a low priority task in the background, returning a job handle which |
421
|
|
|
* can be used to get the status of the running task. |
422
|
|
|
* |
423
|
|
|
* Normal and high priority tasks will get precedence over low priority |
424
|
|
|
* tasks in the job queue. |
425
|
|
|
* |
426
|
|
|
* @param string $name A GearmanBundle registered function the worker is to execute |
427
|
|
|
* @param string $params Parameters to send to job as string |
428
|
|
|
* @param string $unique A unique ID used to identify a particular task |
|
|
|
|
429
|
|
|
* |
430
|
|
|
* @return string The job handle for the submitted task. |
431
|
|
|
*/ |
432
|
|
|
public function doLowBackgroundJob($name, $params = '', $unique = null) |
433
|
|
|
{ |
434
|
|
|
return $this->enqueue($name, $params, GearmanMethods::GEARMAN_METHOD_DOLOWBACKGROUND, $unique); |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
/** |
438
|
|
|
* Fetches the Status of a special Background Job. |
439
|
|
|
* |
440
|
|
|
* @param string $idJob The job handle string |
441
|
|
|
* |
442
|
|
|
* @return JobStatus Job status |
443
|
|
|
*/ |
444
|
|
|
public function getJobStatus($idJob) |
445
|
|
|
{ |
446
|
|
|
$gearmanClient = $this->getNativeClient(); |
447
|
|
|
$this->assignServers($gearmanClient); |
448
|
|
|
$statusData = $gearmanClient->jobStatus($idJob); |
449
|
|
|
|
450
|
|
|
$jobStatus = new JobStatus($statusData); |
451
|
|
|
|
452
|
|
|
$this->gearmanClient = null; |
453
|
|
|
|
454
|
|
|
return $jobStatus; |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
/** |
458
|
|
|
* Gets the return code from the last run job. |
459
|
|
|
* |
460
|
|
|
* @return int |
461
|
|
|
*/ |
462
|
|
|
public function getReturnCode() |
463
|
|
|
{ |
464
|
|
|
return $this->returnCode; |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
/** |
468
|
|
|
* Task methods |
469
|
|
|
*/ |
470
|
|
|
|
471
|
|
|
/** |
472
|
|
|
* Adds a task to be run in parallel with other tasks. |
473
|
|
|
* Call this method for all the tasks to be run in parallel, then call |
474
|
|
|
* GearmanClient::runTasks() to perform the work. |
475
|
|
|
* |
476
|
|
|
* Note that enough workers need to be available for the tasks to all run in |
477
|
|
|
* parallel. |
478
|
|
|
* |
479
|
|
|
* @param string $name A GermanBundle registered function to be executed |
480
|
|
|
* @param string $params Parameters to send to task as string |
481
|
|
|
* @param Mixed &$context Application context to associate with a task |
482
|
|
|
* @param string $unique A unique ID used to identify a particular task |
|
|
|
|
483
|
|
|
* |
484
|
|
|
* @return GearmanClient Return this object |
485
|
|
|
*/ |
486
|
|
|
public function addTask($name, $params = '', &$context = null, $unique = null) |
487
|
|
|
{ |
488
|
|
|
$this->enqueueTask($name, $params, $context, $unique, GearmanMethods::GEARMAN_METHOD_ADDTASK); |
489
|
|
|
|
490
|
|
|
return $this; |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
/** |
494
|
|
|
* Adds a high priority task to be run in parallel with other tasks. |
495
|
|
|
* Call this method for all the high priority tasks to be run in parallel, |
496
|
|
|
* then call GearmanClient::runTasks() to perform the work. |
497
|
|
|
* |
498
|
|
|
* Tasks with a high priority will be selected from the queue before those |
499
|
|
|
* of normal or low priority. |
500
|
|
|
* |
501
|
|
|
* @param string $name A GermanBundle registered function to be executed |
502
|
|
|
* @param string $params Parameters to send to task as string |
503
|
|
|
* @param Mixed &$context Application context to associate with a task |
504
|
|
|
* @param string $unique A unique ID used to identify a particular task |
|
|
|
|
505
|
|
|
* |
506
|
|
|
* @return GearmanClient Return this object |
507
|
|
|
*/ |
508
|
|
|
public function addTaskHigh($name, $params = '', &$context = null, $unique = null) |
509
|
|
|
{ |
510
|
|
|
$this->enqueueTask($name, $params, $context, $unique, GearmanMethods::GEARMAN_METHOD_ADDTASKHIGH); |
511
|
|
|
|
512
|
|
|
return $this; |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
/** |
516
|
|
|
* Adds a low priority background task to be run in parallel with other |
517
|
|
|
* tasks. |
518
|
|
|
* |
519
|
|
|
* Call this method for all the tasks to be run in parallel, then call |
520
|
|
|
* GearmanClient::runTasks() to perform the work. |
521
|
|
|
* |
522
|
|
|
* Tasks with a low priority will be selected from the queue after those of |
523
|
|
|
* normal or low priority. |
524
|
|
|
* |
525
|
|
|
* @param string $name A GermanBundle registered function to be executed |
526
|
|
|
* @param string $params Parameters to send to task as string |
527
|
|
|
* @param Mixed &$context Application context to associate with a task |
528
|
|
|
* @param string $unique A unique ID used to identify a particular task |
|
|
|
|
529
|
|
|
* |
530
|
|
|
* @return GearmanClient Return this object |
531
|
|
|
*/ |
532
|
|
|
public function addTaskLow($name, $params = '', &$context = null, $unique = null) |
533
|
|
|
{ |
534
|
|
|
$this->enqueueTask($name, $params, $context, $unique, GearmanMethods::GEARMAN_METHOD_ADDTASKLOW); |
535
|
|
|
|
536
|
|
|
return $this; |
537
|
|
|
} |
538
|
|
|
|
539
|
|
|
/** |
540
|
|
|
* Adds a background task to be run in parallel with other tasks |
541
|
|
|
* Call this method for all the tasks to be run in parallel, then call |
542
|
|
|
* GearmanClient::runTasks() to perform the work. |
543
|
|
|
* |
544
|
|
|
* @param string $name A GermanBundle registered function to be executed |
545
|
|
|
* @param string $params Parameters to send to task as string |
546
|
|
|
* @param Mixed &$context Application context to associate with a task |
547
|
|
|
* @param string $unique A unique ID used to identify a particular task |
|
|
|
|
548
|
|
|
* |
549
|
|
|
* @return GearmanClient Return this object |
550
|
|
|
*/ |
551
|
|
|
public function addTaskBackground($name, $params = '', &$context = null, $unique = null) |
552
|
|
|
{ |
553
|
|
|
$this->enqueueTask($name, $params, $context, $unique, GearmanMethods::GEARMAN_METHOD_ADDTASKBACKGROUND); |
554
|
|
|
|
555
|
|
|
return $this; |
556
|
|
|
} |
557
|
|
|
|
558
|
|
|
/** |
559
|
|
|
* Adds a high priority background task to be run in parallel with other |
560
|
|
|
* tasks. |
561
|
|
|
* |
562
|
|
|
* Call this method for all the tasks to be run in parallel, then call |
563
|
|
|
* GearmanClient::runTasks() to perform the work. |
564
|
|
|
* |
565
|
|
|
* Tasks with a high priority will be selected from the queue before those |
566
|
|
|
* of normal or low priority. |
567
|
|
|
* |
568
|
|
|
* @param string $name A GermanBundle registered function to be executed |
569
|
|
|
* @param string $params Parameters to send to task as string |
570
|
|
|
* @param Mixed &$context Application context to associate with a task |
571
|
|
|
* @param string $unique A unique ID used to identify a particular task |
|
|
|
|
572
|
|
|
* |
573
|
|
|
* @return GearmanClient Return this object |
574
|
|
|
*/ |
575
|
|
|
public function addTaskHighBackground($name, $params = '', &$context = null, $unique = null) |
576
|
|
|
{ |
577
|
|
|
$this->enqueueTask($name, $params, $context, $unique, GearmanMethods::GEARMAN_METHOD_ADDTASKHIGHBACKGROUND); |
578
|
|
|
|
579
|
|
|
return $this; |
580
|
|
|
} |
581
|
|
|
|
582
|
|
|
/** |
583
|
|
|
* Adds a low priority background task to be run in parallel with other |
584
|
|
|
* tasks. |
585
|
|
|
* |
586
|
|
|
* Call this method for all the tasks to be run in parallel, then call |
587
|
|
|
* GearmanClient::runTasks() to perform the work. |
588
|
|
|
* |
589
|
|
|
* Tasks with a low priority will be selected from the queue after those of |
590
|
|
|
* normal or high priority. |
591
|
|
|
* |
592
|
|
|
* @param string $name A GermanBundle registered function to be executed |
593
|
|
|
* @param string $params Parameters to send to task as string |
594
|
|
|
* @param Mixed &$context Application context to associate with a task |
595
|
|
|
* @param string $unique A unique ID used to identify a particular task |
|
|
|
|
596
|
|
|
* |
597
|
|
|
* @return GearmanClient Return this object |
598
|
|
|
*/ |
599
|
|
|
public function addTaskLowBackground($name, $params = '', &$context = null, $unique = null) |
600
|
|
|
{ |
601
|
|
|
$this->enqueueTask($name, $params, $context, $unique, GearmanMethods::GEARMAN_METHOD_ADDTASKLOWBACKGROUND); |
602
|
|
|
|
603
|
|
|
return $this; |
604
|
|
|
} |
605
|
|
|
|
606
|
|
|
/** |
607
|
|
|
* Adds a task into the structure of tasks with included type of call |
608
|
|
|
* |
609
|
|
|
* @param string $name A GermanBundle registered function to be executed |
610
|
|
|
* @param string $params Parameters to send to task as string |
611
|
|
|
* @param Mixed $context Application context to associate with a task |
612
|
|
|
* @param string $unique A unique ID used to identify a particular task |
613
|
|
|
* @param string $method Method to perform |
614
|
|
|
* |
615
|
|
|
* @return GearmanClient Return this object |
616
|
|
|
*/ |
617
|
|
|
protected function enqueueTask($name, $params, &$context, $unique, $method) |
618
|
|
|
{ |
619
|
|
|
$contextReference = array('context' => &$context); |
620
|
|
|
$task = array( |
621
|
|
|
'name' => $name, |
622
|
|
|
'params' => $params, |
623
|
|
|
'context' => $contextReference, |
624
|
|
|
'unique' => $this->uniqueJobIdentifierGenerator->generateUniqueKey($name, $params, $unique, $method), |
625
|
|
|
'method' => $method, |
626
|
|
|
); |
627
|
|
|
|
628
|
|
|
$this->addTaskToStructure($task); |
629
|
|
|
|
630
|
|
|
return $this; |
631
|
|
|
} |
632
|
|
|
|
633
|
|
|
/** |
634
|
|
|
* Appends a task structure into taskStructure array |
635
|
|
|
* |
636
|
|
|
* @param array $task Task structure |
637
|
|
|
* |
638
|
|
|
* @return GearmanClient Return this object |
639
|
|
|
*/ |
640
|
|
|
protected function addTaskToStructure(array $task) |
641
|
|
|
{ |
642
|
|
|
$this->taskStructure[] = $task; |
643
|
|
|
|
644
|
|
|
return $this; |
645
|
|
|
} |
646
|
|
|
|
647
|
|
|
/** |
648
|
|
|
* For a set of tasks previously added with |
649
|
|
|
* |
650
|
|
|
* GearmanClient::addTask(), |
651
|
|
|
* GearmanClient::addTaskHigh(), |
652
|
|
|
* GearmanClient::addTaskLow(), |
653
|
|
|
* GearmanClient::addTaskBackground(), |
654
|
|
|
* GearmanClient::addTaskHighBackground(), |
655
|
|
|
* GearmanClient::addTaskLowBackground(), |
656
|
|
|
* |
657
|
|
|
* this call starts running the tasks in parallel. |
658
|
|
|
* Note that enough workers need to be available for the tasks to all run in parallel |
659
|
|
|
* |
660
|
|
|
* @return boolean run tasks result |
661
|
|
|
*/ |
662
|
|
|
public function runTasks() |
663
|
|
|
{ |
664
|
|
|
$gearmanClient = $this->getNativeClient(); |
665
|
|
|
$this->assignServers($gearmanClient); |
666
|
|
|
|
667
|
|
|
if ($this->settings['callbacks']) { |
668
|
|
|
|
669
|
|
|
$this->gearmanCallbacksDispatcher->assignTaskCallbacks($gearmanClient); |
670
|
|
|
} |
671
|
|
|
|
672
|
|
|
foreach ($this->taskStructure as $task) { |
673
|
|
|
|
674
|
|
|
$type = $task['method']; |
675
|
|
|
$jobName = $task['name']; |
676
|
|
|
$worker = $this->getJob($jobName); |
677
|
|
|
|
678
|
|
|
if (false !== $worker) { |
679
|
|
|
|
680
|
|
|
$gearmanClient->$type( |
681
|
|
|
$worker['job']['realCallableName'], |
682
|
|
|
$task['params'], |
683
|
|
|
$task['context'], |
684
|
|
|
$task['unique'] |
685
|
|
|
); |
686
|
|
|
} |
687
|
|
|
} |
688
|
|
|
|
689
|
|
|
$this->initTaskStructure(); |
690
|
|
|
|
691
|
|
|
$this->gearmanClient = null; |
692
|
|
|
|
693
|
|
|
return $gearmanClient->runTasks(); |
694
|
|
|
} |
695
|
|
|
} |
696
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.