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 {
|
|
|
|
constructor(url, job, options = {}) {
|
|
|
|
this.url = url;
|
|
|
|
this.job = job;
|
|
|
|
this.mode = modes.EXEC;
|
|
|
|
|
|
|
|
// Internal Buffer
|
|
|
|
this.buf = {};
|
|
|
|
this.buf[ERR] = "";
|
|
|
|
this.buf[OUT] = "";
|
|
|
|
|
|
|
|
// Methods
|
|
|
|
this.spawn = options.spawn ?? this.spawn.bind(this);
|
|
|
|
this.report = options.report ?? this.report.bind(this);
|
|
|
|
this.onProcClose = options.onProcClose ?? this.onProcClose.bind(this);
|
|
|
|
this.onClose = options.onClose ?? this.onClose.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
spawn() {
|
2022-05-07 13:44:54 +00:00
|
|
|
const cmdArgs = this.job.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, {
|
|
|
|
query: { mode: this.mode, jobId: this.job.id },
|
|
|
|
});
|
|
|
|
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-12 22:06:33 +00:00
|
|
|
this.proc.kill("SIGKILL");
|
2022-05-05 12:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onProcClose(code) {
|
|
|
|
this.socket.emit(events.JOB_CLS, code);
|
|
|
|
console.log(`Process finished with code ${code}`);
|
|
|
|
this.socket.disconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
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] = "";
|
|
|
|
}
|
|
|
|
}
|