qualiteer/lib/sockets/clients/Executor.js

79 lines
2.2 KiB
JavaScript
Raw Normal View History

2022-05-05 12:35:47 +00:00
import { Manager } from "socket.io-client";
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) {
this.url = config.url(payload) ?? process.env.QUALITEER_URL;
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() {
const mgr = new Manager(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 = mgr.socket("/");
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-08-12 20:52:58 +00:00
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] = "";
}
}