GearmanJobExecuteCommand.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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\Input\InputArgument;
  16. use Symfony\Component\Console\Helper\DialogHelper;
  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 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. }
  109. /**
  110. * Executes the current command.
  111. *
  112. * @param InputInterface $input An InputInterface instance
  113. * @param OutputInterface $output An OutputInterface instance
  114. *
  115. * @return integer 0 if everything went fine, or an error code
  116. *
  117. * @throws \LogicException When this abstract class is not implemented
  118. */
  119. protected function execute(InputInterface $input, OutputInterface $output)
  120. {
  121. /**
  122. * @var DialogHelper $dialog
  123. */
  124. $dialog = $this->getHelperSet()->get('dialog');
  125. if (
  126. !$input->getOption('no-interaction') &&
  127. !$dialog->askConfirmation(
  128. $output,
  129. '<question>This will execute asked job?</question>',
  130. 'y'
  131. )
  132. ) {
  133. return;
  134. }
  135. if (!$input->getOption('quiet')) {
  136. $output->writeln(sprintf(
  137. '<info>[%s] loading...</info>',
  138. date('Y-m-d H:i:s')
  139. ));
  140. }
  141. $job = $input->getArgument('job');
  142. $jobStructure = $this
  143. ->gearmanClient
  144. ->getJob($job);
  145. if (!$input->getOption('quiet')) {
  146. $this
  147. ->gearmanDescriber
  148. ->describeJob(
  149. $output,
  150. $jobStructure,
  151. true
  152. );
  153. }
  154. if (!$input->getOption('quiet')) {
  155. $output->writeln(sprintf(
  156. '<info>[%s] loaded. Ctrl+C to break</info>',
  157. date('Y-m-d H:i:s')
  158. ));
  159. }
  160. $this
  161. ->gearmanExecute
  162. ->executeJob($job);
  163. }
  164. }