WorkerClass.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 int
  85. *
  86. * Job minimum execution time
  87. */
  88. private $minimumExecutionTime;
  89. /**
  90. * @var int
  91. *
  92. * Timeout for idle job
  93. */
  94. private $timeout;
  95. /**
  96. * @var array
  97. *
  98. * Collection of servers to connect
  99. */
  100. private $servers;
  101. /**
  102. * @var JobCollection
  103. *
  104. * All jobs inside Worker
  105. */
  106. private $jobCollection;
  107. /**
  108. * The prefix for all job names
  109. *
  110. * @var string $jobPrefix
  111. */
  112. private $jobPrefix = null;
  113. /**
  114. * Retrieves all jobs available from worker
  115. *
  116. * @param WorkAnnotation $workAnnotation workAnnotation class
  117. * @param ReflectionClass $reflectionClass Reflexion class
  118. * @param Reader $reader Reader class
  119. * @param array $servers Array of servers defined for Worker
  120. * @param array $defaultSettings Default settings for Worker
  121. */
  122. public function __construct(WorkAnnotation $workAnnotation, ReflectionClass $reflectionClass, Reader $reader, array $servers, array $defaultSettings)
  123. {
  124. $this->namespace = $reflectionClass->getNamespaceName();
  125. /**
  126. * If WorkAnnotation name field is defined, workers_name_prepend_namespace value
  127. * in defaultSettings array must be checked.
  128. *
  129. * If true, namespace must be prepended to workAnnotation name for callableName
  130. * Otherwise, only workAnnotation value is set as callableName
  131. */
  132. $callableNameNamespace = $defaultSettings['workers_name_prepend_namespace']
  133. ? $this->namespace
  134. : '';
  135. /**
  136. * Setting worker callable name
  137. */
  138. $this->callableName = is_null($workAnnotation->name)
  139. ? $reflectionClass->getName()
  140. : $callableNameNamespace . $workAnnotation->name;
  141. $this->callableName = str_replace('\\', '', $this->callableName);
  142. /**
  143. * Setting worker description
  144. */
  145. $this->description = is_null($workAnnotation->description)
  146. ? self::DEFAULT_DESCRIPTION
  147. : $workAnnotation->description;
  148. $this->fileName = $reflectionClass->getFileName();
  149. $this->className = $reflectionClass->getName();
  150. $this->service = $workAnnotation->service;
  151. if (isset($defaultSettings['job_prefix'])) {
  152. $this->jobPrefix = $defaultSettings['job_prefix'];
  153. }
  154. $this->servers = $this->loadServers($workAnnotation, $servers);
  155. $this->iterations = $this->loadIterations($workAnnotation, $defaultSettings);
  156. $this->defaultMethod = $this->loadDefaultMethod($workAnnotation, $defaultSettings);
  157. $this->minimumExecutionTime = $this->loadMinimumExecutionTime($workAnnotation, $defaultSettings);
  158. $this->timeout = $this->loadTimeout($workAnnotation, $defaultSettings);
  159. $this->jobCollection = $this->createJobCollection($reflectionClass, $reader);
  160. }
  161. /**
  162. * Load servers
  163. *
  164. * If any server is defined in JobAnnotation, this one is used.
  165. * Otherwise is used servers set in Class
  166. *
  167. * @param WorkAnnotation $workAnnotation WorkAnnotation class
  168. * @param array $servers Array of servers defined for Worker
  169. *
  170. * @return array Servers
  171. */
  172. private function loadServers(WorkAnnotation $workAnnotation, array $servers)
  173. {
  174. /**
  175. * If is configured some servers definition in the worker, overwrites
  176. */
  177. if ($workAnnotation->servers) {
  178. $servers = (is_array($workAnnotation->servers) && !isset($workAnnotation->servers['host']))
  179. ? $workAnnotation->servers
  180. : array($workAnnotation->servers);
  181. }
  182. return $servers;
  183. }
  184. /**
  185. * Load iterations
  186. *
  187. * If iterations is defined in WorkAnnotation, this one is used.
  188. * Otherwise is used set in Class
  189. *
  190. * @param WorkAnnotation $workAnnotation WorkAnnotation class
  191. * @param array $defaultSettings Default settings for Worker
  192. *
  193. * @return integer Iteration
  194. */
  195. private function loadIterations(WorkAnnotation $workAnnotation, array $defaultSettings)
  196. {
  197. return is_null($workAnnotation->iterations)
  198. ? (int) $defaultSettings['iterations']
  199. : (int) $workAnnotation->iterations;
  200. }
  201. /**
  202. * Load defaultMethod
  203. *
  204. * If defaultMethod is defined in WorkAnnotation, this one is used.
  205. * Otherwise is used set in Class
  206. *
  207. * @param WorkAnnotation $workAnnotation WorkAnnotation class
  208. * @param array $defaultSettings Default settings for Worker
  209. *
  210. * @return string Default method
  211. */
  212. private function loadDefaultMethod(WorkAnnotation $workAnnotation, array $defaultSettings)
  213. {
  214. return is_null($workAnnotation->defaultMethod)
  215. ? $defaultSettings['method']
  216. : $workAnnotation->defaultMethod;
  217. }
  218. /**
  219. * Load minimumExecutionTime
  220. *
  221. * If minimumExecutionTime is defined in JobAnnotation, this one is used.
  222. * Otherwise is used set in Class
  223. *
  224. * @param WorkAnnotation $workAnnotation
  225. * @param array $defaultSettings
  226. *
  227. * @return int
  228. */
  229. private function loadMinimumExecutionTime(WorkAnnotation $workAnnotation, array $defaultSettings)
  230. {
  231. return is_null($workAnnotation->minimumExecutionTime)
  232. ? (int) $defaultSettings['minimum_execution_time']
  233. : (int) $workAnnotation->minimumExecutionTime;
  234. }
  235. /**
  236. * Load timeout
  237. *
  238. * If timeout is defined in JobAnnotation, this one is used.
  239. * Otherwise is used set in Class
  240. *
  241. * @param WorkAnnotation $workAnnotation
  242. * @param array $defaultSettings
  243. *
  244. * @return int
  245. */
  246. private function loadTimeout(WorkAnnotation $workAnnotation, array $defaultSettings)
  247. {
  248. return is_null($workAnnotation->timeout)
  249. ? (int) $defaultSettings['timeout']
  250. : (int) $workAnnotation->timeout;
  251. }
  252. /**
  253. * Creates job collection of worker
  254. *
  255. * @param ReflectionClass $reflectionClass Reflexion class
  256. * @param Reader $reader ReaderAnnotation class
  257. *
  258. * @return WorkerClass self Object
  259. */
  260. private function createJobCollection(ReflectionClass $reflectionClass, Reader $reader)
  261. {
  262. $jobCollection = new JobCollection;
  263. /**
  264. * For each defined method, we parse it
  265. */
  266. foreach ($reflectionClass->getMethods() as $reflectionMethod) {
  267. $methodAnnotations = $reader->getMethodAnnotations($reflectionMethod);
  268. /**
  269. * Every annotation found is parsed
  270. */
  271. foreach ($methodAnnotations as $methodAnnotation) {
  272. /**
  273. * Annotation is only loaded if is typeof JobAnnotation
  274. */
  275. if ($methodAnnotation instanceof JobAnnotation) {
  276. /**
  277. * Creates new Job
  278. */
  279. $job = new Job($methodAnnotation, $reflectionMethod, $this->callableName, $this->servers, array(
  280. 'jobPrefix' => $this->jobPrefix,
  281. 'iterations' => $this->iterations,
  282. 'method' => $this->defaultMethod,
  283. 'minimumExecutionTime' => $this->minimumExecutionTime,
  284. 'timeout' => $this->timeout,
  285. ));
  286. $jobCollection->add($job);
  287. }
  288. }
  289. }
  290. return $jobCollection;
  291. }
  292. /**
  293. * Retrieve all Worker data in cache format
  294. *
  295. * @return array
  296. */
  297. public function toArray()
  298. {
  299. return array(
  300. 'namespace' => $this->namespace,
  301. 'className' => $this->className,
  302. 'fileName' => $this->fileName,
  303. 'callableName' => $this->callableName,
  304. 'description' => $this->description,
  305. 'service' => $this->service,
  306. 'servers' => $this->servers,
  307. 'iterations' => $this->iterations,
  308. 'minimumExecutionTime' => $this->minimumExecutionTime,
  309. 'timeout' => $this->timeout,
  310. 'jobs' => $this->jobCollection->toArray(),
  311. );
  312. }
  313. }