JobClass.php 4.7 KB

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