Minor Adjustments

This commit is contained in:
Dunemask 2023-03-19 13:53:37 -04:00 committed by dunemask
parent ba8e6ded26
commit a90c28dd76
60 changed files with 8240 additions and 8 deletions

View file

@ -0,0 +1,76 @@
import io 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(config, payload) {
this.url = config.url(payload) ?? process.env.QUALITEER_EXECUTOR_URL;
this.jobId = config.jobId(payload) ?? process.env.QUALITEER_JOB_ID;
this.command = config.command(payload) ?? process.env.QUALITEER_COMMAND;
this.mode = modes.EXEC;
// Internal Buffer
this.buf = {};
this.buf[ERR] = "";
this.buf[OUT] = "";
// Methods
this.spawn = this.spawn.bind(this);
this.report = this.report.bind(this);
this.onProcClose = this.onProcClose.bind(this);
this.onClose = this.onClose.bind(this);
}
spawn() {
const cmdArgs = this.command;
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() {
this.socket = io(this.url, {
query: { mode: this.mode, jobId: this.jobId },
});
this.socket.on("connect", this.spawn);
this.socket.on("disconnect", this.onClose);
}
onClose() {
console.log("Server disconnected, terminating process.");
if (this.proc) this.proc.kill("SIGKILL");
}
onProcClose(code) {
this.socket.emit(events.JOB_CLS, code, () => this.socket.disconnect());
console.log(`Process finished with code ${code}`);
}
report(d, dType) {
this.buf[dType] += d;
if (!this.buf[dType].includes("\n")) return;
if (this.buf[dType].endsWith("\n"))
this.buf[dType] = this.buf[dType].slice(0, -1);
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] = "";
}
}

View file

@ -0,0 +1,105 @@
import { io } from "socket.io-client";
import modes from "../modes.js";
import events from "../events.js";
export { default as events } from "../events.js";
export { default as modes } from "../modes.js";
export default class Initiator {
constructor(url, options = {}) {
this.url = url;
this.mode = modes.INIT;
this.onLog = options.onLog ?? ((d) => console.log(`job: ${d}`));
this.onClose = options.onClose ?? (() => {});
this.onCreate = options.onCreate ?? ((id) => console.log(`job id: ${id}`));
this.onPipelineClose =
options.onPipelineClose ??
(() => {
console.log("job pipeline closed");
});
this.sk = null;
}
async newJob(jobRequest, onLog, onClose, onCreate) {
onLog = onLog ?? this.onLog.bind(this);
onClose = onClose ?? this.onClose.bind(this);
onCreate = onCreate ?? this.onCreate.bind(this);
const sk = io(this.url, {
query: { mode: this.mode, job: JSON.stringify(jobRequest) },
});
sk.on(events.JOB_LOG, onLog);
sk.on(events.JOB_CLS, function onJobClose(c) {
sk.disconnect();
onClose(c);
});
this.sk = sk;
return new Promise((res) =>
sk.on(events.JOB_CRT, function onJobCreate(id) {
onCreate(id);
res({ ...jobRequest, id });
})
);
}
async newPipelineJob(
jobRequest,
onLog,
onClose,
onCreate,
onPipelineTrigger,
onPipelineClose
) {
onLog = onLog ?? this.onLog.bind(this);
onClose = onClose ?? this.onClose.bind(this);
onCreate = onCreate ?? this.onCreate.bind(this);
onPipelineTrigger =
onPipelineTrigger ??
((pipeline) => {
console.log("job trg:", pipeline);
const { triggers } = pipeline;
if (!Object.keys(triggers).length) onPipelineClose();
// For each trigger
for (var testName in triggers) {
const delay = triggers[testName].__testDelay ?? 0;
delete triggers[testName].__testDelay;
const jobReq = {
...jobRequest,
pipeline: {
...pipeline,
triggers: triggers[testName],
__test: testName,
},
};
setTimeout(
() =>
this.newPipelineJob(
jobReq,
onLog,
onClose,
onCreate,
onPipelineTrigger,
onPipelineClose
),
delay
);
}
});
onPipelineClose = onPipelineClose ?? this.onPipelineClose.bind(this);
const sk = io(this.url, {
query: { mode: this.mode, job: JSON.stringify(jobRequest) },
});
sk.on(events.JOB_LOG, onLog);
sk.on(events.JOB_CLS, function onJobClose(c) {
sk.disconnect();
onClose(c);
});
sk.on(events.PPL_TRG, onPipelineTrigger);
this.sk = sk;
return new Promise((res) =>
sk.on(events.JOB_CRT, function onJobCreate(id) {
onCreate(id);
res({ ...jobRequest, id });
})
);
}
}

View file

@ -0,0 +1,26 @@
import io from "socket.io-client";
import modes from "../modes.js";
import events from "../events.js";
export { default as events } from "../events.js";
export { default as modes } from "../modes.js";
export default class Viewer {
constructor(url, options = {}) {
this.url = url;
this.mode = modes.VIEW;
this.onLog = options.onLog ?? console.log;
this.onClose = options.onClose ?? (() => {});
}
viewJob(jobId, onLog, onClose) {
onLog = onLog ?? this.onLog.bind(this);
onClose = onClose ?? this.onClose.bind(this);
const sk = io(this.url, {
query: { mode: this.mode, jobId },
});
sk.on(events.JOB_LOG, onLog);
sk.on(events.JOB_CLS, onClose);
return sk;
}
}

View file

@ -0,0 +1,5 @@
export { default as Initiator } from "./Initiator.js";
export { default as Viewer } from "./Viewer.js";
export { default as Executor } from "./Executor.js";

View file

@ -0,0 +1,3 @@
export { default as Initiator } from "./Initiator.js";
export { default as Viewer } from "./Viewer.js";