forked from php-amqplib/RabbitMqBundle
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathBaseConsumerCommand.php
More file actions
118 lines (97 loc) · 4.3 KB
/
BaseConsumerCommand.php
File metadata and controls
118 lines (97 loc) · 4.3 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
namespace OldSound\RabbitMqBundle\Command;
use OldSound\RabbitMqBundle\RabbitMq\AnonConsumer;
use OldSound\RabbitMqBundle\RabbitMq\BaseConsumer;
use OldSound\RabbitMqBundle\RabbitMq\Consumer;
use OldSound\RabbitMqBundle\RabbitMq\DynamicConsumer;
use OldSound\RabbitMqBundle\RabbitMq\MultipleConsumer;
use PhpAmqpLib\Exception\AMQPTimeoutException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
abstract class BaseConsumerCommand extends BaseRabbitMqCommand
{
/** @var DynamicConsumer|MultipleConsumer|AnonConsumer */
protected $consumer;
/** @var string */
protected $amount;
/**
* @return mixed
*/
abstract protected function getConsumerService();
public function stopConsumer(): void
{
if ($this->consumer instanceof BaseConsumer) {
// Process current message, then halt consumer
$this->consumer->forceStopConsumer();
// Halt consumer if waiting for a new message from the queue
try {
$this->consumer->stopConsuming();
} catch (AMQPTimeoutException $e) {}
}
}
public function restartConsumer():void
{
if ($this->consumer instanceof BaseConsumer) {
// Process current message, then halt consumer
$this->consumer->forceStopConsumer();
// Halt consumer if waiting for a new message from the queue
try {
$this->consumer->start();
} catch (\ErrorException $e) {
}
}
}
protected function configure(): void
{
parent::configure();
$this
->addArgument('name', InputArgument::REQUIRED, 'Consumer Name')
->addOption('messages', 'm', InputOption::VALUE_OPTIONAL, 'Messages to consume', 0)
->addOption('route', 'r', InputOption::VALUE_OPTIONAL, 'Routing Key', '')
->addOption('memory-limit', 'l', InputOption::VALUE_OPTIONAL, 'Allowed memory for this process (MB)')
->addOption('debug', 'd', InputOption::VALUE_NONE, 'Enable Debugging')
->addOption('without-signals', 'w', InputOption::VALUE_NONE, 'Disable catching of system signals')
;
}
/**
* Executes the current command.
*
* @throws \InvalidArgumentException When the number of messages to consume is less than 0
* @throws \BadFunctionCallException When the pcntl is not installed and option -s is true
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
if (defined('AMQP_WITHOUT_SIGNALS') === false) {
define('AMQP_WITHOUT_SIGNALS', $input->getOption('without-signals'));
}
if (!AMQP_WITHOUT_SIGNALS && extension_loaded('pcntl')) {
if (!function_exists('pcntl_signal')) {
throw new \BadFunctionCallException("Function 'pcntl_signal' is referenced in the php.ini 'disable_functions' and can't be called.");
}
pcntl_signal(SIGTERM, array(&$this, 'stopConsumer'));
pcntl_signal(SIGINT, array(&$this, 'stopConsumer'));
pcntl_signal(SIGHUP, array(&$this, 'restartConsumer'));
}
if (defined('AMQP_DEBUG') === false) {
define('AMQP_DEBUG', (bool) $input->getOption('debug'));
}
$this->amount = $input->getOption('messages');
if (0 > (int) $this->amount) {
throw new \InvalidArgumentException("The -m option should be null or greater than 0");
}
$this->initConsumer($input);
return $this->consumer->consume((int)$this->amount);
}
protected function initConsumer(InputInterface $input): void
{
$this->consumer = $this->getContainer()
->get(sprintf($this->getConsumerService(), $input->getArgument('name')));
if (!is_null($input->getOption('memory-limit')) && ctype_digit((string) $input->getOption('memory-limit')) && (int)$input->getOption('memory-limit') > 0) {
$this->consumer->setMemoryLimit($input->getOption('memory-limit'));
}
$this->consumer->setRoutingKey($input->getOption('route'));
$this->consumer->setContext($input->getArgument('context'));
}
}