Revamp Job flow
This commit is contained in:
parent
945afdfbbe
commit
4a0a4b29a5
86 changed files with 592 additions and 608 deletions
105
lib/common/sockets/clients/Initiator.js
Normal file
105
lib/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 });
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue