WorkerClass.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. /**
  3. * Gearman Bundle for Symfony2
  4. *
  5. * @author Marc Morera <yuhu@mmoreram.com>
  6. * @since 2013
  7. */
  8. namespace Mmoreram\GearmanBundle\Module;
  9. use Doctrine\Common\Annotations\SimpleAnnotationReader;
  10. use Mmoreram\GearmanBundle\Module\JobCollection;
  11. use Mmoreram\GearmanBundle\Module\JobClass as Job;
  12. use Mmoreram\GearmanBundle\Driver\Gearman\Job as JobAnnotation;
  13. use Mmoreram\GearmanBundle\Driver\Gearman\Work as WorkAnnotation;
  14. use ReflectionClass;
  15. /**
  16. * Worker class
  17. *
  18. * This class provide all worker definition.
  19. */
  20. class WorkerClass
  21. {
  22. /**
  23. * @var string
  24. *
  25. * Default description when is not defined
  26. */
  27. const DEFAULT_DESCRIPTION = 'No description is defined';
  28. /**
  29. * @var string
  30. *
  31. * Namespace of worker class
  32. */
  33. private $namespace;
  34. /**
  35. * @var string
  36. *
  37. * Class name of worker
  38. */
  39. private $className;
  40. /**
  41. * @var string
  42. *
  43. * Filename of worker
  44. */
  45. private $fileName;
  46. /**
  47. * @var string
  48. *
  49. * Callable name for this job.
  50. * If is setted on annotations, this value will be used.
  51. * Otherwise, natural method name will be used.
  52. */
  53. private $callableName;
  54. /**
  55. * @var string
  56. *
  57. * Service alias if this worker is wanted to be built by dependency injection
  58. */
  59. private $service;
  60. /**
  61. * @var string
  62. *
  63. * Description of Job
  64. */
  65. private $description;
  66. /**
  67. * @var integer
  68. *
  69. * Number of iterations this job will be alive before die
  70. */
  71. private $iterations;
  72. /**
  73. * @var string
  74. *
  75. * Default method this job will be call into Gearman client
  76. */
  77. private $defaultMethod;
  78. /**
  79. * @var array
  80. *
  81. * Collection of servers to connect
  82. */
  83. private $servers;
  84. /**
  85. * @var JobCollection
  86. *
  87. * All jobs inside Worker
  88. */
  89. private $jobCollection;
  90. /**
  91. * Retrieves all jobs available from worker
  92. *
  93. * @param WorkAnnotation $workAnnotation workAnnotation class
  94. * @param ReflectionClass $reflectionClass Reflexion class
  95. * @param SimpleAnnotationReader $reader ReaderAnnotation class
  96. * @param array $servers Array of servers defined for Worker
  97. * @param array $defaultSettings Default settings for Worker
  98. */
  99. public function __construct(WorkAnnotation $workAnnotation, ReflectionClass $reflectionClass, SimpleAnnotationReader $reader, array $servers, array $defaultSettings)
  100. {
  101. $this->namespace = $reflectionClass->getNamespaceName();
  102. /**
  103. * Setting worker callable name
  104. */
  105. $this->callableName = is_null($workAnnotation->name)
  106. ? $reflectionClass->getName()
  107. : $this->namespace . $workAnnotation->name;
  108. $this->callableName = str_replace('\\', '', $this->callableName);
  109. /**
  110. * Setting worker description
  111. */
  112. $this->description = is_null($workAnnotation->description)
  113. ? self::DEFAULT_DESCRIPTION
  114. : $workAnnotation->description;
  115. $this->fileName = $reflectionClass->getFileName();
  116. $this->className = $reflectionClass->getName();
  117. $this->service = $workAnnotation->service;
  118. $this->servers = $this->loadServers($workAnnotation, $servers);
  119. $this->iterations = $this->loadIterations($workAnnotation, $defaultSettings);
  120. $this->defaultMethod = $this->loadDefaultMethod($workAnnotation, $defaultSettings);
  121. $this->jobCollection = $this->createJobCollection($reflectionClass, $reader);
  122. }
  123. /**
  124. * Load servers
  125. *
  126. * If any server is defined in JobAnnotation, this one is used.
  127. * Otherwise is used servers set in Class
  128. *
  129. * @param WorkAnnotation $workAnnotation WorkAnnotation class
  130. * @param array $servers Array of servers defined for Worker
  131. *
  132. * @return array Servers
  133. */
  134. private function loadServers(WorkAnnotation $workAnnotation, array $servers)
  135. {
  136. /**
  137. * If is configured some servers definition in the worker, overwrites
  138. */
  139. if ($workAnnotation->servers) {
  140. $servers = ( is_array($workAnnotation->servers) && !isset($workAnnotation->servers['host']) )
  141. ? $workAnnotation->servers
  142. : array($workAnnotation->servers);
  143. }
  144. return $servers;
  145. }
  146. /**
  147. * Load iterations
  148. *
  149. * If iterations is defined in WorkAnnotation, this one is used.
  150. * Otherwise is used set in Class
  151. *
  152. * @param WorkAnnotation $workAnnotation WorkAnnotation class
  153. * @param array $defaultSettings Default settings for Worker
  154. *
  155. * @return integer Iteration
  156. */
  157. private function loadIterations(WorkAnnotation $workAnnotation, array $defaultSettings)
  158. {
  159. return is_null($workAnnotation->iterations)
  160. ? (int) $defaultSettings['iterations']
  161. : (int) $workAnnotation->iterations;
  162. }
  163. /**
  164. * Load defaultMethod
  165. *
  166. * If defaultMethod is defined in WorkAnnotation, this one is used.
  167. * Otherwise is used set in Class
  168. *
  169. * @param WorkAnnotation $workAnnotation WorkAnnotation class
  170. * @param array $defaultSettings Default settings for Worker
  171. *
  172. * @return string Default method
  173. */
  174. private function loadDefaultMethod(WorkAnnotation $workAnnotation, array $defaultSettings)
  175. {
  176. return is_null($workAnnotation->defaultMethod)
  177. ? $defaultSettings['method']
  178. : $workAnnotation->defaultMethod;
  179. }
  180. /**
  181. * Creates job collection of worker
  182. *
  183. * @param ReflectionClass $reflectionClass Reflexion class
  184. * @param SimpleAnnotationReader $reader ReaderAnnotation class
  185. *
  186. * @return WorkerClass self Object
  187. */
  188. private function createJobCollection(ReflectionClass $reflectionClass, SimpleAnnotationReader $reader)
  189. {
  190. $jobCollection = new JobCollection;
  191. /**
  192. * For each defined method, we parse it
  193. */
  194. foreach ($reflectionClass->getMethods() as $reflectionMethod) {
  195. $methodAnnotations = $reader->getMethodAnnotations($reflectionMethod);
  196. /**
  197. * Every annotation found is parsed
  198. */
  199. foreach ($methodAnnotations as $methodAnnotation) {
  200. /**
  201. * Annotation is only laoded if is typeof JobAnnotation
  202. */
  203. if ($methodAnnotation instanceof JobAnnotation) {
  204. /**
  205. * Creates new Job
  206. */
  207. $job = new Job($methodAnnotation, $reflectionMethod, $this->callableName, $this->servers, array(
  208. 'iterations' => $this->iterations,
  209. 'method' => $this->defaultMethod,
  210. ));
  211. $jobCollection->add($job);
  212. }
  213. }
  214. }
  215. return $jobCollection;
  216. }
  217. /**
  218. * Retrieve all Worker data in cache format
  219. *
  220. * @return array
  221. */
  222. public function toArray()
  223. {
  224. return array(
  225. 'namespace' => $this->namespace,
  226. 'className' => $this->className,
  227. 'fileName' => $this->fileName,
  228. 'callableName' => $this->callableName,
  229. 'description' => $this->description,
  230. 'service' => $this->service,
  231. 'servers' => $this->servers,
  232. 'iterations' => $this->iterations,
  233. 'jobs' => $this->jobCollection->toArray(),
  234. );
  235. }
  236. }