GearmanWorkerExecuteCommand.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * Gearman Bundle for Symfony2
  4. *
  5. * @author Marc Morera <yuhu@mmoreram.com>
  6. * @since 2013
  7. */
  8. namespace Mmoreram\GearmanBundle\Command;
  9. use Symfony\Component\Console\Input\InputOption;
  10. use Symfony\Component\Console\Input\InputArgument;
  11. use Symfony\Component\Console\Input\InputInterface;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  14. /**
  15. * Gearman Worker Execute Command class
  16. *
  17. * @author Marc Morera <yuhu@mmoreram.com>
  18. */
  19. class GearmanWorkerExecuteCommand extends ContainerAwareCommand
  20. {
  21. /**
  22. * Console Command configuration
  23. */
  24. protected function configure()
  25. {
  26. parent::configure();
  27. $this->setName('gearman:worker:execute')
  28. ->setDescription('Execute one worker with all contained Jobs')
  29. ->addArgument('worker', InputArgument::REQUIRED, 'work to execute')
  30. ->addOption('no-description', null, InputOption::VALUE_NONE, 'Don\'t print worker description');
  31. }
  32. /**
  33. * Executes the current command.
  34. *
  35. * @param InputInterface $input An InputInterface instance
  36. * @param OutputInterface $output An OutputInterface instance
  37. *
  38. * @return integer 0 if everything went fine, or an error code
  39. *
  40. * @throws \LogicException When this abstract class is not implemented
  41. */
  42. protected function execute(InputInterface $input, OutputInterface $output)
  43. {
  44. $dialog = $this->getHelperSet()->get('dialog');
  45. if (!$input->getOption('no-interaction') && !$dialog->askConfirmation($output, '<question>This will execute asked worker with all its jobs?</question>', 'y')) {
  46. return;
  47. }
  48. $output->writeln(sprintf('<info>[%s] loading...</info>', date('Y-m-d H:i:s')));
  49. $worker = $input->getArgument('worker');
  50. $workerStruct = $this->getContainer()->get('gearman')->getWorker($worker);
  51. if (!$input->getOption('no-description')) {
  52. $this->getContainer()->get('gearman.describer')->describeWorker($output, $workerStruct, true);
  53. }
  54. $output->writeln(sprintf('<info>[%s] loaded. Ctrl+C to break</info>', date('Y-m-d H:i:s')));
  55. $this->getContainer()->get('gearman.execute')->executeWorker($worker);
  56. }
  57. }