-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathGenerateSupervisorRabbitMqConsumerFilesCommand.php
More file actions
91 lines (76 loc) · 2.8 KB
/
GenerateSupervisorRabbitMqConsumerFilesCommand.php
File metadata and controls
91 lines (76 loc) · 2.8 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
<?php
declare(strict_types=1);
namespace CodelyTv\Apps\Mooc\Backend\Command\DomainEvents\RabbitMq;
use CodelyTv\Shared\Domain\Bus\Event\DomainEventSubscriber;
use CodelyTv\Shared\Infrastructure\Bus\Event\DomainEventSubscriberLocator;
use CodelyTv\Shared\Infrastructure\Bus\Event\RabbitMq\RabbitMqQueueNameFormatter;
use Override;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use function Lambdish\Phunctional\each;
#[AsCommand(
name: 'codely:domain-events:rabbitmq:generate-supervisor-files',
description: 'Generate the supervisor configuration for every RabbitMQ subscriber',
)]
final class GenerateSupervisorRabbitMqConsumerFilesCommand extends Command
{
private const int EVENTS_TO_PROCESS_AT_TIME = 200;
private const int NUMBERS_OF_PROCESSES_PER_SUBSCRIBER = 1;
private const SUPERVISOR_PATH = __DIR__ . '/../../../../build/supervisor';
public function __construct(private readonly DomainEventSubscriberLocator $locator)
{
parent::__construct();
}
#[Override]
protected function configure(): void
{
$this->addArgument('command-path', InputArgument::OPTIONAL, 'Path on this is gonna be deployed', '/var/www');
}
#[Override]
protected function execute(InputInterface $input, OutputInterface $output): int
{
$path = $input->getArgument('command-path');
each($this->configCreator($path), $this->locator->all());
return 0;
}
private function configCreator(string $path): callable
{
return function (DomainEventSubscriber $subscriber) use ($path): void {
$queueName = RabbitMqQueueNameFormatter::format($subscriber);
$subscriberName = RabbitMqQueueNameFormatter::shortFormat($subscriber);
$fileContent = str_replace(
['{subscriber_name}', '{queue_name}', '{path}', '{processes}', '{events_to_process}', ],
[
$subscriberName,
$queueName,
$path,
self::NUMBERS_OF_PROCESSES_PER_SUBSCRIBER,
self::EVENTS_TO_PROCESS_AT_TIME,
],
$this->template()
);
file_put_contents($this->fileName($subscriberName), $fileContent);
};
}
private function template(): string
{
return <<<EOF
[program:codelytv_{queue_name}]
command = {path}/apps/mooc/backend/bin/console codely:domain-events:rabbitmq:consume --env=prod {queue_name} {events_to_process}
process_name = %(program_name)s_%(process_num)02d
numprocs = {processes}
startsecs = 1
startretries = 10
exitcodes = 2
stopwaitsecs = 300
autostart = true
EOF;
}
private function fileName(string $queue): string
{
return sprintf('%s/%s.ini', self::SUPERVISOR_PATH, $queue);
}
}