|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Markup\JobQueueBundle\Command; |
| 4 | + |
| 5 | +use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface; |
| 6 | +use PhpAmqpLib\Message\AMQPMessage; |
| 7 | +use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
| 8 | +use Symfony\Component\Console\Input\InputArgument; |
| 9 | +use Symfony\Component\Console\Input\InputInterface; |
| 10 | +use Symfony\Component\Console\Input\InputOption; |
| 11 | +use Symfony\Component\Console\Output\OutputInterface; |
| 12 | + |
| 13 | +/** |
| 14 | + * Consumes messages from the Go consumer |
| 15 | + */ |
| 16 | +class RabbitMqConsumerCommand extends ContainerAwareCommand |
| 17 | +{ |
| 18 | + const STRICT_CODE_ACK = 0; |
| 19 | + const STRICT_CODE_REJECT = 3; |
| 20 | + const STRICT_CODE_REJECT_REQUEUE = 4; |
| 21 | + const STRICT_CODE_NEG_ACK = 5; |
| 22 | + const STRICT_CODE_NEG_ACK_REQUEUE = 6; |
| 23 | + |
| 24 | + protected function configure() |
| 25 | + { |
| 26 | + $this |
| 27 | + ->addArgument('event', InputArgument::REQUIRED) |
| 28 | + ->setName('markup:job_queue:rabbitmq_consumer') |
| 29 | + ->addOption( |
| 30 | + 'strict-exit-code', |
| 31 | + null, |
| 32 | + InputOption::VALUE_NONE, |
| 33 | + 'If strict_exit_code is chosen then this command will return the following exit codes. 0=ACK, 3=REJECT, 4=REJECT & REQUEUE, 5=NEG ACK, 6=NEG ACK & REQUEUE' |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * {inheritDoc} |
| 39 | + */ |
| 40 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 41 | + { |
| 42 | + $data = json_decode(base64_decode($input->getArgument('event')), true); |
| 43 | + |
| 44 | + $strict = $input->getOption('strict-exit-code'); |
| 45 | + |
| 46 | + /** @var ConsumerInterface $consumer */ |
| 47 | + $consumer = $this->getContainer()->get('simple_bus.rabbit_mq_bundle_bridge.commands_consumer'); |
| 48 | + |
| 49 | + if (!$consumer instanceof ConsumerInterface) { |
| 50 | + return 0; |
| 51 | + } |
| 52 | + |
| 53 | + $consumerReturn = $consumer->execute(new AMQPMessage($data['body'], $data['properties'])); |
| 54 | + |
| 55 | + // if not running in strict mode - always acknowledge the message otherwise it will requeue forever |
| 56 | + if (!$strict) { |
| 57 | + exit(self::STRICT_CODE_ACK); |
| 58 | + } |
| 59 | + |
| 60 | + // If in strict mode then test the return value from the consumer and return an appropriate code |
| 61 | + if ($consumerReturn === ConsumerInterface::MSG_REJECT) { |
| 62 | + exit(self::STRICT_CODE_REJECT); |
| 63 | + } |
| 64 | + |
| 65 | + exit(self::STRICT_CODE_ACK); |
| 66 | + } |
| 67 | +} |
0 commit comments