|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace WonderNetwork\SlimKernel\Supervisor; |
| 6 | + |
| 7 | +use Symfony\Component\Console\Command\Command; |
| 8 | +use Symfony\Component\Console\Input\InputArgument; |
| 9 | +use Symfony\Component\Console\Input\InputInterface; |
| 10 | +use Symfony\Component\Console\Input\InputOption; |
| 11 | +use Symfony\Component\Console\Output\OutputInterface; |
| 12 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 13 | +use WonderNetwork\SlimKernel\Cli\InitializeInputParamsTrait; |
| 14 | + |
| 15 | +final class GenerateSupervisorConfigCommand extends Command { |
| 16 | + use InitializeInputParamsTrait; |
| 17 | + |
| 18 | + public function __construct( |
| 19 | + private readonly string $configDir, |
| 20 | + private readonly SupervisorConfiguration $configuration, |
| 21 | + ) { |
| 22 | + parent::__construct('supervisor:generate-config'); |
| 23 | + } |
| 24 | + |
| 25 | + protected function configure(): void { |
| 26 | + $this->addArgument( |
| 27 | + name: 'current-directory', |
| 28 | + mode: InputArgument::REQUIRED, |
| 29 | + description: 'The absolute directory to the script', |
| 30 | + ); |
| 31 | + |
| 32 | + $this->addOption( |
| 33 | + name: 'stdio', |
| 34 | + mode: InputOption::VALUE_NONE, |
| 35 | + description: "Log to stdout/stderr instead of files", |
| 36 | + ); |
| 37 | + |
| 38 | + $this->addOption( |
| 39 | + name: 'purge', |
| 40 | + mode: InputOption::VALUE_NONE | InputOption::VALUE_NEGATABLE, |
| 41 | + description: 'Remove all files before generating new ones', |
| 42 | + ); |
| 43 | + |
| 44 | + $this->addOption( |
| 45 | + name: 'logfile', |
| 46 | + mode: InputOption::VALUE_REQUIRED, |
| 47 | + default: '/var/log/supervisor/{processName}.{suffix}.log', |
| 48 | + ); |
| 49 | + |
| 50 | + $this->addOption( |
| 51 | + name: 'logfile-maxbytes', |
| 52 | + mode: InputOption::VALUE_REQUIRED, |
| 53 | + default: "50MB", |
| 54 | + ); |
| 55 | + |
| 56 | + $this->addOption( |
| 57 | + name: 'config-dir', |
| 58 | + mode: InputOption::VALUE_REQUIRED, |
| 59 | + default: $this->configDir, |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
| 64 | + $io = new SymfonyStyle($input, $output); |
| 65 | + |
| 66 | + $currentDirectory = $this->params->arguments->string('current-directory'); |
| 67 | + $stdio = $this->params->options->bool('stdio'); |
| 68 | + $noPurge = $this->params->options->bool('no-purge'); |
| 69 | + $logfile = $this->params->options->string('logfile'); |
| 70 | + $maxBytes = $this->params->options->string('logfile-maxbytes'); |
| 71 | + $configDir = $this->params->options->string('config-dir'); |
| 72 | + |
| 73 | + if ("" === $configDir) { |
| 74 | + $io->error("Config directory can't be empty"); |
| 75 | + |
| 76 | + return self::FAILURE; |
| 77 | + } |
| 78 | + |
| 79 | + if (false === $noPurge) { |
| 80 | + foreach (glob(sprintf('%s/*.conf', $configDir)) ?: [] as $preExistingConfig) { |
| 81 | + unlink($preExistingConfig); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if ($stdio) { |
| 86 | + $maxBytes = "0"; |
| 87 | + $logfile = '/dev/std{suffix}'; |
| 88 | + } |
| 89 | + |
| 90 | + foreach ($this->configuration->programs as $program) { |
| 91 | + $concurrency = $program->concurrency; |
| 92 | + $processName = $program->name; |
| 93 | + |
| 94 | + if ($concurrency > 1) { |
| 95 | + $processName = '%(program_name)s_%(process_num)02d'; |
| 96 | + } |
| 97 | + |
| 98 | + $errorLog = strtr($logfile, ['{processName}' => $processName, '{suffix}' => 'err']); |
| 99 | + $standardLog = strtr($logfile, ['{processName}' => $processName, '{suffix}' => 'out']); |
| 100 | + |
| 101 | + $fullCommand = sprintf('%s/%s', rtrim($currentDirectory, '/'), $program->command); |
| 102 | + $supervisorConfigPath = sprintf('%s/%s.conf', $configDir, $program->name); |
| 103 | + |
| 104 | + $supervisorConfig = <<<EOF |
| 105 | + [program:$program->name] |
| 106 | + command=$fullCommand |
| 107 | + process_name=$processName |
| 108 | + numprocs=$concurrency |
| 109 | + user=www-data |
| 110 | + autostart=true |
| 111 | + autorestart=true |
| 112 | + stderr_logfile=$errorLog |
| 113 | + stderr_logfile_maxbytes=$maxBytes |
| 114 | + stdout_logfile=$standardLog |
| 115 | + stdout_logfile_maxbytes=$maxBytes |
| 116 | + EOF; |
| 117 | + |
| 118 | + file_put_contents($supervisorConfigPath, $supervisorConfig); |
| 119 | + $io->writeln( |
| 120 | + sprintf( |
| 121 | + 'Writing <comment>%s</comment> configuration file to <info>%s</info>', |
| 122 | + $program->name, |
| 123 | + $supervisorConfigPath, |
| 124 | + ), |
| 125 | + ); |
| 126 | + } |
| 127 | + |
| 128 | + return self::SUCCESS; |
| 129 | + } |
| 130 | +} |
0 commit comments