WorkerClass.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 array
  87. *
  88. * settings
  89. */
  90. private $settings;
  91. /**
  92. * @var JobCollection
  93. *
  94. * All jobs inside Worker
  95. */
  96. private $jobCollection;
  97. /**
  98. * Retrieves all jobs available from worker
  99. *
  100. * @param WorkAnnotation $workAnnotation workAnnotation class
  101. * @param ReflectionClass $reflectionClass Reflexion class
  102. * @param SimpleAnnotationReader $reader ReaderAnnotation class
  103. * @param array $servers Array of servers defined for Worker
  104. * @param array $defaultSettings Default settings for Worker
  105. */
  106. public function __construct(WorkAnnotation $workAnnotation, ReflectionClass $reflectionClass, SimpleAnnotationReader $reader, array $servers, array $defaultSettings)
  107. {
  108. $this->namespace = $reflectionClass->getNamespaceName();
  109. /**
  110. * Setting worker callable name
  111. */
  112. $this->callableName = is_null($workAnnotation->name)
  113. ? $reflectionClass->getName()
  114. : $this->namespace . $workAnnotation->name;
  115. $this->callableName = str_replace('\\', '', $this->callableName);
  116. /**
  117. * Setting worker description
  118. */
  119. $this->description = is_null($workAnnotation->description)
  120. ? self::DEFAULT_DESCRIPTION
  121. : $workAnnotation->description;
  122. $this->fileName = $reflectionClass->getFileName();
  123. $this->className = $reflectionClass->getName();
  124. $this->service = $workAnnotation->service;
  125. $this->servers = $this->loadServers($workAnnotation, $servers);
  126. $this->iterations = $this->loadIterations($workAnnotation, $defaultSettings);
  127. $this->defaultMethod = $this->loadDefaultMethod($workAnnotation, $defaultSettings);
  128. $this->jobCollection = $this->createJobCollection($reflectionClass, $reader);
  129. }
  130. /**
  131. * Load servers
  132. *
  133. * If any server is defined in JobAnnotation, this one is used.
  134. * Otherwise is used servers set in Class
  135. *
  136. * @param WorkAnnotation $workAnnotation WorkAnnotation class
  137. * @param array $servers Array of servers defined for Worker
  138. *
  139. * @return array Servers
  140. */
  141. private function loadServers(WorkAnnotation $workAnnotation, array $servers)
  142. {
  143. /**
  144. * If is configured some servers definition in the worker, overwrites
  145. */
  146. if ($workAnnotation->servers) {
  147. $servers = ( is_array($workAnnotation->servers) && !isset($workAnnotation->servers['host']) )
  148. ? $workAnnotation->servers
  149. : array($workAnnotation->servers);
  150. }
  151. return $servers;
  152. }
  153. /**
  154. * Load iterations
  155. *
  156. * If iterations is defined in WorkAnnotation, this one is used.
  157. * Otherwise is used set in Class
  158. *
  159. * @param WorkAnnotation $workAnnotation WorkAnnotation class
  160. * @param array $defaultSettings Default settings for Worker
  161. *
  162. * @return integer Iteration
  163. */
  164. private function loadIterations(WorkAnnotation $workAnnotation, array $defaultSettings)
  165. {
  166. return is_null($workAnnotation->iterations)
  167. ? (int) $defaultSettings['iterations']
  168. : (int) $workAnnotation->iterations;
  169. }
  170. /**
  171. * Load defaultMethod
  172. *
  173. * If defaultMethod is defined in WorkAnnotation, this one is used.
  174. * Otherwise is used set in Class
  175. *
  176. * @param WorkAnnotation $workAnnotation WorkAnnotation class
  177. * @param array $defaultSettings Default settings for Worker
  178. *
  179. * @return string Default method
  180. */
  181. private function loadDefaultMethod(WorkAnnotation $workAnnotation, array $defaultSettings)
  182. {
  183. return is_null($workAnnotation->defaultMethod)
  184. ? $defaultSettings['method']
  185. : $workAnnotation->defaultMethod;
  186. }
  187. /**
  188. * Creates job collection of worker
  189. *
  190. * @param ReflectionClass $reflectionClass Reflexion class
  191. * @param SimpleAnnotationReader $reader ReaderAnnotation class
  192. *
  193. * @return WorkerClass self Object
  194. */
  195. private function createJobCollection(ReflectionClass $reflectionClass, SimpleAnnotationReader $reader)
  196. {
  197. $jobCollection = new JobCollection;
  198. /**
  199. * For each defined method, we parse it
  200. */
  201. foreach ($reflectionClass->getMethods() as $reflectionMethod) {
  202. $methodAnnotations = $reader->getMethodAnnotations($reflectionMethod);
  203. /**
  204. * Every annotation found is parsed
  205. */
  206. foreach ($methodAnnotations as $methodAnnotation) {
  207. /**
  208. * Annotation is only laoded if is typeof JobAnnotation
  209. */
  210. if ($methodAnnotation instanceof JobAnnotation) {
  211. /**
  212. * Creates new Job
  213. */
  214. $job = new Job($methodAnnotation, $reflectionMethod, $this->callableName, $this->servers, array(
  215. 'iterations' => $this->iterations,
  216. 'method' => $this->defaultMethod,
  217. ));
  218. $jobCollection->add($job);
  219. }
  220. }
  221. }
  222. return $jobCollection;
  223. }
  224. /**
  225. * Retrieve all Worker data in cache format
  226. *
  227. * @return array
  228. */
  229. public function toArray()
  230. {
  231. return array(
  232. 'namespace' => $this->namespace,
  233. 'className' => $this->className,
  234. 'fileName' => $this->fileName,
  235. 'callableName' => $this->callableName,
  236. 'description' => $this->description,
  237. 'service' => $this->service,
  238. 'servers' => $this->servers,
  239. 'iterations' => $this->iterations,
  240. 'jobs' => $this->jobCollection->toArray(),
  241. );
  242. }
  243. }