AbstractGearmanService.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Gearman Bundle for Symfony2
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. *
  8. * Feel free to edit as you please, and have fun.
  9. *
  10. * @author Marc Morera <yuhu@mmoreram.com>
  11. */
  12. namespace Mmoreram\GearmanBundle\Service\Abstracts;
  13. use Mmoreram\GearmanBundle\Exceptions\JobDoesNotExistException;
  14. use Mmoreram\GearmanBundle\Exceptions\WorkerDoesNotExistException;
  15. use Mmoreram\GearmanBundle\Service\GearmanCacheWrapper;
  16. /**
  17. * Gearman execute methods. All Worker methods
  18. *
  19. * @author Marc Morera <yuhu@mmoreram.com>
  20. */
  21. abstract class AbstractGearmanService
  22. {
  23. /**
  24. * All workers
  25. *
  26. * @var array
  27. */
  28. protected $workers;
  29. /**
  30. * The prefix for all job names
  31. *
  32. * @var string $jobPrefix
  33. */
  34. protected $jobPrefix = null;
  35. /**
  36. * Construct method
  37. *
  38. * @param GearmanCacheWrapper $gearmanCacheWrapper GearmanCacheWrapper
  39. * @param array $defaultSettings The default settings for the bundle
  40. */
  41. public function __construct(GearmanCacheWrapper $gearmanCacheWrapper, array $defaultSettings)
  42. {
  43. $this->workers = $gearmanCacheWrapper->getWorkers();
  44. if (isset($defaultSettings['job_prefix'])) {
  45. $this->jobPrefix = $defaultSettings['job_prefix'];
  46. }
  47. }
  48. /**
  49. * Return worker containing a job with $jobName as name
  50. * If is not found, throws JobDoesNotExistException Exception
  51. *
  52. * @param string $jobName Name of job
  53. *
  54. * @return Array
  55. *
  56. * @throws JobDoesNotExistException
  57. */
  58. public function getJob($jobName)
  59. {
  60. $jobName = $this->jobPrefix . $jobName;
  61. foreach ($this->workers as $worker) {
  62. if (is_array($worker['jobs'])) {
  63. foreach ($worker['jobs'] as $job) {
  64. if ($jobName === $job['realCallableName']) {
  65. $worker['job'] = $job;
  66. return $worker;
  67. }
  68. }
  69. }
  70. }
  71. throw new JobDoesNotExistException();
  72. }
  73. /**
  74. * Return worker with $workerName as name and all its jobs
  75. * If is not found, throws WorkerDoesNotExistException Exception
  76. *
  77. * @param string $workerName Name of worker
  78. *
  79. * @return Array
  80. *
  81. * @throws WorkerDoesNotExistException
  82. */
  83. public function getWorker($workerName)
  84. {
  85. foreach ($this->workers as $worker) {
  86. if ($workerName === $worker['callableName']) {
  87. return $worker;
  88. }
  89. }
  90. throw new WorkerDoesNotExistException();
  91. }
  92. /**
  93. * Return array of workers
  94. *
  95. * @return array all available workers
  96. */
  97. public function getWorkers()
  98. {
  99. return $this->workers;
  100. }
  101. }