JobClass.php 6.0 KB

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