JobClass.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. * Timeout for idle worker
  81. */
  82. private $timeout;
  83. /**
  84. * @var array
  85. *
  86. * Collection of servers to connect
  87. */
  88. private $servers;
  89. /**
  90. * @var string
  91. *
  92. * The prefix to be prepended to all job callable names.
  93. */
  94. private $jobPrefix;
  95. /**
  96. * Construct method
  97. *
  98. * @param JobAnnotation $jobAnnotation JobAnnotation class
  99. * @param ReflectionMethod $reflectionMethod ReflextionMethod class
  100. * @param string $callableNameClass Callable name class
  101. * @param array $servers Array of servers defined for Worker
  102. * @param array $defaultSettings Default settings for Worker
  103. */
  104. public function __construct(
  105. JobAnnotation $jobAnnotation,
  106. ReflectionMethod $reflectionMethod,
  107. $callableNameClass,
  108. array $servers,
  109. array $defaultSettings
  110. )
  111. {
  112. $this->callableName = is_null($jobAnnotation->name)
  113. ? $reflectionMethod->getName()
  114. : $jobAnnotation->name;
  115. $this->methodName = $reflectionMethod->getName();
  116. $this->realCallableNameNoPrefix = str_replace('\\', '', $callableNameClass . '~' . $this->callableName);
  117. $this->jobPrefix = isset($defaultSettings['jobPrefix'])
  118. ? $defaultSettings['jobPrefix']
  119. : null;
  120. $this->realCallableName = $this->jobPrefix . $this->realCallableNameNoPrefix;
  121. $this->description = is_null($jobAnnotation->description)
  122. ? self::DEFAULT_DESCRIPTION
  123. : $jobAnnotation->description;
  124. $this->servers = $this->loadServers($jobAnnotation, $servers);
  125. $this->iterations = $this->loadIterations($jobAnnotation, $defaultSettings);
  126. $this->minimumExecutionTime = $this->loadMinimumExecutionTime($jobAnnotation, $defaultSettings);
  127. $this->timeout = $this->loadTimeout($jobAnnotation, $defaultSettings);
  128. $this->defaultMethod = $this->loadDefaultMethod($jobAnnotation, $defaultSettings);
  129. }
  130. /**
  131. * Load servers
  132. *
  133. * If any server is defined in JobAnnotation, this one is used.
  134. * Otherwise is used servers set in Class
  135. *
  136. * @param JobAnnotation $jobAnnotation JobAnnotation class
  137. * @param array $servers Array of servers defined for Worker
  138. *
  139. * @return array Servers
  140. */
  141. private function loadServers(JobAnnotation $jobAnnotation, array $servers)
  142. {
  143. /**
  144. * If is configured some servers definition in the worker, overwrites
  145. */
  146. if ($jobAnnotation->servers) {
  147. $servers = (is_array($jobAnnotation->servers) && !isset($jobAnnotation->servers['host']))
  148. ? $jobAnnotation->servers
  149. : array($jobAnnotation->servers);
  150. }
  151. return $servers;
  152. }
  153. /**
  154. * Load iterations
  155. *
  156. * If iterations is defined in JobAnnotation, this one is used.
  157. * Otherwise is used set in Class
  158. *
  159. * @param JobAnnotation $jobAnnotation JobAnnotation class
  160. * @param array $defaultSettings Default settings for Worker
  161. *
  162. * @return integer Iteration
  163. */
  164. private function loadIterations(JobAnnotation $jobAnnotation, array $defaultSettings)
  165. {
  166. return is_null($jobAnnotation->iterations)
  167. ? (int) $defaultSettings['iterations']
  168. : (int) $jobAnnotation->iterations;
  169. }
  170. /**
  171. * Load defaultMethod
  172. *
  173. * If defaultMethod is defined in JobAnnotation, this one is used.
  174. * Otherwise is used set in Class
  175. *
  176. * @param JobAnnotation $jobAnnotation JobAnnotation class
  177. * @param array $defaultSettings Default settings for Worker
  178. *
  179. * @return string Default method
  180. */
  181. private function loadDefaultMethod(JobAnnotation $jobAnnotation, array $defaultSettings)
  182. {
  183. return is_null($jobAnnotation->defaultMethod)
  184. ? $defaultSettings['method']
  185. : $jobAnnotation->defaultMethod;
  186. }
  187. /**
  188. * Load minimumExecutionTime
  189. *
  190. * If minimumExecutionTime is defined in JobAnnotation, this one is used.
  191. * Otherwise is used set in Class
  192. *
  193. * @param JobAnnotation $jobAnnotation
  194. * @param array $defaultSettings
  195. *
  196. * @return int
  197. */
  198. private function loadMinimumExecutionTime(JobAnnotation $jobAnnotation, array $defaultSettings)
  199. {
  200. return is_null($jobAnnotation->minimumExecutionTime)
  201. ? (int) $defaultSettings['minimumExecutionTime']
  202. : (int) $jobAnnotation->minimumExecutionTime;
  203. }
  204. /**
  205. * Load timeout
  206. *
  207. * If timeout is defined in JobAnnotation, this one is used.
  208. * Otherwise is used set in Class
  209. *
  210. * @param JobAnnotation $jobAnnotation
  211. * @param array $defaultSettings
  212. *
  213. * @return int
  214. */
  215. private function loadTimeout(JobAnnotation $jobAnnotation, array $defaultSettings)
  216. {
  217. return is_null($jobAnnotation->timeout)
  218. ? (int) $defaultSettings['timeout']
  219. : (int) $jobAnnotation->timeout;
  220. }
  221. /**
  222. * Retrieve all Job data in cache format
  223. *
  224. * @return array
  225. */
  226. public function toArray()
  227. {
  228. return array(
  229. 'callableName' => $this->callableName,
  230. 'methodName' => $this->methodName,
  231. 'realCallableName' => $this->realCallableName,
  232. 'jobPrefix' => $this->jobPrefix,
  233. 'realCallableNameNoPrefix' => $this->realCallableNameNoPrefix,
  234. 'description' => $this->description,
  235. 'iterations' => $this->iterations,
  236. 'minimumExecutionTime' => $this->minimumExecutionTime,
  237. 'timeout' => $this->timeout,
  238. 'servers' => $this->servers,
  239. 'defaultMethod' => $this->defaultMethod,
  240. );
  241. }
  242. }