GearmanWorkerExecuteCommand.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * Gearman Bundle for Symfony2
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. *
  8. * Feel free to edit as you please, and have fun.
  9. *
  10. * @author Marc Morera <yuhu@mmoreram.com>
  11. */
  12. namespace Mmoreram\GearmanBundle\Command;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Helper\DialogHelper;
  16. use Symfony\Component\Console\Input\InputArgument;
  17. use Symfony\Component\Console\Input\InputOption;
  18. use Mmoreram\GearmanBundle\Command\Abstracts\AbstractGearmanCommand;
  19. use Mmoreram\GearmanBundle\Service\GearmanDescriber;
  20. use Mmoreram\GearmanBundle\Service\GearmanExecute;
  21. use Mmoreram\GearmanBundle\Service\GearmanClient;
  22. /**
  23. * Gearman Worker Execute Command class
  24. *
  25. * @since 2.3.1
  26. */
  27. class GearmanWorkerExecuteCommand extends AbstractGearmanCommand
  28. {
  29. /**
  30. * @var GearmanClient
  31. *
  32. * Gearman client
  33. */
  34. protected $gearmanClient;
  35. /**
  36. * @var GearmanDescriber
  37. *
  38. * GearmanDescriber
  39. */
  40. protected $gearmanDescriber;
  41. /**
  42. * @var GearmanExecute
  43. *
  44. * Gearman execute
  45. */
  46. protected $gearmanExecute;
  47. /**
  48. * Set gearman client
  49. *
  50. * @param GearmanClient $gearmanClient Gearman client
  51. *
  52. * @return GearmanWorkerExecuteCommand self Object
  53. */
  54. public function setGearmanClient(GearmanClient $gearmanClient)
  55. {
  56. $this->gearmanClient = $gearmanClient;
  57. return $this;
  58. }
  59. /**
  60. * set Gearman describer
  61. *
  62. * @param GearmanDescriber $gearmanDescriber GearmanDescriber
  63. *
  64. * @return GearmanWorkerExecuteCommand self Object
  65. */
  66. public function setGearmanDescriber(GearmanDescriber $gearmanDescriber)
  67. {
  68. $this->gearmanDescriber = $gearmanDescriber;
  69. return $this;
  70. }
  71. /**
  72. * set Gearman execute
  73. *
  74. * @param GearmanExecute $gearmanExecute GearmanExecute
  75. *
  76. * @return GearmanWorkerExecuteCommand self Object
  77. */
  78. public function setGearmanExecute(GearmanExecute $gearmanExecute)
  79. {
  80. $this->gearmanExecute = $gearmanExecute;
  81. return $this;
  82. }
  83. /**
  84. * Console Command configuration
  85. */
  86. protected function configure()
  87. {
  88. parent::configure();
  89. $this
  90. ->setName('gearman:worker:execute')
  91. ->setDescription('Execute one worker with all contained Jobs')
  92. ->addArgument(
  93. 'worker',
  94. InputArgument::REQUIRED,
  95. 'work to execute'
  96. )
  97. ->addOption(
  98. 'no-description',
  99. null,
  100. InputOption::VALUE_NONE,
  101. 'Don\'t print worker description'
  102. );
  103. }
  104. /**
  105. * Executes the current command.
  106. *
  107. * @param InputInterface $input An InputInterface instance
  108. * @param OutputInterface $output An OutputInterface instance
  109. *
  110. * @return integer 0 if everything went fine, or an error code
  111. *
  112. * @throws \LogicException When this abstract class is not implemented
  113. */
  114. protected function execute(InputInterface $input, OutputInterface $output)
  115. {
  116. /**
  117. * @var DialogHelper $dialog
  118. */
  119. $dialog = $this
  120. ->getHelperSet()
  121. ->get('dialog');
  122. if (
  123. !$input->getOption('no-interaction') &&
  124. !$dialog->askConfirmation(
  125. $output,
  126. '<question>This will execute asked worker with all its jobs?</question>',
  127. 'y')
  128. ) {
  129. return;
  130. }
  131. if (!$input->getOption('quiet')) {
  132. $output->writeln(sprintf(
  133. '<info>[%s] loading...</info>',
  134. date('Y-m-d H:i:s')
  135. ));
  136. }
  137. $worker = $input->getArgument('worker');
  138. $workerStructure = $this
  139. ->gearmanClient
  140. ->getWorker($worker);
  141. if (
  142. !$input->getOption('no-description') &&
  143. !$input->getOption('quiet')
  144. ) {
  145. $this
  146. ->gearmanDescriber
  147. ->describeWorker(
  148. $output,
  149. $workerStructure,
  150. true
  151. );
  152. }
  153. if (!$input->getOption('quiet')) {
  154. $output->writeln(sprintf(
  155. '<info>[%s] loaded. Ctrl+C to break</info>',
  156. date('Y-m-d H:i:s')
  157. ));
  158. }
  159. $this
  160. ->gearmanExecute
  161. ->executeWorker($worker);
  162. }
  163. }