-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerateSupervisorConfigCommand.php
More file actions
131 lines (107 loc) · 4.28 KB
/
Copy pathGenerateSupervisorConfigCommand.php
File metadata and controls
131 lines (107 loc) · 4.28 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
121
122
123
124
125
126
127
128
129
130
131
<?php
declare(strict_types=1);
namespace WonderNetwork\SlimKernel\Supervisor;
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\Console\Style\SymfonyStyle;
use WonderNetwork\SlimKernel\Cli\InitializeInputParamsTrait;
final class GenerateSupervisorConfigCommand extends Command {
use InitializeInputParamsTrait;
public function __construct(
private readonly string $configDir,
private readonly SupervisorConfiguration $configuration,
) {
parent::__construct('supervisor:generate-config');
}
protected function configure(): void {
$this->addArgument(
name: 'current-directory',
mode: InputArgument::REQUIRED,
description: 'The absolute directory to the script',
);
$this->addOption(
name: 'stdio',
mode: InputOption::VALUE_NONE,
description: "Log to stdout/stderr instead of files",
);
$this->addOption(
name: 'purge',
mode: InputOption::VALUE_NONE | InputOption::VALUE_NEGATABLE,
description: 'Remove all files before generating new ones',
);
$this->addOption(
name: 'logfile',
mode: InputOption::VALUE_REQUIRED,
default: '/var/log/supervisor/{processName}.{suffix}.log',
);
$this->addOption(
name: 'logfile-maxbytes',
mode: InputOption::VALUE_REQUIRED,
default: "50MB",
);
$this->addOption(
name: 'config-dir',
mode: InputOption::VALUE_REQUIRED,
default: $this->configDir,
);
}
protected function execute(InputInterface $input, OutputInterface $output): int {
$io = new SymfonyStyle($input, $output);
$currentDirectory = $this->params->arguments->string('current-directory');
$stdio = $this->params->options->bool('stdio');
$noPurge = $this->params->options->bool('no-purge');
$logfile = $this->params->options->string('logfile');
$maxBytes = $this->params->options->string('logfile-maxbytes');
$configDir = $this->params->options->string('config-dir');
if ("" === $configDir) {
$io->error("Config directory can't be empty");
return self::FAILURE;
}
if (false === $noPurge) {
foreach (glob(sprintf('%s/*.conf', $configDir)) ?: [] as $preExistingConfig) {
unlink($preExistingConfig);
}
}
if ($stdio) {
$maxBytes = "0";
$logfile = '/dev/std{suffix}';
}
foreach ($this->configuration->programs as $program) {
$concurrency = $program->concurrency;
$processName = $program->name;
if ($concurrency > 1) {
$processName = '%(program_name)s_%(process_num)02d';
}
$errorLog = strtr($logfile, ['{processName}' => $processName, '{suffix}' => 'err']);
$standardLog = strtr($logfile, ['{processName}' => $processName, '{suffix}' => 'out']);
$fullCommand = sprintf('%s/%s', rtrim($currentDirectory, '/'), $program->command);
$supervisorConfigPath = sprintf('%s/%s.conf', $configDir, $program->name);
$supervisorConfig = <<<EOF
[program:$program->name]
command=$fullCommand
process_name=$processName
numprocs=$concurrency
startretries=$program->startretries
user=www-data
autostart=true
autorestart=true
stderr_logfile=$errorLog
stderr_logfile_maxbytes=$maxBytes
stdout_logfile=$standardLog
stdout_logfile_maxbytes=$maxBytes
EOF;
file_put_contents($supervisorConfigPath, $supervisorConfig);
$io->writeln(
sprintf(
'Writing <comment>%s</comment> configuration file to <info>%s</info>',
$program->name,
$supervisorConfigPath,
),
);
}
return self::SUCCESS;
}
}