JobClass.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 Mmoreram\GearmanBundle\Driver\Gearman\Work as WorkAnnotation;
  11. use Symfony\Component\DependencyInjection\ContainerAware;
  12. use ReflectionMethod;
  13. /**
  14. * Job class
  15. *
  16. * This class provide all worker definition.
  17. */
  18. class JobClass extends ContainerAware
  19. {
  20. /**
  21. * @var string
  22. *
  23. * Callable name for this job
  24. * If is setted on annotations, this value will be used
  25. * otherwise, natural method name will be used.
  26. */
  27. private $callableName;
  28. /**
  29. * @var string
  30. *
  31. * Method name
  32. */
  33. private $methodName;
  34. /**
  35. * @var string
  36. *
  37. * RealCallable name for this job
  38. * natural method name will be used.
  39. */
  40. private $realCallableName;
  41. /**
  42. * @var string
  43. *
  44. * Description of Job
  45. */
  46. private $description;
  47. /**
  48. * @var integer
  49. *
  50. * Number of iterations this job will be alive before die
  51. */
  52. private $iterations;
  53. /**
  54. * @var string
  55. *
  56. * Default method this job will be call into Gearman client
  57. */
  58. private $defaultMethod;
  59. /**
  60. * @var array
  61. *
  62. * Collection of servers to connect
  63. */
  64. private $servers;
  65. /**
  66. * Construct method
  67. *
  68. * @param JobAnnotation $jobAnnotation jobAnnotation class
  69. * @param ReflectionMethod $method ReflextionMethod class
  70. * @param string $callableNameClass Callable name class
  71. * @param array $servers Array of servers defined for Worker
  72. * @param array $defaultSettings Default settings for Worker
  73. */
  74. public function __construct(JobAnnotation $jobAnnotation, ReflectionMethod $method, $callableNameClass, array $servers, array $defaultSettings)
  75. {
  76. $this->callableName = is_null($jobAnnotation->name)
  77. ? $method->getName()
  78. : $jobAnnotation->name;
  79. $this->methodName = $method->getName();
  80. $this->realCallableName = str_replace('\\', '', $callableNameClass . '~' . $this->callableName);
  81. $this->description = is_null($jobAnnotation->description)
  82. ? 'No description is defined'
  83. : $jobAnnotation->description;
  84. $this
  85. ->loadSettings($jobAnnotation, $defaultSettings)
  86. ->loadServers($jobAnnotation, $servers);
  87. }
  88. /**
  89. * Load settings
  90. *
  91. * @param JobAnnotation $jobAnnotation JobAnnotation class
  92. * @param array $servers Array of servers defined for Worker
  93. *
  94. * @return JobClass self Object
  95. */
  96. private function loadServers(JobAnnotation $jobAnnotation, array $servers)
  97. {
  98. /**
  99. * By default, this job takes default servers defined in its worker
  100. */
  101. $this->servers = $servers;
  102. /**
  103. * If is configured some servers definition in the worker, overwrites
  104. */
  105. if ($jobAnnotation->servers) {
  106. $this->servers = ( is_array($jobAnnotation->servers) && !isset($jobAnnotation->servers['host']) )
  107. ? $jobAnnotation->servers
  108. : array($jobAnnotation->servers);
  109. }
  110. return $this;
  111. }
  112. /**
  113. * Load settings
  114. *
  115. * @param WorkAnnotation $JobAnnotation JobAnnotation class
  116. * @param array $defaultSettings Default settings for Worker
  117. *
  118. * @return JobClass self Object
  119. */
  120. private function loadSettings(JobAnnotation $jobAnnotation, array $defaultSettings)
  121. {
  122. $this->iterations = is_null($jobAnnotation->iterations)
  123. ? (int) $defaultSettings['iterations']
  124. : $jobAnnotation->iterations;
  125. $this->defaultMethod = is_null($jobAnnotation->defaultMethod)
  126. ? $defaultSettings['method']
  127. : $jobAnnotation->defaultMethod;
  128. return $this;
  129. }
  130. /**
  131. * Retrieve all Job data in cache format
  132. *
  133. * @return array
  134. */
  135. public function toArray()
  136. {
  137. return array(
  138. 'callableName' => $this->callableName,
  139. 'methodName' => $this->methodName,
  140. 'realCallableName' => $this->realCallableName,
  141. 'description' => $this->description,
  142. 'iterations' => $this->iterations,
  143. 'servers' => $this->servers,
  144. 'defaultMethod' => $this->defaultMethod,
  145. );
  146. }
  147. }