|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace OldSound\RabbitMqBundle\Command; |
| 4 | + |
| 5 | +use OldSound\RabbitMqBundle\RabbitMq\BatchConsumer; |
| 6 | +use PhpAmqpLib\Exception\AMQPTimeoutException; |
| 7 | +use Symfony\Component\Console\Input\InputArgument; |
| 8 | +use Symfony\Component\Console\Input\InputInterface; |
| 9 | +use Symfony\Component\Console\Input\InputOption; |
| 10 | +use Symfony\Component\Console\Output\OutputInterface; |
| 11 | + |
| 12 | +final class BatchConsumerCommand extends BaseRabbitMqCommand |
| 13 | +{ |
| 14 | + /** |
| 15 | + * @var BatchConsumer |
| 16 | + */ |
| 17 | + protected $consumer; |
| 18 | + |
| 19 | + public function stopConsumer() |
| 20 | + { |
| 21 | + if ($this->consumer instanceof BatchConsumer) { |
| 22 | + // Process current message, then halt consumer |
| 23 | + $this->consumer->forceStopConsumer(); |
| 24 | + |
| 25 | + // Halt consumer if waiting for a new message from the queue |
| 26 | + try { |
| 27 | + $this->consumer->stopConsuming(); |
| 28 | + } catch (AMQPTimeoutException $e) {} |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + protected function configure() |
| 33 | + { |
| 34 | + parent::configure(); |
| 35 | + |
| 36 | + $this |
| 37 | + ->setName('rabbitmq:batch:consumer') |
| 38 | + ->addArgument('name', InputArgument::REQUIRED, 'Consumer Name') |
| 39 | + ->addOption('route', 'r', InputOption::VALUE_OPTIONAL, 'Routing Key', '') |
| 40 | + ->addOption('memory-limit', 'l', InputOption::VALUE_OPTIONAL, 'Allowed memory for this process', null) |
| 41 | + ->addOption('debug', 'd', InputOption::VALUE_NONE, 'Enable Debugging') |
| 42 | + ->addOption('without-signals', 'w', InputOption::VALUE_NONE, 'Disable catching of system signals') |
| 43 | + ->setDescription('Executes a Batch Consumer'); |
| 44 | + ; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Executes the current command. |
| 49 | + * |
| 50 | + * @param InputInterface $input An InputInterface instance |
| 51 | + * @param OutputInterface $output An OutputInterface instance |
| 52 | + * |
| 53 | + * @return integer 0 if everything went fine, or an error code |
| 54 | + * |
| 55 | + * @throws \InvalidArgumentException When the number of messages to consume is less than 0 |
| 56 | + * @throws \BadFunctionCallException When the pcntl is not installed and option -s is true |
| 57 | + */ |
| 58 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 59 | + { |
| 60 | + if (defined('AMQP_WITHOUT_SIGNALS') === false) { |
| 61 | + define('AMQP_WITHOUT_SIGNALS', $input->getOption('without-signals')); |
| 62 | + } |
| 63 | + |
| 64 | + if (!AMQP_WITHOUT_SIGNALS && extension_loaded('pcntl')) { |
| 65 | + if (!function_exists('pcntl_signal')) { |
| 66 | + throw new \BadFunctionCallException("Function 'pcntl_signal' is referenced in the php.ini 'disable_functions' and can't be called."); |
| 67 | + } |
| 68 | + |
| 69 | + pcntl_signal(SIGTERM, array(&$this, 'stopConsumer')); |
| 70 | + pcntl_signal(SIGINT, array(&$this, 'stopConsumer')); |
| 71 | + } |
| 72 | + |
| 73 | + if (defined('AMQP_DEBUG') === false) { |
| 74 | + define('AMQP_DEBUG', (bool) $input->getOption('debug')); |
| 75 | + } |
| 76 | + |
| 77 | + $this->initConsumer($input); |
| 78 | + |
| 79 | + return $this->consumer->consume(); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @param InputInterface $input |
| 84 | + */ |
| 85 | + protected function initConsumer(InputInterface $input) |
| 86 | + { |
| 87 | + $this->consumer = $this->getContainer() |
| 88 | + ->get(sprintf($this->getConsumerService(), $input->getArgument('name'))); |
| 89 | + |
| 90 | + if (null !== $input->getOption('memory-limit') && |
| 91 | + ctype_digit((string) $input->getOption('memory-limit')) && |
| 92 | + $input->getOption('memory-limit') > 0 |
| 93 | + ) { |
| 94 | + $this->consumer->setMemoryLimit($input->getOption('memory-limit')); |
| 95 | + } |
| 96 | + $this->consumer->setRoutingKey($input->getOption('route')); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @return string |
| 101 | + */ |
| 102 | + protected function getConsumerService() |
| 103 | + { |
| 104 | + return 'old_sound_rabbit_mq.%s_batch'; |
| 105 | + } |
| 106 | +} |
0 commit comments