GearmanWorkerExecuteCommand.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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\Helper\DialogHelper;
  14. use Symfony\Component\Console\Input\InputArgument;
  15. use Symfony\Component\Console\Input\InputInterface;
  16. use Symfony\Component\Console\Input\InputOption;
  17. use Symfony\Component\Console\Output\OutputInterface;
  18. use Mmoreram\GearmanBundle\Command\Abstracts\AbstractGearmanCommand;
  19. use Mmoreram\GearmanBundle\Service\GearmanClient;
  20. use Mmoreram\GearmanBundle\Service\GearmanDescriber;
  21. use Mmoreram\GearmanBundle\Service\GearmanExecute;
  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. ->addOption(
  104. 'iterations',
  105. null,
  106. InputOption::VALUE_OPTIONAL,
  107. 'Override configured iterations'
  108. )
  109. ->addOption(
  110. 'minimum-execution-time',
  111. null,
  112. InputOption::VALUE_OPTIONAL,
  113. 'Override configured minimum execution time'
  114. )
  115. ->addOption(
  116. 'timeout',
  117. null,
  118. InputOption::VALUE_OPTIONAL,
  119. 'Override configured timeout'
  120. );
  121. }
  122. /**
  123. * Executes the current command.
  124. *
  125. * @param InputInterface $input An InputInterface instance
  126. * @param OutputInterface $output An OutputInterface instance
  127. *
  128. * @return integer 0 if everything went fine, or an error code
  129. *
  130. * @throws \LogicException When this abstract class is not implemented
  131. */
  132. protected function execute(InputInterface $input, OutputInterface $output)
  133. {
  134. /**
  135. * @var DialogHelper $dialog
  136. */
  137. $dialog = $this
  138. ->getHelperSet()
  139. ->get('dialog');
  140. if (
  141. !$input->getOption('no-interaction') &&
  142. !$dialog->askConfirmation(
  143. $output,
  144. '<question>This will execute asked worker with all its jobs?</question>',
  145. 'y')
  146. ) {
  147. return;
  148. }
  149. if (!$input->getOption('quiet')) {
  150. $output->writeln(sprintf(
  151. '<info>[%s] loading...</info>',
  152. date('Y-m-d H:i:s')
  153. ));
  154. }
  155. $worker = $input->getArgument('worker');
  156. $workerStructure = $this
  157. ->gearmanClient
  158. ->getWorker($worker);
  159. if (
  160. !$input->getOption('no-description') &&
  161. !$input->getOption('quiet')
  162. ) {
  163. $this
  164. ->gearmanDescriber
  165. ->describeWorker(
  166. $output,
  167. $workerStructure,
  168. true
  169. );
  170. }
  171. if (!$input->getOption('quiet')) {
  172. $output->writeln(sprintf(
  173. '<info>[%s] loaded. Ctrl+C to break</info>',
  174. date('Y-m-d H:i:s')
  175. ));
  176. }
  177. $this
  178. ->gearmanExecute
  179. ->setOutput($output)
  180. ->executeWorker($worker, array(
  181. 'iterations' => $input->getOption('iterations'),
  182. 'minimum_execution_time' => $input->getOption('minimum-execution-time'),
  183. 'timeout' => $input->getOption('timeout')
  184. ));
  185. }
  186. }