|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OldSound\RabbitMqBundle\Command; |
|
4
|
|
|
|
|
5
|
|
|
use OldSound\RabbitMqBundle\RabbitMq\BaseConsumer as Consumer; |
|
6
|
|
|
use PhpAmqpLib\Exception\AMQPTimeoutException; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
11
|
|
|
|
|
12
|
|
|
abstract class BaseConsumerCommand extends BaseRabbitMqCommand |
|
13
|
|
|
{ |
|
14
|
|
|
protected $consumer; |
|
15
|
|
|
|
|
16
|
|
|
/** @var int */ |
|
17
|
|
|
protected $amount; |
|
18
|
|
|
|
|
19
|
|
|
abstract protected function getConsumerService(); |
|
20
|
|
|
|
|
21
|
|
|
public function stopConsumer() |
|
22
|
|
|
{ |
|
23
|
|
|
if ($this->consumer instanceof Consumer) { |
|
24
|
|
|
// Process current message, then halt consumer |
|
25
|
|
|
$this->consumer->forceStopConsumer(); |
|
26
|
|
|
|
|
27
|
|
|
// Halt consumer if waiting for a new message from the queue |
|
28
|
|
|
try { |
|
29
|
|
|
$this->consumer->stopConsuming(); |
|
30
|
|
|
} catch (AMQPTimeoutException $e) { |
|
|
|
|
|
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function restartConsumer() |
|
36
|
|
|
{ |
|
37
|
|
|
// TODO: Implement restarting of consumer |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
3 |
|
protected function configure(): void |
|
41
|
|
|
{ |
|
42
|
3 |
|
parent::configure(); |
|
43
|
|
|
|
|
44
|
|
|
$this |
|
45
|
3 |
|
->addArgument('name', InputArgument::REQUIRED, 'Consumer Name') |
|
46
|
3 |
|
->addOption('messages', 'm', InputOption::VALUE_OPTIONAL, 'Messages to consume', '0') |
|
47
|
3 |
|
->addOption('route', 'r', InputOption::VALUE_OPTIONAL, 'Routing Key', '') |
|
48
|
3 |
|
->addOption('memory-limit', 'l', InputOption::VALUE_OPTIONAL, 'Allowed memory for this process (MB)') |
|
49
|
3 |
|
->addOption('debug', 'd', InputOption::VALUE_NONE, 'Enable Debugging') |
|
50
|
3 |
|
->addOption('without-signals', 'w', InputOption::VALUE_NONE, 'Disable catching of system signals') |
|
51
|
|
|
; |
|
52
|
3 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
protected function initialize(InputInterface $input, OutputInterface $output): void |
|
55
|
|
|
{ |
|
56
|
|
|
$this->amount = (int)$input->getOption('messages'); |
|
57
|
|
|
if (0 > $this->amount) { |
|
58
|
|
|
throw new \InvalidArgumentException("The -m option should be null or greater than 0"); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Executes the current command. |
|
64
|
|
|
* |
|
65
|
|
|
* @param InputInterface $input An InputInterface instance |
|
66
|
|
|
* @param OutputInterface $output An OutputInterface instance |
|
67
|
|
|
* |
|
68
|
|
|
* @return integer 0 if everything went fine, or an error code |
|
69
|
|
|
* |
|
70
|
|
|
* @throws \InvalidArgumentException When the number of messages to consume is less than 0 |
|
71
|
|
|
* @throws \BadFunctionCallException When the pcntl is not installed and option -s is true |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
|
74
|
|
|
{ |
|
75
|
|
|
if (defined('AMQP_WITHOUT_SIGNALS') === false) { |
|
76
|
|
|
define('AMQP_WITHOUT_SIGNALS', $input->getOption('without-signals')); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
if (!AMQP_WITHOUT_SIGNALS && extension_loaded('pcntl')) { |
|
80
|
|
|
if (!function_exists('pcntl_signal')) { |
|
81
|
|
|
throw new \BadFunctionCallException("Function 'pcntl_signal' is referenced in the php.ini 'disable_functions' and can't be called."); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
pcntl_signal(SIGTERM, [&$this, 'stopConsumer']); |
|
85
|
|
|
pcntl_signal(SIGINT, [&$this, 'stopConsumer']); |
|
86
|
|
|
pcntl_signal(SIGQUIT, [&$this, 'stopConsumer']); |
|
87
|
|
|
pcntl_signal(SIGHUP, [&$this, 'restartConsumer']); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
if (defined('AMQP_DEBUG') === false) { |
|
91
|
|
|
define('AMQP_DEBUG', (bool) $input->getOption('debug')); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$this->initConsumer($input); |
|
95
|
|
|
|
|
96
|
|
|
return $this->consumer->consume($this->amount); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
protected function initConsumer(InputInterface $input) |
|
100
|
|
|
{ |
|
101
|
|
|
$this->consumer = $this->getContainer() |
|
102
|
|
|
->get(sprintf($this->getConsumerService(), $input->getArgument('name'))); |
|
103
|
|
|
|
|
104
|
|
|
if ($input->hasOption('memory-limit')) { |
|
105
|
|
|
$memoryLimit = (int)$input->getOption('memory-limit'); |
|
106
|
|
|
if ($memoryLimit > 0) { |
|
107
|
|
|
$this->consumer->setMemoryLimit($memoryLimit); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
$this->consumer->setRoutingKey($input->getOption('route')); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|