WorkerClass.php 8.4 KB

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