WorkerClass.php 10 KB

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