GearmanJobExecuteCommand.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace Mmoreram\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 Job Execute Command class
  11. *
  12. * @author Marc Morera <yuhu@mmoreram.com>
  13. */
  14. class GearmanJobExecuteCommand extends ContainerAwareCommand
  15. {
  16. /**
  17. * Console Command configuration
  18. */
  19. protected function configure()
  20. {
  21. parent::configure();
  22. $this->setName('gearman:job:execute')
  23. ->setDescription('Execute one single job')
  24. ->addArgument('job', InputArgument::REQUIRED, 'job to execute')
  25. ->addOption('no-description', null, InputOption::VALUE_NONE, 'Don\'t print job 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 job?</question>', 'y')) {
  41. return;
  42. }
  43. $output->writeln(sprintf('<info>[%s] loading...</info>', date('Y-m-d H:i:s')));
  44. $job = $input->getArgument('job');
  45. $jobStruct = $this->getContainer()->get('gearman')->getJob($job);
  46. if (!$input->getOption('no-description')) {
  47. $this->getContainer()->get('gearman.describer')->describeJob($output, $jobStruct, 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')->executeJob($job);
  51. }
  52. }