JobClass.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 Mmoreram\GearmanBundle\Driver\Gearman\Job as JobAnnotation;
  10. use Symfony\Component\DependencyInjection\ContainerAware;
  11. use ReflectionMethod;
  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(JobAnnotation $jobAnnotation, ReflectionMethod $reflectionMethod, $callableNameClass, array $servers, array $defaultSettings)
  93. {
  94. $this->callableName = is_null($jobAnnotation->name)
  95. ? $reflectionMethod->getName()
  96. : $jobAnnotation->name;
  97. $this->methodName = $reflectionMethod->getName();
  98. $this->realCallableNameNoPrefix = str_replace('\\', '', $callableNameClass . '~' . $this->callableName);
  99. if(true === isset($defaultSettings['jobPrefix'])){
  100. $this->jobPrefix = $defaultSettings['jobPrefix'];
  101. }
  102. else{
  103. $this->jobPrefix = null;
  104. }
  105. $this->realCallableName = $this->jobPrefix . $this->realCallableNameNoPrefix;
  106. $this->description = is_null($jobAnnotation->description)
  107. ? self::DEFAULT_DESCRIPTION
  108. : $jobAnnotation->description;
  109. $this->servers = $this->loadServers($jobAnnotation, $servers);
  110. $this->iterations = $this->loadIterations($jobAnnotation, $defaultSettings);
  111. $this->defaultMethod = $this->loadDefaultMethod($jobAnnotation, $defaultSettings);
  112. }
  113. /**
  114. * Load servers
  115. *
  116. * If any server is defined in JobAnnotation, this one is used.
  117. * Otherwise is used servers set in Class
  118. *
  119. * @param JobAnnotation $jobAnnotation JobAnnotation class
  120. * @param array $servers Array of servers defined for Worker
  121. *
  122. * @return array Servers
  123. */
  124. private function loadServers(JobAnnotation $jobAnnotation, array $servers)
  125. {
  126. /**
  127. * If is configured some servers definition in the worker, overwrites
  128. */
  129. if ($jobAnnotation->servers) {
  130. $servers = ( is_array($jobAnnotation->servers) && !isset($jobAnnotation->servers['host']) )
  131. ? $jobAnnotation->servers
  132. : array($jobAnnotation->servers);
  133. }
  134. return $servers;
  135. }
  136. /**
  137. * Load iterations
  138. *
  139. * If iterations is defined in JobAnnotation, this one is used.
  140. * Otherwise is used set in Class
  141. *
  142. * @param JobAnnotation $jobAnnotation JobAnnotation class
  143. * @param array $defaultSettings Default settings for Worker
  144. *
  145. * @return integer Iteration
  146. */
  147. private function loadIterations(JobAnnotation $jobAnnotation, array $defaultSettings)
  148. {
  149. return is_null($jobAnnotation->iterations)
  150. ? (int) $defaultSettings['iterations']
  151. : (int) $jobAnnotation->iterations;
  152. }
  153. /**
  154. * Load defaultMethod
  155. *
  156. * If defaultMethod 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 string Default method
  163. */
  164. private function loadDefaultMethod(JobAnnotation $jobAnnotation, array $defaultSettings)
  165. {
  166. return is_null($jobAnnotation->defaultMethod)
  167. ? $defaultSettings['method']
  168. : $jobAnnotation->defaultMethod;
  169. }
  170. /**
  171. * Retrieve all Job data in cache format
  172. *
  173. * @return array
  174. */
  175. public function toArray()
  176. {
  177. return array(
  178. 'callableName' => $this->callableName,
  179. 'methodName' => $this->methodName,
  180. 'realCallableName' => $this->realCallableName,
  181. 'jobPrefix' => $this->jobPrefix,
  182. 'realCallableNameNoPrefix' => $this->realCallableNameNoPrefix,
  183. 'description' => $this->description,
  184. 'iterations' => $this->iterations,
  185. 'servers' => $this->servers,
  186. 'defaultMethod' => $this->defaultMethod,
  187. );
  188. }
  189. }