1 | <?php |
||
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(){ |
||
92 | |||
93 | /** |
||
94 | * Init tasks structure |
||
95 | * |
||
96 | * @return GearmanClient self Object |
||
97 | */ |
||
98 | 2 | public function initTaskStructure() |
|
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) |
|
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) |
|
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) |
|
146 | |||
147 | /** |
||
148 | * Set default settings |
||
149 | * |
||
150 | * @param array $settings |
||
151 | * |
||
152 | * @return GearmanClient self Object |
||
153 | */ |
||
154 | 2 | public function setDefaultSettings($settings) |
|
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) |
||
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) |
||
195 | |||
196 | /** |
||
197 | * Clear server list |
||
198 | * |
||
199 | * @return GearmanClient Returns self object |
||
200 | */ |
||
201 | public function clearServers() |
||
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) |
||
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) |
||
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) |
||
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) |
||
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) |
||
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) |
||
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) |
||
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) |
||
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) |
||
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) |
||
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) |
||
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) |
||
456 | |||
457 | /** |
||
458 | * Gets the return code from the last run job. |
||
459 | * |
||
460 | * @return int |
||
461 | */ |
||
462 | public function getReturnCode() |
||
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) |
||
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) |
||
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) |
||
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) |
||
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) |
||
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) |
||
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) |
||
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) |
||
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() |
||
695 | } |
||
696 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.