2022-08-14 19:23:37 +00:00
|
|
|
import io from "socket.io-client";
|
2022-05-05 12:35:47 +00:00
|
|
|
import cp from "child_process";
|
|
|
|
|
|
|
|
import modes from "../modes.js";
|
|
|
|
import events from "../events.js";
|
|
|
|
|
|
|
|
export { default as events } from "../events.js";
|
|
|
|
export { default as modes } from "../modes.js";
|
|
|
|
|
|
|
|
// Data Stream Types
|
|
|
|
const ERR = "e";
|
|
|
|
const OUT = "o";
|
|
|
|
|
|
|
|
export default class Executor {
|
2022-08-09 04:29:10 +00:00
|
|
|
constructor(config, payload) {
|
2022-10-08 17:47:46 +00:00
|
|
|
this.url = config.url(payload) ?? process.env.QUALITEER_EXECUTOR_URL;
|
2022-08-09 04:29:10 +00:00
|
|
|
this.jobId = config.jobId(payload) ?? process.env.QUALITEER_JOB_ID;
|
|
|
|
this.command = config.command(payload) ?? process.env.QUALITEER_COMMAND;
|
2022-05-05 12:35:47 +00:00
|
|
|
this.mode = modes.EXEC;
|
|
|
|
|
|
|
|
// Internal Buffer
|
|
|
|
this.buf = {};
|
|
|
|
this.buf[ERR] = "";
|
|
|
|
this.buf[OUT] = "";
|
|
|
|
|
|
|
|
// Methods
|
2022-08-09 04:29:10 +00:00
|
|
|
this.spawn = this.spawn.bind(this);
|
|
|
|
this.report = this.report.bind(this);
|
|
|
|
this.onProcClose = this.onProcClose.bind(this);
|
|
|
|
this.onClose = this.onClose.bind(this);
|
2022-05-05 12:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
spawn() {
|
2022-07-18 20:39:16 +00:00
|
|
|
const cmdArgs = this.command;
|
2022-05-05 12:35:47 +00:00
|
|
|
const cmd = cmdArgs.shift();
|
|
|
|
this.proc = cp.spawn(cmd, cmdArgs);
|
|
|
|
|
|
|
|
// Set Encoding
|
|
|
|
this.proc.stdout.setEncoding("utf8");
|
|
|
|
this.proc.stderr.setEncoding("utf8");
|
|
|
|
|
|
|
|
// Process Events
|
|
|
|
this.proc.stdout.on("data", (d) => this.report(d.toString(), OUT));
|
|
|
|
this.proc.stderr.on("data", (d) => this.report(d.toString(), ERR));
|
|
|
|
this.proc.on("close", this.onProcClose);
|
|
|
|
}
|
|
|
|
|
|
|
|
runJob() {
|
2022-08-14 19:23:37 +00:00
|
|
|
this.socket = io(this.url, {
|
2022-07-18 20:39:16 +00:00
|
|
|
query: { mode: this.mode, jobId: this.jobId },
|
2022-05-05 12:35:47 +00:00
|
|
|
});
|
|
|
|
this.socket.on("connect", this.spawn);
|
|
|
|
this.socket.on("disconnect", this.onClose);
|
|
|
|
}
|
|
|
|
|
|
|
|
onClose() {
|
|
|
|
console.log("Server disconnected, terminating process.");
|
2022-07-19 01:39:57 +00:00
|
|
|
if (this.proc) this.proc.kill("SIGKILL");
|
2022-05-05 12:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onProcClose(code) {
|
2022-08-12 20:52:58 +00:00
|
|
|
this.socket.emit(events.JOB_CLS, code, () => this.socket.disconnect());
|
2022-05-05 12:35:47 +00:00
|
|
|
console.log(`Process finished with code ${code}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
report(d, dType) {
|
|
|
|
this.buf[dType] += d;
|
|
|
|
if (!this.buf[dType].includes("\n")) return;
|
2022-05-17 12:32:04 +00:00
|
|
|
if (this.buf[dType].endsWith("\n"))
|
|
|
|
this.buf[dType] = this.buf[dType].slice(0, -1);
|
2022-05-05 12:35:47 +00:00
|
|
|
this.socket.emit(events.JOB_REP, this.buf[dType]);
|
|
|
|
if (dType === ERR) console.error(`err: ${this.buf[dType]}`);
|
|
|
|
else console.log(`out: ${this.buf[dType]}`);
|
|
|
|
this.buf[dType] = "";
|
|
|
|
}
|
|
|
|
}
|