JobClass.php 6.1 KB

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