feat: more pm2 fixes

This commit is contained in:
Eli Bosley
2025-01-29 10:55:42 -05:00
parent 4b3d6a7ba3
commit 4655d72fbb
2 changed files with 9 additions and 36 deletions

View File

@@ -16,7 +16,9 @@
".env.*", ".env.*",
"myservers.cfg" "myservers.cfg"
], ],
"log": "/var/log/unraid-api/unraid-api.log" "out_file": "/var/log/unraid-api/unraid-api.log",
"error_file": "/var/log/unraid-api/unraid-api.error.log",
"kill_timeout": 3000
} }
] ]
} }

View File

@@ -1,6 +1,3 @@
import { existsSync } from 'node:fs';
import { writeFile } from 'node:fs/promises';
import { execa } from 'execa'; import { execa } from 'execa';
import { Command, CommandRunner, Option } from 'nest-commander'; import { Command, CommandRunner, Option } from 'nest-commander';
@@ -8,7 +5,6 @@ import type { LogLevel } from '@app/core/log';
import { ECOSYSTEM_PATH, PM2_PATH } from '@app/consts'; import { ECOSYSTEM_PATH, PM2_PATH } from '@app/consts';
import { levels } from '@app/core/log'; import { levels } from '@app/core/log';
import { LogService } from '@app/unraid-api/cli/log.service'; import { LogService } from '@app/unraid-api/cli/log.service';
import { LOG_LEVEL, NODE_ENV } from '@app/environment';
interface StartCommandOptions { interface StartCommandOptions {
'log-level'?: string; 'log-level'?: string;
@@ -20,48 +16,23 @@ export class StartCommand extends CommandRunner {
super(); super();
} }
async configurePm2(): Promise<void> { async cleanupPM2State() {
if (existsSync('/tmp/pm2-configured')) { await execa(PM2_PATH, ['stop', ECOSYSTEM_PATH])
this.logger.debug('PM2 already configured');
return;
}
// Write a temp file when first started to prevent needing to run this again
// Install PM2 Logrotate
await execa(PM2_PATH, ['install', 'pm2-logrotate'])
.then(({ stdout }) => {
this.logger.debug(stdout);
})
.catch(({ stderr }) => {
this.logger.error('PM2 Logrotate Error: ' + stderr);
});
// Now set logrotate options
await execa(PM2_PATH, ['set', 'pm2-logrotate:retain', '2'])
.then(({ stdout }) => this.logger.debug(stdout)) .then(({ stdout }) => this.logger.debug(stdout))
.catch(({ stderr }) => this.logger.error('PM2 Logrotate Set Error: ' + stderr)); .catch(({ stderr }) => this.logger.error('PM2 Stop Error: ' + stderr));
await execa(PM2_PATH, ['set', 'pm2-logrotate:compress', 'true']) await execa(PM2_PATH, ['delete', ECOSYSTEM_PATH])
.then(({ stdout }) => this.logger.debug(stdout)) .then(({ stdout }) => this.logger.debug(stdout))
.catch(({ stderr }) => this.logger.error('PM2 Logrotate Compress Error: ' + stderr)); .catch(({ stderr }) => this.logger.error('PM2 Delete API Error: ' + stderr));
await execa(PM2_PATH, ['set', 'pm2-logrotate:max_size', '1M'])
.then(({ stdout }) => this.logger.debug(stdout))
.catch(({ stderr }) => this.logger.error('PM2 Logrotate Max Size Error: ' + stderr));
// PM2 Save Settings
await execa(PM2_PATH, ['set', 'pm2:autodump', 'true'])
.then(({ stdout }) => this.logger.debug(stdout))
.catch(({ stderr }) => this.logger.error('PM2 Autodump Error: ' + stderr));
// Update PM2 // Update PM2
await execa(PM2_PATH, ['update']) await execa(PM2_PATH, ['update'])
.then(({ stdout }) => this.logger.debug(stdout)) .then(({ stdout }) => this.logger.debug(stdout))
.catch(({ stderr }) => this.logger.error('PM2 Update Error: ' + stderr)); .catch(({ stderr }) => this.logger.error('PM2 Update Error: ' + stderr));
await writeFile('/tmp/pm2-configured', 'true');
} }
async run(_: string[], options: StartCommandOptions): Promise<void> { async run(_: string[], options: StartCommandOptions): Promise<void> {
this.logger.info('Starting the Unraid API'); this.logger.info('Starting the Unraid API');
await this.configurePm2(); await this.cleanupPM2State();
const envLog = options['log-level'] ? `LOG_LEVEL=${options['log-level']}` : ''; const envLog = options['log-level'] ? `LOG_LEVEL=${options['log-level']}` : '';
const { stderr, stdout } = await execa(`${envLog} ${PM2_PATH}`.trim(), [ const { stderr, stdout } = await execa(`${envLog} ${PM2_PATH}`.trim(), [
'start', 'start',