1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Antidot\Queue\Cli; |
6
|
|
|
|
7
|
|
|
use Enqueue\Consumption\QueueConsumerInterface; |
8
|
|
|
use Enqueue\Consumption\Result; |
9
|
|
|
use Interop\Queue\Context; |
10
|
|
|
use Interop\Queue\Message; |
11
|
|
|
use Interop\Queue\Processor; |
12
|
|
|
use InvalidArgumentException; |
13
|
|
|
use Symfony\Component\Console\Command\Command; |
14
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
17
|
|
|
|
18
|
|
|
class StartQueueConsumer extends Command |
19
|
|
|
{ |
20
|
|
|
public const NAME = 'queue:start'; |
21
|
|
|
private QueueConsumerInterface $consumer; |
22
|
|
|
private Processor $processor; |
23
|
|
|
/** @var Context */ |
24
|
|
|
private Context $context; |
25
|
|
|
|
26
|
2 |
|
public function __construct(QueueConsumerInterface $consumer, Processor $messageProcessor, Context $context) |
27
|
|
|
{ |
28
|
2 |
|
$this->consumer = $consumer; |
29
|
2 |
|
$this->processor = $messageProcessor; |
30
|
2 |
|
$this->context = $context; |
31
|
2 |
|
parent::__construct(); |
32
|
2 |
|
} |
33
|
|
|
|
34
|
2 |
|
protected function configure(): void |
35
|
|
|
{ |
36
|
2 |
|
$this->setName(self::NAME) |
37
|
2 |
|
->setDescription('Start listening to the given queue name.') |
38
|
2 |
|
->addArgument( |
39
|
2 |
|
'queue_name', |
40
|
2 |
|
InputArgument::REQUIRED, |
41
|
2 |
|
'The queue name we want to consume' |
42
|
|
|
); |
43
|
2 |
|
} |
44
|
|
|
|
45
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
46
|
|
|
{ |
47
|
|
|
$queue = $input->getArgument('queue_name'); |
48
|
|
|
if (false === is_string($queue)) { |
49
|
|
|
throw new InvalidArgumentException('Argument "queue_name" must be of type string.'); |
50
|
|
|
} |
51
|
|
|
$this->consumer->bindCallback( |
52
|
|
|
$queue, |
|
|
|
|
53
|
|
|
fn(Message $message) => $this->processor->process($message, $this->context) |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
$this->consumer->consume(); |
57
|
|
|
|
58
|
|
|
return 0; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|