From 691e067817db801e65a73d0bbdd0813baad55c96 Mon Sep 17 00:00:00 2001 From: Reinaldy Rafli Date: Sun, 17 Sep 2023 09:41:31 +0700 Subject: [PATCH] chore(rce): finish span on finally (#138) --- rce/src/job/job.ts | 130 +++++++++++++++++++++++++-------------------- 1 file changed, 71 insertions(+), 59 deletions(-) diff --git a/rce/src/job/job.ts b/rce/src/job/job.ts index 2cd4fa3..4994fb6 100644 --- a/rce/src/job/job.ts +++ b/rce/src/job/job.ts @@ -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; @@ -148,7 +148,7 @@ export class Job implements JobPrerequisites { await this.cleanup(); throw error; } finally { - span?.finish(); + span?.finish(); } } @@ -193,8 +193,9 @@ export class Job implements JobPrerequisites { return result; } catch (error) { await this.cleanup(); - span?.finish(); throw error; + } finally { + span?.finish(); } } @@ -219,72 +220,83 @@ export class Job implements JobPrerequisites { } private executeCommand(command: string[]): Promise { - 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(); + } } }