WorkerClass.php 8.0 KB

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