-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathConsumeCommand.php
More file actions
120 lines (103 loc) · 3.84 KB
/
ConsumeCommand.php
File metadata and controls
120 lines (103 loc) · 3.84 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
<?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;
/**
* Class ConsumeCommand.
*/
class ConsumeCommand extends ContainerAwareCommand
{
const OPTION_MAX_MESSAGES = 'killAfter';
const OPTION_MAX_MESSAGES_DEFAULT_VALUE = -1; // -1 = No limit
const OPTION_MAX_TIME = 'killAfterTime';
const OPTION_MAX_TIME_DEFAULT_VALUE = 0; // 0 = No limit
/** @var EndpointInterface */
protected $endpoint;
/** @var InputInterface */
protected $input;
/**
* @return \Smartbox\Integration\FrameworkBundle\Core\Endpoints\EndpointInterface
*/
protected function getSourceEndpoint()
{
$uri = $this->getInput()->getArgument('uri');
return $this->getContainer()->get('smartesb.endpoint_factory')->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.
Ex:
Consume all the messages, and never die:
app/console smartesb:consumer:start queue://api --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
);
$this->addOption(
self::OPTION_MAX_TIME,
't',
InputOption::VALUE_REQUIRED,
'How long before the worker is killed? 0 for never, default value is '.self::OPTION_MAX_TIME_DEFAULT_VALUE.'.',
self::OPTION_MAX_TIME_DEFAULT_VALUE
);
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->input = $input;
$producer = null;
$this->endpoint = $this->getSourceEndpoint();
$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';
}
if ($input->getOption(self::OPTION_MAX_TIME) > 0) {
$message .= ' limited to '.$input->getOption(self::OPTION_MAX_TIME).' seconds';
}
$message .= '.</info>';
$output->writeln($message);
pcntl_signal(SIGINT, [$this, 'handleSignal']);
pcntl_signal(SIGTERM, [$this, 'handleSignal']);
$this->endpoint->consume($input->getOption(self::OPTION_MAX_MESSAGES), $input->getOption(self::OPTION_MAX_TIME));
$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();
}
}