qualiteer/lib/server/core/JobManager.js

72 lines
1.9 KiB
JavaScript
Raw Normal View History

2022-07-18 21:43:10 +00:00
import { v4 } from "uuid";
2022-10-15 11:47:47 +00:00
import { getTest } from "../database/queries/catalog.js";
import applyJobInternally from "../k8s/k8s-internal.js";
import applyJob from "../k8s/k8s.js";
2022-07-18 21:43:10 +00:00
const maxJobs = process.env.MAX_JOBS ? parseInt(process.env.MAX_JOBS) : 3;
2022-08-21 00:10:36 +00:00
const internalDeploy = process.env.INTERNAL_DEPLOY === "true";
const launchJob = internalDeploy ? applyJobInternally : applyJob;
2022-07-18 21:43:10 +00:00
2022-10-15 11:47:47 +00:00
async function getTests(job) {
if (job.pipeline) return [await getTest(job.pipeline.__test)];
if (!job.testNames) return [];
const tests = await Promise.all(job.testNames.map((name) => getTest(name)));
return tests;
}
2022-07-18 21:43:10 +00:00
class JobManager {
constructor() {
this.clientMaxJobs = maxJobs;
this.clients = {};
}
getJob(clientId, jobId) {
return this.clients[clientId].jobs.find((j) => j.id === jobId);
}
getJobById(jobId) {
for (var client of Object.values(this.clients)) {
const job = client.jobs.find((j) => j.id === jobId);
if (!job) continue;
return job;
}
}
pushLog(jobId, log) {
const job = this.getJobById(jobId);
2022-10-08 17:47:46 +00:00
if (!job) return;
2022-07-18 21:43:10 +00:00
if (log instanceof Array) job.log.push(...log);
else job.log.push(log);
}
closeJob(jobId, exitcode) {
const job = this.getJobById(jobId);
2022-10-15 11:47:47 +00:00
if (!job) return;
2022-07-18 21:43:10 +00:00
job.exitcode = exitcode;
}
2022-10-15 11:47:47 +00:00
async newJob(jobRequest, id) {
2022-07-18 21:43:10 +00:00
if (!jobRequest) throw Error("Request Must Be Object!");
if (!this.clients[id]) this.clients[id] = { jobs: [] };
2022-10-15 11:47:47 +00:00
const job = { ...jobRequest };
job.image = "registry.dunemask.net/garden/dev/reed:latest";
2022-07-18 21:43:10 +00:00
job.id = v4();
job.log = [];
this.clients[id].jobs.push(job);
2022-10-15 11:47:47 +00:00
job.dashboardSocketId = id;
job.tests = await getTests(job);
for (var t of job.tests) if (!t) throw Error("1 or more tests not found!");
2022-08-21 00:10:36 +00:00
launchJob(job);
2022-07-18 21:43:10 +00:00
return { ...job };
}
removeJob(clientId, id) {
this.clients[clientId].jobs = this.clients[clientId].jobs.filter(
(j) => j.id !== id
);
}
}
export default new JobManager();