JobClass.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace Mmoreram\GearmanBundle\Module;
  3. use Mmoreram\GearmanBundle\Driver\Gearman\Job;
  4. use Mmoreram\GearmanBundle\Driver\Gearman\Work;
  5. use Symfony\Component\DependencyInjection\ContainerAware;
  6. use Mmoreram\GearmanBundle\Exceptions\SettingValueMissingException;
  7. use Mmoreram\GearmanBundle\Exceptions\SettingValueBadFormatException;
  8. use ReflectionMethod;
  9. /**
  10. * Job class
  11. *
  12. * @author Marc Morera <yuhu@mmoreram.com>
  13. */
  14. class JobClass extends ContainerAware
  15. {
  16. /**
  17. * @var string
  18. *
  19. * Callable name for this job
  20. * If is setted on annotations, this value will be used
  21. * otherwise, natural method name will be used.
  22. */
  23. private $callableName;
  24. /**
  25. * @var string
  26. *
  27. * Method name
  28. */
  29. private $methodName;
  30. /**
  31. * @var string
  32. *
  33. * RealCallable name for this job
  34. * natural method name will be used.
  35. */
  36. private $realCallableName;
  37. /**
  38. * @var string
  39. *
  40. * Description of Job
  41. */
  42. private $description;
  43. /**
  44. * @var integer
  45. *
  46. * Number of iterations this job will be alive before die
  47. */
  48. private $iterations;
  49. /**
  50. * @var string
  51. *
  52. * Default method this job will be call into Gearman client
  53. */
  54. private $defaultMethod;
  55. /**
  56. * @var array
  57. *
  58. * Collection of servers to connect
  59. */
  60. private $servers;
  61. /**
  62. * Construct method
  63. *
  64. * @param Job $methodAnnotation MethodAnnotation class
  65. * @param ReflectionMethod $method ReflextionMethod class
  66. * @param Work $classAnnotation Work class
  67. * @param string $callableNameClass Callable name class
  68. * @param array $settings Settings structure
  69. */
  70. public function __construct( Job $methodAnnotation, ReflectionMethod $method, $callableNameClass, array $servers, array $defaultSettings)
  71. {
  72. $this->callableName = is_null($methodAnnotation->name)
  73. ? $method->getName()
  74. : $methodAnnotation->name;
  75. $this->methodName = $method->getName();
  76. $this->realCallableName = str_replace('\\', '', $callableNameClass . '~' . $this->callableName);
  77. $this->description = is_null($methodAnnotation->description)
  78. ? 'No description is defined'
  79. : $methodAnnotation->description;
  80. $this->iterations = is_null($methodAnnotation->iterations)
  81. ? (int) $defaultSettings['iterations']
  82. : $methodAnnotation->iterations;
  83. $this->defaultMethod = is_null($methodAnnotation->defaultMethod)
  84. ? $defaultSettings['method']
  85. : $methodAnnotation->defaultMethod;
  86. /**
  87. * By default, this worker takes default servers definition
  88. */
  89. $this->servers = $servers;
  90. /**
  91. * If is configured some servers definition in the worker, overwrites
  92. */
  93. if ($methodAnnotation->servers) {
  94. if (is_array($methodAnnotation->servers)) {
  95. $this->servers = $methodAnnotation->servers;
  96. } else {
  97. $this->servers = array($methodAnnotation->servers);
  98. }
  99. }
  100. }
  101. /**
  102. * Retrieve all Job data in cache format
  103. *
  104. * @return array
  105. */
  106. public function toArray()
  107. {
  108. return array(
  109. 'callableName' => $this->callableName,
  110. 'methodName' => $this->methodName,
  111. 'realCallableName' => $this->realCallableName,
  112. 'description' => $this->description,
  113. 'iterations' => $this->iterations,
  114. 'servers' => $this->servers,
  115. 'defaultMethod' => $this->defaultMethod,
  116. );
  117. }
  118. }