Skip to content

Commit

Permalink
chore(rce): finish span on finally (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
aldy505 authored Sep 17, 2023
1 parent 898c936 commit 691e067
Showing 1 changed file with 71 additions and 59 deletions.
130 changes: 71 additions & 59 deletions rce/src/job/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface CommandOutput {
export class Job implements JobPrerequisites {
private _sourceFilePath: string[];
private _builtFilePath: string;
private _entrypointsPath: string[];
private readonly _entrypointsPath: string[];
private _baseFilePath: string;
public readonly compileTimeout: number;
public readonly runTimeout: number;
Expand Down Expand Up @@ -148,7 +148,7 @@ export class Job implements JobPrerequisites {
await this.cleanup();
throw error;
} finally {
span?.finish();
span?.finish();
}
}

Expand Down Expand Up @@ -193,8 +193,9 @@ export class Job implements JobPrerequisites {
return result;
} catch (error) {
await this.cleanup();
span?.finish();
throw error;
} finally {
span?.finish();
}
}

Expand All @@ -219,72 +220,83 @@ export class Job implements JobPrerequisites {
}

private executeCommand(command: string[]): Promise<CommandOutput> {
const { gid, uid, username } = this.user;
const timeout = this.compileTimeout;

return new Promise((resolve, reject) => {
let stdout = "";
let stderr = "";
let output = "";
let exitCode = 0;
let exitSignal = "";

const cmd = childProcess.spawn(command[0], command.slice(1), {
env: {
PATH: process.env?.PATH ?? "",
LOGGER_TOKEN: "",
LOGGER_SERVER_ADDRESS: "",
ENVIRONMENT: "",
...this.runtime.environment
},
cwd: "/code/" + username,
gid: gid,
uid: uid,
timeout: timeout ?? 5_000,
stdio: "pipe",
detached: true
});
const span = Sentry.getCurrentHub()?.getScope()?.getSpan()?.startChild({
op: "job.execute_command",
data: {
command
}
});

cmd.stdout.on("data", (data) => {
stdout += data.toString();
output += data.toString();
try {
const { gid, uid, username } = this.user;
const timeout = this.compileTimeout;

return new Promise((resolve, reject) => {
let stdout = "";
let stderr = "";
let output = "";
let exitCode = 0;
let exitSignal = "";

const cmd = childProcess.spawn(command[0], command.slice(1), {
env: {
PATH: process.env?.PATH ?? "",
LOGGER_TOKEN: "",
LOGGER_SERVER_ADDRESS: "",
ENVIRONMENT: "",
...this.runtime.environment
},
cwd: "/code/" + username,
gid: gid,
uid: uid,
timeout: timeout ?? 5_000,
stdio: "pipe",
detached: true
});

if (process.env.ENVIRONMENT === "development") {
console.log(data.toString());
}
});
cmd.stdout.on("data", (data) => {
stdout += data.toString();
output += data.toString();

cmd.stderr.on("data", (data) => {
stderr += data.toString();
output += data.toString();
if (process.env.ENVIRONMENT === "development") {
console.log(data.toString());
}
});

if (process.env.ENVIRONMENT === "development") {
console.log(data.toString());
}
});
cmd.stderr.on("data", (data) => {
stderr += data.toString();
output += data.toString();

cmd.on("error", (error) => {
cmd.stdout.destroy();
cmd.stderr.destroy();
if (process.env.ENVIRONMENT === "development") {
console.log(data.toString());
}
});

reject(error.message);
});
cmd.on("error", (error) => {
cmd.stdout.destroy();
cmd.stderr.destroy();

cmd.on("close", (code, signal) => {
cmd.stdout.destroy();
cmd.stderr.destroy();
reject(error.message);
});

cmd.on("close", (code, signal) => {
cmd.stdout.destroy();
cmd.stderr.destroy();

exitCode = code ?? 0;
exitSignal = signal ?? "";
exitCode = code ?? 0;
exitSignal = signal ?? "";

resolve({
stdout: stdout.slice(0, 5000),
stderr: stderr.slice(0, 5000),
output: output.slice(0, 5000),
exitCode,
signal: exitSignal
resolve({
stdout: stdout.slice(0, 5000),
stderr: stderr.slice(0, 5000),
output: output.slice(0, 5000),
exitCode,
signal: exitSignal
});
});
});
});
} finally {
span?.finish();
}
}
}

0 comments on commit 691e067

Please sign in to comment.