-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathConsumerCommand.php
97 lines (83 loc) · 3.12 KB
/
ConsumerCommand.php
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
<?php
declare(strict_types=1);
namespace StompPhp\StompBundle\Command;
use StompPhp\StompBundle\DependencyInjection\StompPhpStompExtension;
use StompPhp\StompBundle\Stomp\Subscription;
use Symfony\Component\Console\Command\Command;
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\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ConsumerCommand extends Command implements ContainerAwareInterface
{
/**
* @var ContainerInterface
*/
private $container;
/**
* @var Subscription
*/
private $subscription;
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
public function run(InputInterface $input, OutputInterface $output)
{
$maxMessages = $input->getOption('messages');
$messages = 0;
$this->subscription = $this->getSubscription($input->getArgument('name'));
$signalHandler = $this->registerSignalHandler();
foreach ($this->subscription->consume() as $unit) {
if (null !== $unit) {
++$messages;
if ($output->isVerbose()) {
$output->writeln(sprintf('Message #%s %s', $messages, ($unit ? 'processed' : 'skipped')));
}
if ($maxMessages && $messages == $maxMessages) {
$this->subscription->stop();
}
} elseif ($output->isVeryVerbose()) {
$output->writeln('No message available');
}
if ($signalHandler) {
pcntl_signal_dispatch();
}
}
return 0;
}
private function getSubscription(string $name): Subscription
{
$id = sprintf(StompPhpStompExtension::CONSUMER_ID, $name);
$subscription = $this->container->get($id);
if (!is_a($subscription, Subscription::class)) {
throw new \LogicException(sprintf('The service "%s" is not a "%s".', $id, Subscription::class));
}
return $subscription;
}
private function registerSignalHandler(): bool
{
if (extension_loaded('pcntl') && function_exists('pcntl_signal')) {
pcntl_signal(SIGTERM, [$this, 'stopSubscription']);
pcntl_signal(SIGINT, [$this, 'stopSubscription']);
return true;
}
return false;
}
protected function configure()
{
$this->setName('stomp:consumer')
->setDescription('Start a process for the given consumer.')
->addArgument('name', InputArgument::REQUIRED, 'Consumer Name')
->addOption('messages', 'm', InputOption::VALUE_OPTIONAL, 'Amount of messages to consume.', 0)
->addOption('selector', 's', InputOption::VALUE_OPTIONAL, 'Selector (override)', null);
}
private function stopSubscription(): void
{
if ($this->subscription) {
$this->subscription->stop();
}
}
}