WorkerClass.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 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. * Namespace of worker class
  27. */
  28. private $namespace;
  29. /**
  30. * @var string
  31. *
  32. * Class name of worker
  33. */
  34. private $className;
  35. /**
  36. * @var string
  37. *
  38. * Filename of worker
  39. */
  40. private $fileName;
  41. /**
  42. * @var string
  43. *
  44. * Callable name for this job.
  45. * If is setted on annotations, this value will be used.
  46. * Otherwise, natural method name will be used.
  47. */
  48. private $callableName;
  49. /**
  50. * @var string
  51. *
  52. * Service alias if this worker is wanted to be built by dependency injection
  53. */
  54. private $service;
  55. /**
  56. * @var string
  57. *
  58. * Description of Job
  59. */
  60. private $description;
  61. /**
  62. * @var integer
  63. *
  64. * Number of iterations this job will be alive before die
  65. */
  66. private $iterations;
  67. /**
  68. * @var string
  69. *
  70. * Default method this job will be call into Gearman client
  71. */
  72. private $defaultMethod;
  73. /**
  74. * @var array
  75. *
  76. * Collection of servers to connect
  77. */
  78. private $servers;
  79. /**
  80. * @var array
  81. *
  82. * settings
  83. */
  84. private $settings;
  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 Reader $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, Reader $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. ? 'No description is defined'
  115. : $workAnnotation->description;
  116. $this->fileName = $reflectionClass->getFileName();
  117. $this->className = $reflectionClass->getName();
  118. $this->service = $workAnnotation->service;
  119. $this
  120. ->loadSettings($workAnnotation, $defaultSettings)
  121. ->loadServers($workAnnotation, $servers)
  122. ->createJobCollection($reflectionClass, $reader);
  123. }
  124. /**
  125. * Load settings
  126. *
  127. * @param WorkAnnotation $workAnnotation WorkAnnotation class
  128. * @param array $servers Array of servers defined for Worker
  129. *
  130. * @return WorkerClass self Object
  131. */
  132. private function loadServers(WorkAnnotation $workAnnotation, array $servers)
  133. {
  134. /**
  135. * By default, this worker takes default servers definition
  136. */
  137. $this->servers = $servers;
  138. /**
  139. * If is configured some servers definition in the worker, overwrites
  140. */
  141. if ($workAnnotation->servers) {
  142. $this->servers = ( is_array($workAnnotation->servers) && !isset($workAnnotation->servers['host']) )
  143. ? $workAnnotation->servers
  144. : array($workAnnotation->servers);
  145. }
  146. return $this;
  147. }
  148. /**
  149. * Load settings
  150. *
  151. * @param WorkAnnotation $workAnnotation WorkAnnotation class
  152. * @param array $defaultSettings Default settings for Worker
  153. *
  154. * @return WorkerClass self Object
  155. */
  156. private function loadSettings(WorkAnnotation $workAnnotation, array $defaultSettings)
  157. {
  158. $this->iterations = is_null($workAnnotation->iterations)
  159. ? (int) $defaultSettings['iterations']
  160. : $workAnnotation->iterations;
  161. $defaultSettings['iterations'] = $this->iterations;
  162. $this->defaultMethod = is_null($workAnnotation->defaultMethod)
  163. ? $defaultSettings['method']
  164. : $workAnnotation->defaultMethod;
  165. $defaultSettings['method'] = $this->defaultMethod;
  166. $this->settings = $defaultSettings;
  167. return $this;
  168. }
  169. /**
  170. * Creates job collection of worker
  171. *
  172. * @param ReflectionClass $reflectionClass Reflexion class
  173. * @param Reader $reader ReaderAnnotation class
  174. *
  175. * @return WorkerClass self Object
  176. */
  177. private function createJobCollection(ReflectionClass $reflectionClass, Reader $reader)
  178. {
  179. $this->jobCollection = new JobCollection;
  180. /**
  181. * For each defined method, we parse it
  182. */
  183. foreach ($reflectionClass->getMethods() as $method) {
  184. $reflectionMethod = new ReflectionMethod($method->class, $method->name);
  185. $methodAnnotations = $reader->getMethodAnnotations($reflectionMethod);
  186. /**
  187. * Every annotation found is parsed
  188. */
  189. foreach ($methodAnnotations as $methodAnnotation) {
  190. /**
  191. * Annotation is only laoded if is typeof JobAnnotation
  192. */
  193. if ($methodAnnotation instanceof JobAnnotation) {
  194. /**
  195. * Creates new Job
  196. */
  197. $job = new Job($methodAnnotation, $reflectionMethod, $this->callableName, $this->servers, $this->settings);
  198. $this->jobCollection->add($job);
  199. }
  200. }
  201. }
  202. return $this;
  203. }
  204. /**
  205. * Retrieve all Worker data in cache format
  206. *
  207. * @return array
  208. */
  209. public function toArray()
  210. {
  211. return array(
  212. 'namespace' => $this->namespace,
  213. 'className' => $this->className,
  214. 'fileName' => $this->fileName,
  215. 'callableName' => $this->callableName,
  216. 'description' => $this->description,
  217. 'service' => $this->service,
  218. 'servers' => $this->servers,
  219. 'iterations' => $this->iterations,
  220. 'jobs' => $this->jobCollection->toArray(),
  221. );
  222. }
  223. }