|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Enqueue\Symfony\Consumption; |
| 4 | + |
| 5 | +use Enqueue\Consumption\ChainExtension; |
| 6 | +use Enqueue\Consumption\Extension\LoggerExtension; |
| 7 | +use Enqueue\Consumption\QueueConsumerRegistryInterface; |
| 8 | +use Enqueue\Consumption\QueueSubscriberInterface; |
| 9 | +use Enqueue\ProcessorRegistryInterface; |
| 10 | +use Symfony\Component\Console\Command\Command; |
| 11 | +use Symfony\Component\Console\Input\InputArgument; |
| 12 | +use Symfony\Component\Console\Input\InputInterface; |
| 13 | +use Symfony\Component\Console\Input\InputOption; |
| 14 | +use Symfony\Component\Console\Logger\ConsoleLogger; |
| 15 | +use Symfony\Component\Console\Output\OutputInterface; |
| 16 | + |
| 17 | +class ConfigurableConsumeCommand extends Command |
| 18 | +{ |
| 19 | + use LimitsExtensionsCommandTrait; |
| 20 | + use QueueConsumerOptionsCommandTrait; |
| 21 | + |
| 22 | + protected static $defaultName = 'enqueue:transport:consume'; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var QueueConsumerRegistryInterface |
| 26 | + */ |
| 27 | + protected $consumerRegistry; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var ProcessorRegistryInterface |
| 31 | + */ |
| 32 | + private $processorRegistry; |
| 33 | + |
| 34 | + public function __construct(QueueConsumerRegistryInterface $consumerRegistry, ProcessorRegistryInterface $processorRegistry) |
| 35 | + { |
| 36 | + parent::__construct(static::$defaultName); |
| 37 | + |
| 38 | + $this->consumerRegistry = $consumerRegistry; |
| 39 | + $this->processorRegistry = $processorRegistry; |
| 40 | + } |
| 41 | + |
| 42 | + protected function configure(): void |
| 43 | + { |
| 44 | + $this->configureLimitsExtensions(); |
| 45 | + $this->configureQueueConsumerOptions(); |
| 46 | + |
| 47 | + $this |
| 48 | + ->setDescription('A worker that consumes message from a broker. '. |
| 49 | + 'To use this broker you have to explicitly set a queue to consume from '. |
| 50 | + 'and a message processor service') |
| 51 | + ->addArgument('processor', InputArgument::REQUIRED, 'A message processor.') |
| 52 | + ->addArgument('queues', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'A queue to consume from', []) |
| 53 | + ->addOption('transport', 't', InputOption::VALUE_OPTIONAL, 'The transport to consume messages from.', 'default') |
| 54 | + ; |
| 55 | + } |
| 56 | + |
| 57 | + protected function execute(InputInterface $input, OutputInterface $output): ?int |
| 58 | + { |
| 59 | + $consumer = $this->consumerRegistry->get($input->getOption('transport')); |
| 60 | + |
| 61 | + $this->setQueueConsumerOptions($consumer, $input); |
| 62 | + |
| 63 | + $processor = $this->processorRegistry->get($input->getArgument('processor')); |
| 64 | + |
| 65 | + $queues = $input->getArgument('queues'); |
| 66 | + if (empty($queues) && $processor instanceof QueueSubscriberInterface) { |
| 67 | + $queues = $processor::getSubscribedQueues(); |
| 68 | + } |
| 69 | + |
| 70 | + if (empty($queues)) { |
| 71 | + throw new \LogicException(sprintf( |
| 72 | + 'The queue is not provided. The processor must implement "%s" interface and it must return not empty array of queues or a queue set using as a second argument.', |
| 73 | + QueueSubscriberInterface::class |
| 74 | + )); |
| 75 | + } |
| 76 | + |
| 77 | + $extensions = $this->getLimitsExtensions($input, $output); |
| 78 | + array_unshift($extensions, new LoggerExtension(new ConsoleLogger($output))); |
| 79 | + |
| 80 | + $runtimeExtensions = new ChainExtension($extensions); |
| 81 | + |
| 82 | + foreach ($queues as $queue) { |
| 83 | + $consumer->bind($queue, $processor); |
| 84 | + } |
| 85 | + |
| 86 | + $consumer->consume($runtimeExtensions); |
| 87 | + |
| 88 | + return null; |
| 89 | + } |
| 90 | +} |
0 commit comments