Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions api/src/unraid-api/cli/log.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,10 @@ export class LogService {
this.logger.debug(message, ...optionalParams);
}
}

trace(message: any, ...optionalParams: any[]): void {
if (this.shouldLog('trace')) {
this.logger.log(message, ...optionalParams);
}
}
}
29 changes: 16 additions & 13 deletions api/src/unraid-api/cli/pm2.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,32 @@ export class PM2Service {
constructor(private readonly logger: LogService) {}

// Type Overload: if raw is true, return an execa ResultPromise (which is a Promise with extra properties)
/**
* Executes a PM2 command with the specified context and arguments.
* Handles logging automatically (stdout -> trace, stderr -> error), unless the `raw` flag is
* set to true, in which case the caller must handle desired effects.
*
* @param context - Execa Options for command execution, such as a unique tag for logging
* and whether the result should be handled raw.
* @param args - The arguments to pass to the PM2 command.
* @returns ResultPromise\<@param context\> When raw is true
* @returns Promise\<Result\> When raw is false
*/
run<T extends CmdContext>(context: T & { raw: true }, ...args: string[]): ResultPromise<T>;

// Type Overload: if raw is false, return a plain Promise<Result>
run(context: CmdContext & { raw?: false }, ...args: string[]): Promise<Result>;

/**
* Executes a PM2 command with the provided arguments and environment variables.
*
* @param context - An object containing a tag for logging purposes and optional environment variables (merging with current env).
* @param args - Arguments to pass to the PM2 command. Each arguement is escaped.
* @returns A promise that resolves to a Result object containing the command's output.
* Logs debug information on success and error details on failure.
*/
async run(context: CmdContext, ...args: string[]) {
const { tag, raw, ...execOptions } = context;
execOptions.extendEnv ??= false;
execOptions.shell ??= 'bash';
const runCommand = () => execa(PM2_PATH, [...args], execOptions satisfies Options);
if (raw) {
return runCommand();
}
return runCommand()
.then((result) => {
this.logger.debug(result.stdout);
this.logger.log(`Operation "${tag}" completed.`);
this.logger.trace(result.stdout);
return result;
})
.catch((result: Result) => {
Expand All @@ -59,13 +62,13 @@ export class PM2Service {
/**
* Deletes the PM2 dump file.
*
* This method removes the PM2 dump file located at `~/.pm2/dump.pm2`.
* This method removes the PM2 dump file located at `~/.pm2/dump.pm2` by default.
* It logs a message indicating that the PM2 dump has been cleared.
*
* @returns A promise that resolves once the dump file is removed.
*/
async deleteDump(dumpFile = join(PM2_HOME, 'dump.pm2')) {
await rm(dumpFile, { force: true });
this.logger.log('PM2 dump cleared.');
this.logger.trace('PM2 dump cleared.');
}
}
6 changes: 1 addition & 5 deletions api/src/unraid-api/cli/status.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ export class StatusCommand extends CommandRunner {
super();
}
async run(): Promise<void> {
await this.pm2.run(
{ tag: 'PM2 Status', stdio: 'inherit', shell: 'bash', raw: true },
'status',
'unraid-api'
);
await this.pm2.run({ tag: 'PM2 Status', stdio: 'inherit', raw: true }, 'status', 'unraid-api');
}
}
Loading