GearmanWorkerExecuteCommand.php 2.2 KB

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