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
|
|
|
public function restartConsumer() |
35
|
|
|
{ |
36
|
|
|
// TODO: Implement restarting of consumer |
37
|
|
|
} |
38
|
|
|
|
39
|
3 |
|
protected function configure() |
40
|
|
|
{ |
41
|
3 |
|
parent::configure(); |
42
|
|
|
|
43
|
|
|
$this |
44
|
3 |
|
->addArgument('name', InputArgument::REQUIRED, 'Consumer Name') |
45
|
3 |
|
->addOption('messages', 'm', InputOption::VALUE_OPTIONAL, 'Messages to consume', '0') |
46
|
3 |
|
->addOption('route', 'r', InputOption::VALUE_OPTIONAL, 'Routing Key', '') |
47
|
3 |
|
->addOption('memory-limit', 'l', InputOption::VALUE_OPTIONAL, 'Allowed memory for this process (MB)', null) |
48
|
3 |
|
->addOption('debug', 'd', InputOption::VALUE_NONE, 'Enable Debugging') |
49
|
3 |
|
->addOption('without-signals', 'w', InputOption::VALUE_NONE, 'Disable catching of system signals') |
50
|
|
|
; |
51
|
3 |
|
} |
52
|
|
|
|
53
|
|
|
protected function initialize(InputInterface $input, OutputInterface $output) |
54
|
|
|
{ |
55
|
|
|
$this->amount = (int)$input->getOption('messages'); |
56
|
|
|
if (0 > $this->amount) { |
57
|
|
|
throw new \InvalidArgumentException("The -m option should be null or greater than 0"); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Executes the current command. |
63
|
|
|
* |
64
|
|
|
* @param InputInterface $input An InputInterface instance |
65
|
|
|
* @param OutputInterface $output An OutputInterface instance |
66
|
|
|
* |
67
|
|
|
* @return integer 0 if everything went fine, or an error code |
68
|
|
|
* |
69
|
|
|
* @throws \InvalidArgumentException When the number of messages to consume is less than 0 |
70
|
|
|
* @throws \BadFunctionCallException When the pcntl is not installed and option -s is true |
71
|
|
|
*/ |
72
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
73
|
|
|
{ |
74
|
|
|
if (defined('AMQP_WITHOUT_SIGNALS') === false) { |
75
|
|
|
define('AMQP_WITHOUT_SIGNALS', $input->getOption('without-signals')); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if (!AMQP_WITHOUT_SIGNALS && extension_loaded('pcntl')) { |
79
|
|
|
if (!function_exists('pcntl_signal')) { |
80
|
|
|
throw new \BadFunctionCallException("Function 'pcntl_signal' is referenced in the php.ini 'disable_functions' and can't be called."); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
pcntl_signal(SIGTERM, array(&$this, 'stopConsumer')); |
84
|
|
|
pcntl_signal(SIGINT, array(&$this, 'stopConsumer')); |
85
|
|
|
pcntl_signal(SIGHUP, array(&$this, 'restartConsumer')); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if (defined('AMQP_DEBUG') === false) { |
89
|
|
|
define('AMQP_DEBUG', (bool) $input->getOption('debug')); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$this->initConsumer($input); |
93
|
|
|
|
94
|
|
|
return $this->consumer->consume($this->amount); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
protected function initConsumer(InputInterface $input) |
98
|
|
|
{ |
99
|
|
|
$this->consumer = $this->getContainer() |
100
|
|
|
->get(sprintf($this->getConsumerService(), $input->getArgument('name'))); |
|
|
|
|
101
|
|
|
|
102
|
|
|
if ($input->hasOption('memory-limit')) { |
103
|
|
|
$memoryLimit = (int)$input->getOption('memory-limit'); |
104
|
|
|
if ($memoryLimit > 0) { |
105
|
|
|
$this->consumer->setMemoryLimit($memoryLimit); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
$this->consumer->setRoutingKey($input->getOption('route')); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|