Skip to content

Commit

Permalink
trim args and log output by default
Browse files Browse the repository at this point in the history
  • Loading branch information
nicktrn committed Sep 26, 2024
1 parent adb3d44 commit 0cc518f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion apps/coordinator/src/checkpointer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ export class Checkpointer {
attemptNumber?: number
) {
const containterNameWithAttempt = this.#getRunContainerName(runId, attemptNumber);
const exec = new Exec({ logger: this.#logger, abortSignal, logOutput: true });
const exec = new Exec({ logger: this.#logger, abortSignal });

try {
if (this.opts.forceSimulate || !this.#canCheckpoint) {
Expand Down
30 changes: 18 additions & 12 deletions apps/coordinator/src/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ interface ExecOptions {
logger?: SimpleLogger;
abortSignal?: AbortSignal;
logOutput?: boolean;
trimArgs?: boolean;
}

export class Exec {
private logger: SimpleLogger;
private abortSignal: AbortSignal | undefined;

private logOutput: boolean;
private trimArgs: boolean;

constructor(opts: ExecOptions) {
this.logger = opts.logger ?? new SimpleLogger();
Expand All @@ -38,11 +41,12 @@ export class Exec {
});
}

this.logOutput = opts.logOutput ?? false;
this.logOutput = opts.logOutput ?? true;
this.trimArgs = opts.trimArgs ?? true;
}

async x(command: string, args?: string[], opts?: { neverThrow?: boolean; trimArgs?: boolean }) {
const argsTrimmed = opts?.trimArgs === true ? args?.map((arg) => arg.trim()) : args;
async x(command: string, args?: string[], opts?: { neverThrow?: boolean }) {
const argsTrimmed = this.trimArgs ? args?.map((arg) => arg.trim()) : args;

const commandWithFirstArg = `${command}${argsTrimmed?.length ? ` ${argsTrimmed[0]}` : ""}`;
this.logger.debug(`exec: ${commandWithFirstArg}`, { command, args, argsTrimmed });
Expand All @@ -55,31 +59,33 @@ export class Exec {

const output = await result;

const metadata = {
command,
argsRaw: args,
argsTrimmed,
...output,
};

if (this.logOutput) {
this.logger.debug(`output: ${commandWithFirstArg}`, { command, args, argsTrimmed, output });
this.logger.debug(`output: ${commandWithFirstArg}`, metadata);
}

if (opts?.neverThrow) {
return output;
}

if (result.aborted) {
this.logger.error(`aborted: ${commandWithFirstArg}`, { command, args, argsTrimmed, output });
this.logger.error(`aborted: ${commandWithFirstArg}`, metadata);
throw new TinyResult(result);
}

if (result.killed) {
this.logger.error(`killed: ${commandWithFirstArg}`, { command, args, argsTrimmed, output });
this.logger.error(`killed: ${commandWithFirstArg}`, metadata);
throw new TinyResult(result);
}

if (result.exitCode !== 0) {
this.logger.error(`non-zero exit: ${commandWithFirstArg}`, {
command,
args,
argsTrimmed,
output,
});
this.logger.error(`non-zero exit: ${commandWithFirstArg}`, metadata);
throw new TinyResult(result);
}

Expand Down

0 comments on commit 0cc518f

Please sign in to comment.