WorkerClass.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace Mmoreramerino\GearmanBundle\Module;
  3. use Doctrine\Common\Annotations\Reader;
  4. use Mmoreramerino\GearmanBundle\Driver\Gearman\Work;
  5. use Mmoreramerino\GearmanBundle\Module\JobCollection;
  6. use Mmoreramerino\GearmanBundle\Module\JobClass as Job;
  7. use Mmoreramerino\GearmanBundle\Exceptions\SettingValueMissingException;
  8. use Mmoreramerino\GearmanBundle\Exceptions\SettingValueBadFormatException;
  9. /**
  10. * Worker class
  11. *
  12. * @author Marc Morera <marc@ulabox.com>
  13. */
  14. class WorkerClass
  15. {
  16. /**
  17. * All jobs inside Worker
  18. *
  19. * @var JobCollection
  20. */
  21. private $jobCollection;
  22. /**
  23. * Callable name for this job.
  24. * If is setted on annotations, this value will be used.
  25. * Otherwise, natural method name will be used.
  26. *
  27. * @var string
  28. */
  29. private $callableName;
  30. /**
  31. * Namespace of Work class
  32. *
  33. * @var string
  34. */
  35. private $namespace;
  36. /**
  37. * Retrieves all jobs available from worker
  38. *
  39. * @param Work $classAnnotation ClassAnnotation class
  40. * @param \ReflectionClass $reflectionClass Reflexion class
  41. * @param Reader $reader ReaderAnnotation class
  42. * @param array $settings Settings array
  43. */
  44. public function __construct(Work $classAnnotation, \ReflectionClass $reflectionClass, Reader $reader, array $settings)
  45. {
  46. $this->namespace = $reflectionClass->getNamespaceName();
  47. $this->callableName = str_replace('\\', '', ((null !== $classAnnotation->name) ?
  48. ($this->namespace .'\\' .$classAnnotation->name) :
  49. $reflectionClass->getName()));
  50. $this->description = (null !== $classAnnotation->description) ?
  51. $classAnnotation->description :
  52. 'No description is defined';
  53. $this->fileName = $reflectionClass->getFileName();
  54. $this->className = $reflectionClass->getName();
  55. $this->service = $classAnnotation->service;
  56. if (!isset($settings['defaults'])) {
  57. throw new SettingValueMissingException('defaults');
  58. }
  59. if (isset($settings['defaults']['iterations']) && null !== $settings['defaults']['iterations']) {
  60. $iter = (int) ($settings['defaults']['iterations']);
  61. if (null !== $classAnnotation->iterations) {
  62. $iter = (int) ($classAnnotation->iterations);
  63. }
  64. } else {
  65. throw new SettingValueMissingException('defaults/iterations');
  66. }
  67. $this->iterations = $iter;
  68. /**
  69. * Servers definition for worker
  70. */
  71. $servers = array();
  72. if (isset($settings['defaults']['servers']) && null !== $settings['defaults']['servers']) {
  73. if (is_array($settings['defaults']['servers'])) {
  74. foreach ($settings['defaults']['servers'] as $name => $server) {
  75. $servername = $server['hostname'].':'.(int) ($server['port']);
  76. $servers[$name] = $servername;
  77. }
  78. } else {
  79. throw new SettingValueBadFormatException('servers');
  80. }
  81. if (null !== $classAnnotation->servers) {
  82. if (is_array($classAnnotation->servers)) {
  83. $servers = $classAnnotation->servers;
  84. } else {
  85. $servers = array($classAnnotation->servers);
  86. }
  87. }
  88. } else {
  89. throw new SettingValueMissingException('defaults/servers');
  90. }
  91. $this->servers = $servers;
  92. $this->jobCollection = new JobCollection;
  93. foreach ($reflectionClass->getMethods() as $method) {
  94. $reflMethod = new \ReflectionMethod($method->class, $method->name);
  95. $methodAnnotations = $reader->getMethodAnnotations($reflMethod);
  96. foreach ($methodAnnotations as $annot) {
  97. if ($annot instanceof \Mmoreramerino\GearmanBundle\Driver\Gearman\Job) {
  98. $this->jobCollection->add(new Job($annot, $reflMethod, $classAnnotation, $this->callableName, $settings));
  99. }
  100. }
  101. }
  102. }
  103. /**
  104. * Retrieve all Worker data in cache format
  105. *
  106. * @return array
  107. */
  108. public function __toCache()
  109. {
  110. $dump = array(
  111. 'namespace' => $this->namespace,
  112. 'className' => $this->className,
  113. 'fileName' => $this->fileName,
  114. 'callableName' => $this->callableName,
  115. 'description' => $this->description,
  116. 'service' => $this->service,
  117. 'servers' => $this->servers,
  118. 'iterations' => $this->iterations,
  119. 'jobs' => array(),
  120. );
  121. $dump['jobs'] = $this->jobCollection->__toCache();
  122. return $dump;
  123. }
  124. }