JobClass.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 Symfony\Component\DependencyInjection\ContainerAware;
  10. use ReflectionMethod;
  11. use Mmoreram\GearmanBundle\Driver\Gearman\Job as JobAnnotation;
  12. /**
  13. * Job class
  14. *
  15. * This class provide all worker definition.
  16. */
  17. class JobClass extends ContainerAware
  18. {
  19. /**
  20. * @var string
  21. *
  22. * Default description when is not defined
  23. */
  24. const DEFAULT_DESCRIPTION = 'No description is defined';
  25. /**
  26. * @var string
  27. *
  28. * Callable name for this job
  29. * If is setted on annotations, this value will be used
  30. * otherwise, natural method name will be used.
  31. */
  32. private $callableName;
  33. /**
  34. * @var string
  35. *
  36. * Method name
  37. */
  38. private $methodName;
  39. /**
  40. * @var string
  41. *
  42. * RealCallable name for this job without the job prefix
  43. *
  44. */
  45. private $realCallableNameNoPrefix;
  46. /**
  47. * @var string
  48. *
  49. * RealCallable name for this job
  50. * natural method name will be used.
  51. */
  52. private $realCallableName;
  53. /**
  54. * @var string
  55. *
  56. * Description of Job
  57. */
  58. private $description;
  59. /**
  60. * @var integer
  61. *
  62. * Number of iterations this job will be alive before die
  63. */
  64. private $iterations;
  65. /**
  66. * @var string
  67. *
  68. * Default method this job will be call into Gearman client
  69. */
  70. private $defaultMethod;
  71. /**
  72. * @var array
  73. *
  74. * Collection of servers to connect
  75. */
  76. private $servers;
  77. /**
  78. * @var string
  79. *
  80. * The prefix to be prepended to all job callable names.
  81. */
  82. private $jobPrefix;
  83. /**
  84. * Construct method
  85. *
  86. * @param JobAnnotation $jobAnnotation JobAnnotation class
  87. * @param ReflectionMethod $reflectionMethod ReflextionMethod class
  88. * @param string $callableNameClass Callable name class
  89. * @param array $servers Array of servers defined for Worker
  90. * @param array $defaultSettings Default settings for Worker
  91. */
  92. public function __construct(
  93. JobAnnotation $jobAnnotation,
  94. ReflectionMethod $reflectionMethod,
  95. $callableNameClass,
  96. array $servers,
  97. array $defaultSettings
  98. )
  99. {
  100. $this->callableName = is_null($jobAnnotation->name)
  101. ? $reflectionMethod->getName()
  102. : $jobAnnotation->name;
  103. $this->methodName = $reflectionMethod->getName();
  104. $this->realCallableNameNoPrefix = str_replace('\\', '', $callableNameClass . '~' . $this->callableName);
  105. $this->jobPrefix = isset($defaultSettings['jobPrefix'])
  106. ? $defaultSettings['jobPrefix']
  107. : null;
  108. $this->realCallableName = $this->jobPrefix . $this->realCallableNameNoPrefix;
  109. $this->description = is_null($jobAnnotation->description)
  110. ? self::DEFAULT_DESCRIPTION
  111. : $jobAnnotation->description;
  112. $this->servers = $this->loadServers($jobAnnotation, $servers);
  113. $this->iterations = $this->loadIterations($jobAnnotation, $defaultSettings);
  114. $this->defaultMethod = $this->loadDefaultMethod($jobAnnotation, $defaultSettings);
  115. }
  116. /**
  117. * Load servers
  118. *
  119. * If any server is defined in JobAnnotation, this one is used.
  120. * Otherwise is used servers set in Class
  121. *
  122. * @param JobAnnotation $jobAnnotation JobAnnotation class
  123. * @param array $servers Array of servers defined for Worker
  124. *
  125. * @return array Servers
  126. */
  127. private function loadServers(JobAnnotation $jobAnnotation, array $servers)
  128. {
  129. /**
  130. * If is configured some servers definition in the worker, overwrites
  131. */
  132. if ($jobAnnotation->servers) {
  133. $servers = (is_array($jobAnnotation->servers) && !isset($jobAnnotation->servers['host']))
  134. ? $jobAnnotation->servers
  135. : array($jobAnnotation->servers);
  136. }
  137. return $servers;
  138. }
  139. /**
  140. * Load iterations
  141. *
  142. * If iterations is defined in JobAnnotation, this one is used.
  143. * Otherwise is used set in Class
  144. *
  145. * @param JobAnnotation $jobAnnotation JobAnnotation class
  146. * @param array $defaultSettings Default settings for Worker
  147. *
  148. * @return integer Iteration
  149. */
  150. private function loadIterations(JobAnnotation $jobAnnotation, array $defaultSettings)
  151. {
  152. return is_null($jobAnnotation->iterations)
  153. ? (int)$defaultSettings['iterations']
  154. : (int)$jobAnnotation->iterations;
  155. }
  156. /**
  157. * Load defaultMethod
  158. *
  159. * If defaultMethod is defined in JobAnnotation, this one is used.
  160. * Otherwise is used set in Class
  161. *
  162. * @param JobAnnotation $jobAnnotation JobAnnotation class
  163. * @param array $defaultSettings Default settings for Worker
  164. *
  165. * @return string Default method
  166. */
  167. private function loadDefaultMethod(JobAnnotation $jobAnnotation, array $defaultSettings)
  168. {
  169. return is_null($jobAnnotation->defaultMethod)
  170. ? $defaultSettings['method']
  171. : $jobAnnotation->defaultMethod;
  172. }
  173. /**
  174. * Retrieve all Job data in cache format
  175. *
  176. * @return array
  177. */
  178. public function toArray()
  179. {
  180. return array(
  181. 'callableName' => $this->callableName,
  182. 'methodName' => $this->methodName,
  183. 'realCallableName' => $this->realCallableName,
  184. 'jobPrefix' => $this->jobPrefix,
  185. 'realCallableNameNoPrefix' => $this->realCallableNameNoPrefix,
  186. 'description' => $this->description,
  187. 'iterations' => $this->iterations,
  188. 'servers' => $this->servers,
  189. 'defaultMethod' => $this->defaultMethod,
  190. );
  191. }
  192. }