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