qualiteer/lib/sockets/clients/Initiator.js

97 lines
2.9 KiB
JavaScript
Raw Normal View History

2022-05-05 12:35:47 +00:00
import { Manager } 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}`));
2022-07-12 02:44:44 +00:00
this.onPipelineClose =
options.onPipelineClose ??
(() => {
console.log("job pipeline closed");
});
2022-08-08 11:42:28 +00:00
this.sk = null;
2022-05-05 12:35:47 +00:00
}
async newJob(jobRequest, onLog, onClose, onCreate) {
const mgr = new Manager(this.url, {
query: { mode: this.mode, job: JSON.stringify(jobRequest) },
});
onLog = onLog ?? this.onLog.bind(this);
onClose = onClose ?? this.onClose.bind(this);
onCreate = onCreate ?? this.onCreate.bind(this);
2022-08-08 11:42:28 +00:00
this.sk = mgr.socket("/");
this.sk.on(events.JOB_LOG, onLog);
this.sk.on(events.JOB_CLS, onClose);
2022-05-05 12:35:47 +00:00
return new Promise((res) =>
2022-08-08 11:42:28 +00:00
this.sk.on(events.JOB_CRT, function onJobCreate(id) {
2022-05-05 12:35:47 +00:00
onCreate(id);
res({ ...jobRequest, id });
})
);
}
2022-07-12 02:44:44 +00:00
async newPipelineJob(
jobRequest,
onLog,
onClose,
onCreate,
onPipelineTrigger,
onPipelineClose
) {
const mgr = new Manager(this.url, {
query: { mode: this.mode, job: JSON.stringify(jobRequest) },
});
onLog = onLog ?? this.onLog.bind(this);
onClose = onClose ?? this.onClose.bind(this);
onCreate = onCreate ?? this.onCreate.bind(this);
onPipelineTrigger =
onPipelineTrigger ??
2022-08-09 04:29:10 +00:00
((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,
2022-08-09 14:07:53 +00:00
pipeline: {...pipeline, triggers: triggers[testName],__test:testName },
2022-08-09 04:29:10 +00:00
};
setTimeout(
() =>
this.newPipelineJob(
jobReq,
onLog,
onClose,
onCreate,
onPipelineTrigger,
onPipelineClose
),
delay
);
}
2022-07-12 02:44:44 +00:00
});
onPipelineClose = onPipelineClose ?? this.onPipelineClose.bind(this);
2022-08-08 11:42:28 +00:00
this.sk = mgr.socket("/");
this.sk.on(events.JOB_LOG, onLog);
this.sk.on(events.JOB_CLS, onClose);
this.sk.on(events.PPL_TRG, onPipelineTrigger);
this.sk.on(events.PPL_CLS, onPipelineClose);
2022-07-12 02:44:44 +00:00
return new Promise((res) =>
2022-08-08 11:42:28 +00:00
this.sk.on(events.JOB_CRT, function onJobCreate(id) {
2022-07-12 02:44:44 +00:00
onCreate(id);
res({ ...jobRequest, id });
})
);
}
2022-05-05 12:35:47 +00:00
}