JobClass.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 ReflectionMethod;
  14. use Symfony\Component\DependencyInjection\ContainerAware;
  15. use Mmoreram\GearmanBundle\Driver\Gearman\Job as JobAnnotation;
  16. /**
  17. * Job class
  18. *
  19. * This class provide all worker definition.
  20. *
  21. * @since 2.3.1
  22. */
  23. class JobClass extends ContainerAware
  24. {
  25. /**
  26. * @var string
  27. *
  28. * Default description when is not defined
  29. */
  30. const DEFAULT_DESCRIPTION = 'No description is defined';
  31. /**
  32. * @var string
  33. *
  34. * Callable name for this job
  35. * If is setted on annotations, this value will be used
  36. * otherwise, natural method name will be used.
  37. */
  38. private $callableName;
  39. /**
  40. * @var string
  41. *
  42. * Method name
  43. */
  44. private $methodName;
  45. /**
  46. * @var string
  47. *
  48. * RealCallable name for this job without the job prefix
  49. *
  50. */
  51. private $realCallableNameNoPrefix;
  52. /**
  53. * @var string
  54. *
  55. * RealCallable name for this job
  56. * natural method name will be used.
  57. */
  58. private $realCallableName;
  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 int
  79. *
  80. * Job minimum execution time
  81. */
  82. private $minimumExecutionTime;
  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 string
  97. *
  98. * The prefix to be prepended to all job callable names.
  99. */
  100. private $jobPrefix;
  101. /**
  102. * Construct method
  103. *
  104. * @param JobAnnotation $jobAnnotation JobAnnotation class
  105. * @param ReflectionMethod $reflectionMethod ReflextionMethod class
  106. * @param string $callableNameClass Callable name class
  107. * @param array $servers Array of servers defined for Worker
  108. * @param array $defaultSettings Default settings for Worker
  109. */
  110. public function __construct(
  111. JobAnnotation $jobAnnotation,
  112. ReflectionMethod $reflectionMethod,
  113. $callableNameClass,
  114. array $servers,
  115. array $defaultSettings
  116. )
  117. {
  118. $this->callableName = is_null($jobAnnotation->name)
  119. ? $reflectionMethod->getName()
  120. : $jobAnnotation->name;
  121. $this->methodName = $reflectionMethod->getName();
  122. $this->realCallableNameNoPrefix = str_replace('\\', '', $callableNameClass . '~' . $this->callableName);
  123. $this->jobPrefix = isset($defaultSettings['jobPrefix'])
  124. ? $defaultSettings['jobPrefix']
  125. : null;
  126. $this->realCallableName = $this->jobPrefix . $this->realCallableNameNoPrefix;
  127. $this->description = is_null($jobAnnotation->description)
  128. ? self::DEFAULT_DESCRIPTION
  129. : $jobAnnotation->description;
  130. $this->servers = $this->loadServers($jobAnnotation, $servers);
  131. $this->iterations = $this->loadIterations($jobAnnotation, $defaultSettings);
  132. $this->minimumExecutionTime = $this->loadMinimumExecutionTime($jobAnnotation, $defaultSettings);
  133. $this->timeout = $this->loadTimeout($jobAnnotation, $defaultSettings);
  134. $this->defaultMethod = $this->loadDefaultMethod($jobAnnotation, $defaultSettings);
  135. }
  136. /**
  137. * Load servers
  138. *
  139. * If any server is defined in JobAnnotation, this one is used.
  140. * Otherwise is used servers set in Class
  141. *
  142. * @param JobAnnotation $jobAnnotation JobAnnotation class
  143. * @param array $servers Array of servers defined for Worker
  144. *
  145. * @return array Servers
  146. */
  147. private function loadServers(JobAnnotation $jobAnnotation, array $servers)
  148. {
  149. /**
  150. * If is configured some servers definition in the worker, overwrites
  151. */
  152. if ($jobAnnotation->servers) {
  153. $servers = (is_array($jobAnnotation->servers) && !isset($jobAnnotation->servers['host']))
  154. ? $jobAnnotation->servers
  155. : array($jobAnnotation->servers);
  156. }
  157. return $servers;
  158. }
  159. /**
  160. * Load iterations
  161. *
  162. * If iterations is defined in JobAnnotation, this one is used.
  163. * Otherwise is used set in Class
  164. *
  165. * @param JobAnnotation $jobAnnotation JobAnnotation class
  166. * @param array $defaultSettings Default settings for Worker
  167. *
  168. * @return integer Iteration
  169. */
  170. private function loadIterations(JobAnnotation $jobAnnotation, array $defaultSettings)
  171. {
  172. return is_null($jobAnnotation->iterations)
  173. ? (int) $defaultSettings['iterations']
  174. : (int) $jobAnnotation->iterations;
  175. }
  176. /**
  177. * Load defaultMethod
  178. *
  179. * If defaultMethod is defined in JobAnnotation, this one is used.
  180. * Otherwise is used set in Class
  181. *
  182. * @param JobAnnotation $jobAnnotation JobAnnotation class
  183. * @param array $defaultSettings Default settings for Worker
  184. *
  185. * @return string Default method
  186. */
  187. private function loadDefaultMethod(JobAnnotation $jobAnnotation, array $defaultSettings)
  188. {
  189. return is_null($jobAnnotation->defaultMethod)
  190. ? $defaultSettings['method']
  191. : $jobAnnotation->defaultMethod;
  192. }
  193. /**
  194. * Load minimumExecutionTime
  195. *
  196. * If minimumExecutionTime is defined in JobAnnotation, this one is used.
  197. * Otherwise is used set in Class
  198. *
  199. * @param JobAnnotation $jobAnnotation
  200. * @param array $defaultSettings
  201. *
  202. * @return int
  203. */
  204. private function loadMinimumExecutionTime(JobAnnotation $jobAnnotation, array $defaultSettings)
  205. {
  206. return is_null($jobAnnotation->minimumExecutionTime)
  207. ? (int) $defaultSettings['minimumExecutionTime']
  208. : (int) $jobAnnotation->minimumExecutionTime;
  209. }
  210. /**
  211. * Load timeout
  212. *
  213. * If timeout is defined in JobAnnotation, this one is used.
  214. * Otherwise is used set in Class
  215. *
  216. * @param JobAnnotation $jobAnnotation
  217. * @param array $defaultSettings
  218. *
  219. * @return int
  220. */
  221. private function loadTimeout(JobAnnotation $jobAnnotation, array $defaultSettings)
  222. {
  223. return is_null($jobAnnotation->timeout)
  224. ? (int) $defaultSettings['timeout']
  225. : (int) $jobAnnotation->timeout;
  226. }
  227. /**
  228. * Retrieve all Job data in cache format
  229. *
  230. * @return array
  231. */
  232. public function toArray()
  233. {
  234. return array(
  235. 'callableName' => $this->callableName,
  236. 'methodName' => $this->methodName,
  237. 'realCallableName' => $this->realCallableName,
  238. 'jobPrefix' => $this->jobPrefix,
  239. 'realCallableNameNoPrefix' => $this->realCallableNameNoPrefix,
  240. 'description' => $this->description,
  241. 'iterations' => $this->iterations,
  242. 'minimumExecutionTime' => $this->minimumExecutionTime,
  243. 'timeout' => $this->timeout,
  244. 'servers' => $this->servers,
  245. 'defaultMethod' => $this->defaultMethod,
  246. );
  247. }
  248. }