import { v4 } from "uuid"; import { getTest } from "../database/queries/catalog.js"; import applyJobInternally from "../k8s/k8s-internal.js"; import applyJob from "../k8s/k8s.js"; const maxJobs = process.env.MAX_JOBS ? parseInt(process.env.MAX_JOBS) : 3; const internalDeploy = process.env.INTERNAL_DEPLOY === "true"; const launchJob = internalDeploy ? applyJobInternally : applyJob; 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; } 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); if (!job) return; if (log instanceof Array) job.log.push(...log); else job.log.push(log); } closeJob(jobId, exitcode) { const job = this.getJobById(jobId); if (!job) return; job.exitcode = exitcode; } async newJob(jobRequest, id) { if (!jobRequest) throw Error("Request Must Be Object!"); if (!this.clients[id]) this.clients[id] = { jobs: [] }; const job = { ...jobRequest }; job.image = "registry.dunemask.net/garden/dev/reed:latest"; job.id = v4(); job.log = []; this.clients[id].jobs.push(job); job.dashboardSocketId = id; job.tests = await getTests(job); for (var t of job.tests) if (!t) throw Error("1 or more tests not found!"); launchJob(job); return { ...job }; } removeJob(clientId, id) { this.clients[clientId].jobs = this.clients[clientId].jobs.filter( (j) => j.id !== id ); } } export default new JobManager();