JobClass.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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;
  10. use Mmoreram\GearmanBundle\Driver\Gearman\Work;
  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 Job $methodAnnotation MethodAnnotation 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( Job $methodAnnotation, ReflectionMethod $method, $callableNameClass, array $servers, array $defaultSettings)
  75. {
  76. $this->callableName = is_null($methodAnnotation->name)
  77. ? $method->getName()
  78. : $methodAnnotation->name;
  79. $this->methodName = $method->getName();
  80. $this->realCallableName = str_replace('\\', '', $callableNameClass . '~' . $this->callableName);
  81. $this->description = is_null($methodAnnotation->description)
  82. ? 'No description is defined'
  83. : $methodAnnotation->description;
  84. $this->iterations = is_null($methodAnnotation->iterations)
  85. ? (int) $defaultSettings['iterations']
  86. : $methodAnnotation->iterations;
  87. $this->defaultMethod = is_null($methodAnnotation->defaultMethod)
  88. ? $defaultSettings['method']
  89. : $methodAnnotation->defaultMethod;
  90. /**
  91. * By default, this job takes default servers defined in its worker
  92. */
  93. $this->servers = $servers;
  94. /**
  95. * If is configured some servers definition in the worker, overwrites
  96. */
  97. if ($methodAnnotation->servers) {
  98. $this->servers = ( is_array($methodAnnotation->servers) && !isset($methodAnnotation->servers['host']) )
  99. ? $methodAnnotation->servers
  100. : array($methodAnnotation->servers);
  101. }
  102. }
  103. /**
  104. * Retrieve all Job data in cache format
  105. *
  106. * @return array
  107. */
  108. public function toArray()
  109. {
  110. return array(
  111. 'callableName' => $this->callableName,
  112. 'methodName' => $this->methodName,
  113. 'realCallableName' => $this->realCallableName,
  114. 'description' => $this->description,
  115. 'iterations' => $this->iterations,
  116. 'servers' => $this->servers,
  117. 'defaultMethod' => $this->defaultMethod,
  118. );
  119. }
  120. }