Skip to content

Commit 83e64ba

Browse files
committed
feat: better pm2 calls, log lines
1 parent 608d381 commit 83e64ba

File tree

5 files changed

+78
-25
lines changed

5 files changed

+78
-25
lines changed

api/src/consts.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { PORT } from '@app/environment';
2-
import { type JSONWebKeySet } from 'jose';
2+
import type { JSONWebKeySet } from 'jose';
33
import { join } from 'path';
44

55
export const getInternalApiAddress = (isHttp = true, nginxPort = 80) => {
@@ -81,4 +81,5 @@ export const KEYSERVER_VALIDATION_ENDPOINT =
8181
/** Set the max retries for the GraphQL Client */
8282
export const MAX_RETRIES_FOR_LINEAR_BACKOFF = 100;
8383

84-
export const PM2_PATH = join(import.meta.dirname, '../../', 'node_modules', '.bin', 'pm2');
84+
export const PM2_PATH = join(import.meta.dirname, '../../', 'node_modules', '.bin', 'pm2');
85+
export const ECOSYSTEM_PATH = join(import.meta.dirname, '../../', 'ecosystem.config.json');

api/src/unraid-api/cli/cli.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { SwitchEnvCommand } from '@app/unraid-api/cli/switch-env.command';
1010
import { VersionCommand } from '@app/unraid-api/cli/version.command';
1111
import { StatusCommand } from '@app/unraid-api/cli/status.command';
1212
import { ValidateTokenCommand } from '@app/unraid-api/cli/validate-token.command';
13+
import { LogsCommand } from '@app/unraid-api/cli/logs.command';
1314

1415
@Module({
1516
providers: [
@@ -22,7 +23,8 @@ import { ValidateTokenCommand } from '@app/unraid-api/cli/validate-token.command
2223
SwitchEnvCommand,
2324
VersionCommand,
2425
StatusCommand,
25-
ValidateTokenCommand
26+
ValidateTokenCommand,
27+
LogsCommand
2628
],
2729
})
2830
export class CliModule {}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { execa } from 'execa';
2+
import { Command, CommandRunner, Option } from 'nest-commander';
3+
4+
import { ECOSYSTEM_PATH, PM2_PATH } from '@app/consts';
5+
import { LogService } from '@app/unraid-api/cli/log.service';
6+
7+
interface LogsOptions {
8+
lines: number
9+
}
10+
11+
@Command({ name: 'logs' })
12+
export class LogsCommand extends CommandRunner {
13+
constructor(private readonly logger: LogService) {
14+
super();
15+
}
16+
17+
@Option({ flags: '-l, --lines', description: 'Number of lines to tail'})
18+
parseLines(input: string): number
19+
{
20+
return isNaN(parseInt(input)) ? 100 : parseInt(input)
21+
}
22+
23+
async run(passedParams: string[], options?: LogsOptions): Promise<void> {
24+
const lines = options?.lines ?? 100;
25+
const subprocess = execa(PM2_PATH, ['logs', ECOSYSTEM_PATH, '--lines', lines.toString()]);
26+
27+
subprocess.stdout?.on('data', (data) => {
28+
this.logger.log(data.toString());
29+
});
30+
31+
subprocess.stderr?.on('data', (data) => {
32+
this.logger.error(data.toString());
33+
});
34+
35+
await subprocess;
36+
}
37+
}
Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,38 @@
1-
import { PM2_PATH } from '@app/consts';
21
import { execSync } from 'child_process';
3-
import { Command, CommandRunner } from 'nest-commander';
42
import { join } from 'path';
53

4+
5+
6+
import { Command, CommandRunner } from 'nest-commander';
7+
8+
9+
10+
import { ECOSYSTEM_PATH, PM2_PATH } from '@app/consts';
11+
12+
13+
14+
15+
616
/**
717
* Stop a running API process and then start it again.
818
*/
919
@Command({ name: 'restart', description: 'Restart / Start the Unraid API'})
1020
export class RestartCommand extends CommandRunner {
1121
async run(_): Promise<void> {
22+
console.log(
23+
'Dirname is ',
24+
import.meta.dirname,
25+
' command is ',
26+
`${PM2_PATH} restart ${ECOSYSTEM_PATH} --update-env`
27+
);
1228
execSync(
13-
`${PM2_PATH} restart ${join(import.meta.dirname, '../../', 'ecosystem.config.json')} --update-env`,
29+
`${PM2_PATH} restart ${ECOSYSTEM_PATH} --update-env`,
1430
{
1531
env: process.env,
16-
stdio: 'inherit',
32+
stdio: 'pipe',
1733
cwd: process.cwd(),
1834
}
1935
);
2036
}
2137

22-
}
38+
}

api/src/unraid-api/cli/start.command.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { execSync } from 'child_process';
22
import { join } from 'path';
33

4+
import { execa } from 'execa';
45
import { Command, CommandRunner, Option } from 'nest-commander';
56

6-
import { PM2_PATH } from '@app/consts';
7+
import { ECOSYSTEM_PATH, PM2_PATH } from '@app/consts';
78
import { levels } from '@app/core/log';
89
import { LogService } from '@app/unraid-api/cli/log.service';
910

@@ -21,27 +22,23 @@ export class StartCommand extends CommandRunner {
2122
}
2223

2324
async run(_, options: StartCommandOptions): Promise<void> {
24-
this.logger.debug(options);
25-
this.logger.log(
26-
`Starting unraid-api with command:
27-
${PM2_PATH} start ${join(import.meta.dirname, 'ecosystem.config.json')} --update-env`
28-
);
29-
30-
execSync(
31-
`${PM2_PATH} start ${join(import.meta.dirname, '../../', 'ecosystem.config.json')} --update-env`,
32-
{
33-
env: process.env,
34-
stdio: 'inherit',
35-
cwd: process.cwd(),
36-
}
37-
);
25+
this.logger.info('Starting the Unraid API');
26+
const envLog = options['log-level'] ? `LOG_LEVEL=${options['log-level']}` : ''
27+
const { stderr, stdout } = await execa(`${envLog} ${PM2_PATH}`.trim(), ['start', ECOSYSTEM_PATH, '--update-env']);
28+
if (stdout) {
29+
this.logger.log(stdout);
30+
}
31+
if (stderr) {
32+
this.logger.error(stderr);
33+
process.exit(1);
34+
}
3835
}
3936

4037
@Option({
41-
flags: '--log-level [string]',
38+
flags: `--log-level <${levels.join('|')}>`,
4239
description: 'log level to use',
4340
})
44-
parseLogLevel(val: unknown): typeof levels {
41+
parseLogLevel(val: string): typeof levels {
4542
return (levels.includes(val as (typeof levels)[number])
4643
? (val as (typeof levels)[number])
4744
: 'info') as unknown as typeof levels;

0 commit comments

Comments
 (0)