forked from CodelyTV/php-ddd-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenerateSupervisorFilesCommand.php
75 lines (62 loc) · 2.44 KB
/
GenerateSupervisorFilesCommand.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
<?php
declare(strict_types = 1);
namespace CodelyTv\MoocBackend\Command;
use CodelyTv\Shared\Infrastructure\Bus\Event\DomainEventSubscriberConfig;
use CodelyTv\Shared\Infrastructure\Bus\Event\DomainEventSubscribersConfiguration;
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;
final class GenerateSupervisorFilesCommand extends Command
{
private const SUPERVISOR_PATH = __DIR__ . '/../../app/config/supervisor';
private $configuration;
public function __construct(DomainEventSubscribersConfiguration $configuration)
{
parent::__construct();
$this->configuration = $configuration;
}
protected function configure(): void
{
$this
->setName('codelytv:domain-events:generate-supervisor-files')
->setDescription('Generate the supervisor configuration for every subscriber')
->addArgument('command-path', InputArgument::OPTIONAL, 'Path on this is gonna be deployed', '/var/www');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
/** @var string $path */
$path = $input->getArgument('command-path');
each($this->configCreator($path), $this->configuration->all());
}
private function configCreator(string $path): callable
{
return function (DomainEventSubscriberConfig $config) use ($path) {
$fileContent = str_replace(
['{subscriber}', '{path}', '{processes}', '{events_to_process}', '{enabled}'],
[$config->name(), $path, $config->processes(), $config->eventsToProcess(), $config->enabledString()],
$this->template()
);
file_put_contents($this->fileName($config->name()), $fileContent);
};
}
private function template(): string
{
return <<<EOF
[program:codely_{subscriber}]
command = {path}/applications/api/bin/console codelytv:domain-events:consume --env=prod {subscriber} {events_to_process}
process_name = %(program_name)s_%(process_num)02d
numprocs = {processes}
startsecs = 1
startretries = 10
exitcodes = 2
stopwaitsecs = 300
autostart = {enabled}
EOF;
}
private function fileName(string $queue)
{
return sprintf('%s/%s.ini', self::SUPERVISOR_PATH, $queue);
}
}