|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Portiny\RabbitMQ\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Bunny\Channel; |
|
6
|
|
|
use Portiny\RabbitMQ\BunnyManager; |
|
7
|
|
|
use Portiny\RabbitMQ\Consumer\AbstractConsumer; |
|
8
|
|
|
use Symfony\Component\Console\Command\Command; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
12
|
|
|
|
|
13
|
|
|
final class ConsumeCommand extends Command |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var BunnyManager |
|
18
|
|
|
*/ |
|
19
|
|
|
private $bunnyManager; |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
public function __construct(BunnyManager $bunnyManager) |
|
23
|
|
|
{ |
|
24
|
|
|
parent::__construct(); |
|
25
|
|
|
|
|
26
|
|
|
$this->bunnyManager = $bunnyManager; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* {@inheritdoc} |
|
32
|
|
|
*/ |
|
33
|
|
|
protected function configure(): void |
|
34
|
|
|
{ |
|
35
|
|
|
$this->setName('rabbitmq:consume') |
|
36
|
|
|
->setDescription('Run a RabbitMQ consumer') |
|
37
|
|
|
->addArgument('consumer', InputArgument::REQUIRED, 'FQDN or alias of the consumer') |
|
38
|
|
|
->addOption('messages', 'm', InputArgument::OPTIONAL, 'Amount of messages to consume') |
|
39
|
|
|
->addOption('time', 't', InputArgument::OPTIONAL, 'Max seconds for consumer to run'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritdoc} |
|
45
|
|
|
*/ |
|
46
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
|
47
|
|
|
{ |
|
48
|
|
|
/** @var string $consumerName */ |
|
49
|
|
|
$consumerName = $input->getArgument('consumer'); |
|
50
|
|
|
$numberOfMessages = $this->getNumberOfMessages($input); |
|
51
|
|
|
$secondsToRun = $this->getSecondsToRun($input); |
|
52
|
|
|
|
|
53
|
|
|
$output->writeln( |
|
54
|
|
|
sprintf( |
|
55
|
|
|
'<comment>[%s]</comment> <info>Starting consumer "%s"...</info>', |
|
56
|
|
|
date('Y-m-d H:i:s'), |
|
57
|
|
|
$consumerName |
|
58
|
|
|
) |
|
59
|
|
|
); |
|
60
|
|
|
|
|
61
|
|
|
$consumer = $this->bunnyManager->getConsumerByAlias($consumerName); |
|
62
|
|
|
if ($consumer === null) { |
|
63
|
|
|
$output->writeln('<error>Consumer not found!</error>'); |
|
64
|
|
|
return -1; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** @var Channel $channel */ |
|
68
|
|
|
$channel = $this->bunnyManager->getChannel(); |
|
69
|
|
|
|
|
70
|
|
|
$output->writeln('<info>Consuming...</info>'); |
|
71
|
|
|
|
|
72
|
|
|
/** @var AbstractConsumer $consumer */ |
|
73
|
|
|
$consumer->consume($channel, $numberOfMessages); |
|
74
|
|
|
|
|
75
|
|
|
$this->bunnyManager->getClient()->run($secondsToRun); |
|
76
|
|
|
|
|
77
|
|
|
return 0; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
protected function getNumberOfMessages(InputInterface $input): ?int |
|
82
|
|
|
{ |
|
83
|
|
|
/** @var string|int $messages */ |
|
84
|
|
|
$messages = $input->getOption('messages'); |
|
85
|
|
|
|
|
86
|
|
|
return $messages ? (int) $messages : null; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
protected function getSecondsToRun(InputInterface $input): ?int |
|
91
|
|
|
{ |
|
92
|
|
|
/** @var string|int $seconds */ |
|
93
|
|
|
$seconds = $input->getOption('time'); |
|
94
|
|
|
|
|
95
|
|
|
return $seconds ? (int) $seconds : null; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|