WorkerClass.php 7.8 KB

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