JobClass.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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
  43. * natural method name will be used.
  44. */
  45. private $realCallableName;
  46. /**
  47. * @var string
  48. *
  49. * Description of Job
  50. */
  51. private $description;
  52. /**
  53. * @var integer
  54. *
  55. * Number of iterations this job will be alive before die
  56. */
  57. private $iterations;
  58. /**
  59. * @var string
  60. *
  61. * Default method this job will be call into Gearman client
  62. */
  63. private $defaultMethod;
  64. /**
  65. * @var array
  66. *
  67. * Collection of servers to connect
  68. */
  69. private $servers;
  70. /**
  71. * Construct method
  72. *
  73. * @param JobAnnotation $jobAnnotation JobAnnotation class
  74. * @param ReflectionMethod $method ReflextionMethod class
  75. * @param string $callableNameClass Callable name class
  76. * @param array $servers Array of servers defined for Worker
  77. * @param array $defaultSettings Default settings for Worker
  78. */
  79. public function __construct(JobAnnotation $jobAnnotation, ReflectionMethod $method, $callableNameClass, array $servers, array $defaultSettings)
  80. {
  81. $this->callableName = is_null($jobAnnotation->name)
  82. ? $method->getName()
  83. : $jobAnnotation->name;
  84. $this->methodName = $method->getName();
  85. $this->realCallableName = str_replace('\\', '', $callableNameClass . '~' . $this->callableName);
  86. $this->description = is_null($jobAnnotation->description)
  87. ? self::DEFAULT_DESCRIPTION
  88. : $jobAnnotation->description;
  89. $this->servers = $this->loadServers($jobAnnotation, $servers);
  90. $this->iterations = $this->loadIterations($jobAnnotation, $defaultSettings);
  91. $this->defaultMethod = $this->loadDefaultMethod($jobAnnotation, $defaultSettings);
  92. }
  93. /**
  94. * Load servers
  95. *
  96. * If any server is defined in JobAnnotation, this one is used.
  97. * Otherwise is used servers set in Class
  98. *
  99. * @param JobAnnotation $jobAnnotation JobAnnotation class
  100. * @param array $servers Array of servers defined for Worker
  101. *
  102. * @return array Servers
  103. */
  104. private function loadServers(JobAnnotation $jobAnnotation, array $servers)
  105. {
  106. /**
  107. * If is configured some servers definition in the worker, overwrites
  108. */
  109. if ($jobAnnotation->servers) {
  110. $servers = ( is_array($jobAnnotation->servers) && !isset($jobAnnotation->servers['host']) )
  111. ? $jobAnnotation->servers
  112. : array($jobAnnotation->servers);
  113. }
  114. return $servers;
  115. }
  116. /**
  117. * Load iterations
  118. *
  119. * If iterations is defined in JobAnnotation, this one is used.
  120. * Otherwise is used set in Class
  121. *
  122. * @param JobAnnotation $jobAnnotation JobAnnotation class
  123. * @param array $defaultSettings Default settings for Worker
  124. *
  125. * @return integer Iteration
  126. */
  127. private function loadIterations(JobAnnotation $jobAnnotation, array $defaultSettings)
  128. {
  129. return is_null($jobAnnotation->iterations)
  130. ? (int) $defaultSettings['iterations']
  131. : (int) $jobAnnotation->iterations;
  132. }
  133. /**
  134. * Load defaultMethod
  135. *
  136. * If defaultMethod is defined in JobAnnotation, this one is used.
  137. * Otherwise is used set in Class
  138. *
  139. * @param JobAnnotation $jobAnnotation JobAnnotation class
  140. * @param array $defaultSettings Default settings for Worker
  141. *
  142. * @return string Default method
  143. */
  144. private function loadDefaultMethod(JobAnnotation $jobAnnotation, array $defaultSettings)
  145. {
  146. return is_null($jobAnnotation->defaultMethod)
  147. ? $defaultSettings['method']
  148. : $jobAnnotation->defaultMethod;
  149. }
  150. /**
  151. * Retrieve all Job data in cache format
  152. *
  153. * @return array
  154. */
  155. public function toArray()
  156. {
  157. return array(
  158. 'callableName' => $this->callableName,
  159. 'methodName' => $this->methodName,
  160. 'realCallableName' => $this->realCallableName,
  161. 'description' => $this->description,
  162. 'iterations' => $this->iterations,
  163. 'servers' => $this->servers,
  164. 'defaultMethod' => $this->defaultMethod,
  165. );
  166. }
  167. }