From 70cf75b94d5ad6f81b91748fa8adb67f682a7da5 Mon Sep 17 00:00:00 2001 From: Minzkraut Date: Tue, 10 Jan 2023 23:01:42 +0100 Subject: [PATCH] Add route to request a batch of jobs --- index.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/index.js b/index.js index 91a1c46..5f7aaba 100644 --- a/index.js +++ b/index.js @@ -49,6 +49,20 @@ app.post("/jobs", (req, res) => { console.log(`Queued Job ${jobId}`); res.json({ 'jobId': jobId }); }); + +app.get("/batch", async (req, res) => { + let queued = Object.values(jobs['queued']); + const batchSize = req.query.size || Math.min(queued.length, 10); + let assignedJobs = [] + for (let index = 0; index < batchSize; index++) { + let job = queued[index] + assignedJobs.push(job); + jobs['waiting'][job['jobId']] = job; + delete jobs['queued'][job['jobId']]; + console.log(`Handed out Job ${job['jobId']}`); + } + res.json({ 'count': assignedJobs.length, 'jobs': assignedJobs }); +}); app.listen(PORT, () => { console.log("Job Server running") }) \ No newline at end of file