GearmanJobExecuteCommand.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 Job Execute Command class
  24. *
  25. * @since 2.3.1
  26. */
  27. class GearmanJobExecuteCommand 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. * @var DialogHelper
  49. *
  50. * Dialog
  51. */
  52. protected $dialog;
  53. /**
  54. * Set gearman client
  55. *
  56. * @param GearmanClient $gearmanClient Gearman client
  57. *
  58. * @return GearmanJobExecuteCommand self Object
  59. */
  60. public function setGearmanClient(GearmanClient $gearmanClient)
  61. {
  62. $this->gearmanClient = $gearmanClient;
  63. return $this;
  64. }
  65. /**
  66. * set Gearman describer
  67. *
  68. * @param GearmanDescriber $gearmanDescriber GearmanDescriber
  69. *
  70. * @return GearmanJobExecuteCommand self Object
  71. */
  72. public function setGearmanDescriber(GearmanDescriber $gearmanDescriber)
  73. {
  74. $this->gearmanDescriber = $gearmanDescriber;
  75. return $this;
  76. }
  77. /**
  78. * set Gearman execute
  79. *
  80. * @param GearmanExecute $gearmanExecute GearmanExecute
  81. *
  82. * @return GearmanJobExecuteCommand self Object
  83. */
  84. public function setGearmanExecute(GearmanExecute $gearmanExecute)
  85. {
  86. $this->gearmanExecute = $gearmanExecute;
  87. return $this;
  88. }
  89. /**
  90. * Console Command configuration
  91. */
  92. protected function configure()
  93. {
  94. $this
  95. ->setName('gearman:job:execute')
  96. ->setDescription('Execute one single job')
  97. ->addArgument(
  98. 'job',
  99. InputArgument::REQUIRED,
  100. 'job to execute'
  101. )
  102. ->addOption(
  103. 'no-description',
  104. null,
  105. InputOption::VALUE_NONE,
  106. 'Don\'t print job description'
  107. )
  108. ->addOption(
  109. 'iterations',
  110. null,
  111. InputOption::VALUE_OPTIONAL,
  112. 'Override configured iterations'
  113. )
  114. ->addOption(
  115. 'minimum-execution-time',
  116. null,
  117. InputOption::VALUE_OPTIONAL,
  118. 'Override configured minimum execution time'
  119. )
  120. ->addOption(
  121. 'timeout',
  122. null,
  123. InputOption::VALUE_OPTIONAL,
  124. 'Override configured timeout'
  125. );
  126. }
  127. /**
  128. * Executes the current command.
  129. *
  130. * @param InputInterface $input An InputInterface instance
  131. * @param OutputInterface $output An OutputInterface instance
  132. *
  133. * @return integer 0 if everything went fine, or an error code
  134. *
  135. * @throws \LogicException When this abstract class is not implemented
  136. */
  137. protected function execute(InputInterface $input, OutputInterface $output)
  138. {
  139. /**
  140. * @var DialogHelper $dialog
  141. */
  142. $dialog = $this->getHelperSet()->get('dialog');
  143. if (
  144. !$input->getOption('no-interaction') &&
  145. !$dialog->askConfirmation(
  146. $output,
  147. '<question>This will execute asked job?</question>',
  148. 'y'
  149. )
  150. ) {
  151. return;
  152. }
  153. if (!$input->getOption('quiet')) {
  154. $output->writeln(sprintf(
  155. '<info>[%s] loading...</info>',
  156. date('Y-m-d H:i:s')
  157. ));
  158. }
  159. $job = $input->getArgument('job');
  160. $jobStructure = $this
  161. ->gearmanClient
  162. ->getJob($job);
  163. if (
  164. !$input->getOption('no-description') &&
  165. !$input->getOption('quiet')
  166. ) {
  167. $this
  168. ->gearmanDescriber
  169. ->describeJob(
  170. $output,
  171. $jobStructure,
  172. true
  173. );
  174. }
  175. if (!$input->getOption('quiet')) {
  176. $output->writeln(sprintf(
  177. '<info>[%s] loaded. Ctrl+C to break</info>',
  178. date('Y-m-d H:i:s')
  179. ));
  180. }
  181. $this
  182. ->gearmanExecute
  183. ->setOutput($output)
  184. ->executeJob($job, array(
  185. 'iterations' => $input->getOption('iterations'),
  186. 'minimum_execution_time' => $input->getOption('minimum-execution-time'),
  187. 'timeout' => $input->getOption('timeout')
  188. ));
  189. }
  190. }