-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathConsumeCommand.php
More file actions
129 lines (108 loc) · 3.86 KB
/
ConsumeCommand.php
File metadata and controls
129 lines (108 loc) · 3.86 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
119
120
121
122
123
124
125
126
127
128
129
<?php
namespace Smartbox\Integration\FrameworkBundle\Command;
use Smartbox\Integration\FrameworkBundle\Core\Endpoints\EndpointFactory;
use Smartbox\Integration\FrameworkBundle\Core\Endpoints\EndpointInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Logger\ConsoleLogger;
/**
* Class ConsumeCommand.
*/
class ConsumeCommand extends ContainerAwareCommand
{
const OPTION_MAX_MESSAGES = 'killAfter';
const OPTION_MAX_MESSAGES_DEFAULT_VALUE = -1; // -1 = No limit
/** @var EndpointInterface */
protected $endpoint;
/** @var InputInterface */
protected $input;
/** @var EndpointFactory */
protected $endpointFactory;
/**
* ConsumeCommand constructor.
*
* @param EndpointFactory $endpointFactory
*/
public function __construct(EndpointFactory $endpointFactory)
{
parent::__construct();
$this->endpointFactory = $endpointFactory;
}
/**
* @return mixed|\Smartbox\Integration\FrameworkBundle\Core\Endpoints\Endpoint
*/
protected function getSourceEndpoint()
{
$uri = $this->getInput()->getArgument('uri');
return $this->endpointFactory->createEndpoint($uri, EndpointFactory::MODE_CONSUME);
}
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('smartesb:consumer:start')
->setDescription('Start a daemon consuming messages from a given URI')
->setHelp('Run the consumer. You can kill the consumer after x messages by using the --killAfter option. Use the -vv option to display extra information.
Ex:
Consume all the messages, never die and display an alert each time a message is consumed:
app/console smartesb:consumer:start queue://api -vv --killAfter -1
Consume the events and die after 10 messages:
app/console smartesb:consumer:start queue://events --killAfter 10
')
;
$this->addArgument(
'uri',
InputArgument::REQUIRED,
'Source URI ( e.g.: queue://api/* )'
);
$this->addOption(
self::OPTION_MAX_MESSAGES,
'k',
InputOption::VALUE_REQUIRED,
'How many messages should be processed before the worker is killed? -1 for never, default value is '.self::OPTION_MAX_MESSAGES_DEFAULT_VALUE.'.',
self::OPTION_MAX_MESSAGES_DEFAULT_VALUE
);
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->input = $input;
$this->endpoint = $this->getSourceEndpoint();
$consumer = $this->endpoint->getConsumer();
if (method_exists($consumer, 'setLogger')) {
$logger = new ConsoleLogger($output);
$consumer->setLogger($logger);
}
$message = '<info>Consuming from '.$this->endpoint->getURI();
if ($input->getOption(self::OPTION_MAX_MESSAGES) > 0) {
$message .= ' limited to '.$input->getOption(self::OPTION_MAX_MESSAGES).' messages';
}
$message .= '.</info>';
$output->writeln($message);
pcntl_signal(SIGINT, [$this, 'handleSignal']);
pcntl_signal(SIGTERM, [$this, 'handleSignal']);
$this->endpoint->consume($input->getOption(self::OPTION_MAX_MESSAGES));
$output->writeln('<info>Consumer was gracefully stopped for: '.$this->endpoint->getURI().'</info>');
}
/**
* @return InputInterface
*/
protected function getInput()
{
return $this->input;
}
/**
* Handles a signal.
*/
public function handleSignal()
{
$this->endpoint->getConsumer()->stop();
}
}