GearmanService.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace Mmoreram\GearmanBundle\Service;
  3. use Mmoreram\GearmanBundle\Service\GearmanCache as Cache;
  4. use Mmoreram\GearmanBundle\Exceptions\JobDoesNotExistException;
  5. use Mmoreram\GearmanBundle\Exceptions\WorkerDoesNotExistException;
  6. /**
  7. * Gearman execute methods. All Worker methods
  8. *
  9. * @author Marc Morera <yuhu@mmoreram.com>
  10. */
  11. abstract class GearmanService
  12. {
  13. /**
  14. * All workers
  15. *
  16. * @var type
  17. */
  18. protected $workers;
  19. /**
  20. * Construct method
  21. *
  22. * @param GearmanCacheWrapper $gearmanCacheWrapper GearmanCacheWrapper
  23. */
  24. public function __construct(GearmanCacheWrapper $gearmanCacheWrapper)
  25. {
  26. $this->workers = $gearmanCacheWrapper->getWorkerCollection();
  27. }
  28. /**
  29. * Return worker containing a job with $jobName as name
  30. * If is not found, throws JobDoesNotExistException Exception
  31. *
  32. * @param string $jobName Name of job
  33. *
  34. * @return Array
  35. */
  36. public function getJob($jobName)
  37. {
  38. foreach ($this->workers as $worker) {
  39. if (is_array($worker['jobs'])) {
  40. foreach ($worker['jobs'] as $job) {
  41. if ($jobName === $job['realCallableName']) {
  42. $worker['job'] = $job;
  43. return $worker;
  44. }
  45. }
  46. }
  47. }
  48. throw new JobDoesNotExistException($jobName);
  49. }
  50. /**
  51. * Return worker with $workerName as name and all its jobs
  52. * If is not found, throws WorkerDoesNotExistException Exception
  53. *
  54. * @param string $workerName Name of worker
  55. *
  56. * @return Array
  57. */
  58. public function getWorker($workerName)
  59. {
  60. foreach ($this->workers as $worker) {
  61. if ($workerName === $worker['callableName']) {
  62. return $worker;
  63. }
  64. }
  65. throw new WorkerDoesNotExistException($workerName);
  66. }
  67. }