RunWorkersCommand.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php declare(strict_types=1);
  2. namespace Packagist\WebBundle\Command;
  3. use Symfony\Component\Console\Input\InputInterface;
  4. use Symfony\Component\Console\Input\InputOption;
  5. use Symfony\Component\Console\Output\OutputInterface;
  6. use Symfony\Component\Filesystem\LockHandler;
  7. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  8. use Seld\Signal\SignalHandler;
  9. class RunWorkersCommand extends ContainerAwareCommand
  10. {
  11. protected function configure()
  12. {
  13. $this
  14. ->setName('packagist:run-workers')
  15. ->setDescription('Run worker services')
  16. ->addOption('messages', null, InputOption::VALUE_OPTIONAL, 'Amount of messages to process before exiting', 5000)
  17. ->addOption('worker-id', 'w', InputOption::VALUE_OPTIONAL, 'Unique worker ID', '1')
  18. ;
  19. }
  20. protected function execute(InputInterface $input, OutputInterface $output)
  21. {
  22. $lock = new LockHandler('packagist_run_' . $input->getOption('worker-id'));
  23. // another dumper is still active
  24. if (!$lock->lock()) {
  25. if ($input->getOption('verbose')) {
  26. $output->writeln('Aborting, another of the same worker is still active');
  27. }
  28. return;
  29. }
  30. try {
  31. $logger = $this->getContainer()->get('logger');
  32. $worker = $this->getContainer()->get('packagist.queue_worker');
  33. $logger->notice('Worker started successfully');
  34. $logger->reset();
  35. $worker->processMessages((int) $input->getOption('messages'));
  36. $logger->notice('Worker exiting successfully');
  37. } finally {
  38. $lock->release();
  39. }
  40. }
  41. }