From 4655d72fbbe09fda6282feda3b29fdc5fd5d9945 Mon Sep 17 00:00:00 2001 From: Eli Bosley Date: Wed, 29 Jan 2025 10:55:42 -0500 Subject: [PATCH] feat: more pm2 fixes --- api/ecosystem.config.json | 4 ++- api/src/unraid-api/cli/start.command.ts | 41 ++++--------------------- 2 files changed, 9 insertions(+), 36 deletions(-) diff --git a/api/ecosystem.config.json b/api/ecosystem.config.json index 8cb5960c3..456e6376b 100644 --- a/api/ecosystem.config.json +++ b/api/ecosystem.config.json @@ -16,7 +16,9 @@ ".env.*", "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 } ] } \ No newline at end of file diff --git a/api/src/unraid-api/cli/start.command.ts b/api/src/unraid-api/cli/start.command.ts index a604fdfb5..e8c9c3954 100644 --- a/api/src/unraid-api/cli/start.command.ts +++ b/api/src/unraid-api/cli/start.command.ts @@ -1,6 +1,3 @@ -import { existsSync } from 'node:fs'; -import { writeFile } from 'node:fs/promises'; - import { execa } from 'execa'; 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 { levels } from '@app/core/log'; import { LogService } from '@app/unraid-api/cli/log.service'; -import { LOG_LEVEL, NODE_ENV } from '@app/environment'; interface StartCommandOptions { 'log-level'?: string; @@ -20,48 +16,23 @@ export class StartCommand extends CommandRunner { super(); } - async configurePm2(): Promise { - if (existsSync('/tmp/pm2-configured')) { - 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']) + async cleanupPM2State() { + await execa(PM2_PATH, ['stop', ECOSYSTEM_PATH]) .then(({ stdout }) => this.logger.debug(stdout)) - .catch(({ stderr }) => this.logger.error('PM2 Logrotate Set Error: ' + stderr)); - await execa(PM2_PATH, ['set', 'pm2-logrotate:compress', 'true']) + .catch(({ stderr }) => this.logger.error('PM2 Stop Error: ' + stderr)); + await execa(PM2_PATH, ['delete', ECOSYSTEM_PATH]) .then(({ stdout }) => this.logger.debug(stdout)) - .catch(({ stderr }) => this.logger.error('PM2 Logrotate Compress 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)); + .catch(({ stderr }) => this.logger.error('PM2 Delete API Error: ' + stderr)); // Update PM2 await execa(PM2_PATH, ['update']) .then(({ stdout }) => this.logger.debug(stdout)) .catch(({ stderr }) => this.logger.error('PM2 Update Error: ' + stderr)); - - await writeFile('/tmp/pm2-configured', 'true'); } async run(_: string[], options: StartCommandOptions): Promise { this.logger.info('Starting the Unraid API'); - await this.configurePm2(); - + await this.cleanupPM2State(); const envLog = options['log-level'] ? `LOG_LEVEL=${options['log-level']}` : ''; const { stderr, stdout } = await execa(`${envLog} ${PM2_PATH}`.trim(), [ 'start',