Minor Adjustments
This commit is contained in:
parent
1084f5d937
commit
f486d50efa
60 changed files with 1965 additions and 127 deletions
76
libold/common/sockets/clients/Executor.js
Normal file
76
libold/common/sockets/clients/Executor.js
Normal 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] = "";
|
||||
}
|
||||
}
|
105
libold/common/sockets/clients/Initiator.js
Normal file
105
libold/common/sockets/clients/Initiator.js
Normal 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 });
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
26
libold/common/sockets/clients/Viewer.js
Normal file
26
libold/common/sockets/clients/Viewer.js
Normal 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;
|
||||
}
|
||||
}
|
5
libold/common/sockets/clients/index.js
Normal file
5
libold/common/sockets/clients/index.js
Normal 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";
|
3
libold/common/sockets/clients/web.index.js
Normal file
3
libold/common/sockets/clients/web.index.js
Normal file
|
@ -0,0 +1,3 @@
|
|||
export { default as Initiator } from "./Initiator.js";
|
||||
|
||||
export { default as Viewer } from "./Viewer.js";
|
15
libold/common/sockets/events.js
Normal file
15
libold/common/sockets/events.js
Normal file
|
@ -0,0 +1,15 @@
|
|||
const JOB_REP = "jr"; // Job Report Event
|
||||
const JOB_LOG = "jl"; // Job Log Event
|
||||
const JOB_CLS = "jc"; // Job Close Event
|
||||
const JOB_CRT = "jcr"; // Job Create Event
|
||||
const PPL_TRG = "plr"; // Pipeline Trigger Event
|
||||
const ERR = "e"; // Socket Error
|
||||
|
||||
export default {
|
||||
JOB_REP,
|
||||
JOB_LOG,
|
||||
JOB_CLS,
|
||||
JOB_CRT,
|
||||
PPL_TRG,
|
||||
ERR,
|
||||
};
|
8
libold/common/sockets/modes.js
Normal file
8
libold/common/sockets/modes.js
Normal file
|
@ -0,0 +1,8 @@
|
|||
const INIT = "i"; // Intiator Socket
|
||||
const EXEC = "e"; // Execution Socket
|
||||
const VIEW = "v"; // View Socket
|
||||
export default {
|
||||
INIT,
|
||||
EXEC,
|
||||
VIEW,
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue