GearmanWorkerExecuteCommand.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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
  28. ->setName('gearman:worker:execute')
  29. ->setDescription('Execute one worker with all contained Jobs')
  30. ->addArgument('worker', InputArgument::REQUIRED, 'work to execute')
  31. ->addOption('no-description', null, InputOption::VALUE_NONE, 'Don\'t print worker description');
  32. }
  33. /**
  34. * Executes the current command.
  35. *
  36. * @param InputInterface $input An InputInterface instance
  37. * @param OutputInterface $output An OutputInterface instance
  38. *
  39. * @return integer 0 if everything went fine, or an error code
  40. *
  41. * @throws \LogicException When this abstract class is not implemented
  42. */
  43. protected function execute(InputInterface $input, OutputInterface $output)
  44. {
  45. $dialog = $this->getHelperSet()->get('dialog');
  46. if (!$input->getOption('no-interaction') && !$dialog->askConfirmation($output, '<question>This will execute asked worker with all its jobs?</question>', 'y')) {
  47. return;
  48. }
  49. $output->writeln(sprintf('<info>[%s] loading...</info>', date('Y-m-d H:i:s')));
  50. $worker = $input->getArgument('worker');
  51. $workerStruct = $this->getContainer()->get('gearman')->getWorker($worker);
  52. if (!$input->getOption('no-description')) {
  53. $this->getContainer()->get('gearman.describer')->describeWorker($output, $workerStruct, true);
  54. }
  55. $output->writeln(sprintf('<info>[%s] loaded. Ctrl+C to break</info>', date('Y-m-d H:i:s')));
  56. $this->getContainer()->get('gearman.execute')->executeWorker($worker);
  57. }
  58. }