-
Notifications
You must be signed in to change notification settings - Fork 440
/
Copy pathConsumeCommand.php
91 lines (72 loc) · 2.92 KB
/
ConsumeCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
namespace Enqueue\Symfony\Consumption;
use Enqueue\Consumption\ChainExtension;
use Enqueue\Consumption\Extension\ExitStatusExtension;
use Enqueue\Consumption\QueueConsumerInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
#[AsCommand('enqueue:transport:consume')]
class ConsumeCommand extends Command
{
use ChooseLoggerCommandTrait;
use LimitsExtensionsCommandTrait;
use QueueConsumerOptionsCommandTrait;
/**
* @var ContainerInterface
*/
private $container;
/**
* @var string
*/
private $defaultTransport;
/**
* @var string
*/
private $queueConsumerIdPattern;
public function __construct(ContainerInterface $container, string $defaultTransport, string $queueConsumerIdPattern = 'enqueue.transport.%s.queue_consumer')
{
$this->container = $container;
$this->defaultTransport = $defaultTransport;
$this->queueConsumerIdPattern = $queueConsumerIdPattern;
parent::__construct();
}
protected function configure(): void
{
$this->configureLimitsExtensions();
$this->configureQueueConsumerOptions();
$this->configureLoggerExtension();
$this
->addOption('transport', 't', InputOption::VALUE_OPTIONAL, 'The transport to consume messages from.', $this->defaultTransport)
->setDescription('A worker that consumes message from a broker. '.
'To use this broker you have to configure queue consumer before adding to the command')
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$transport = $input->getOption('transport');
try {
// QueueConsumer must be pre configured outside of the command!
$consumer = $this->getQueueConsumer($transport);
} catch (NotFoundExceptionInterface $e) {
throw new \LogicException(sprintf('Transport "%s" is not supported.', $transport), 0, $e);
}
$this->setQueueConsumerOptions($consumer, $input);
$extensions = $this->getLimitsExtensions($input, $output);
if ($loggerExtension = $this->getLoggerExtension($input, $output)) {
array_unshift($extensions, $loggerExtension);
}
$exitStatusExtension = new ExitStatusExtension();
array_unshift($extensions, $exitStatusExtension);
$consumer->consume(new ChainExtension($extensions));
return $exitStatusExtension->getExitStatus() ?? 0;
}
private function getQueueConsumer(string $name): QueueConsumerInterface
{
return $this->container->get(sprintf($this->queueConsumerIdPattern, $name));
}
}