Add queued image jobs to server selection logic

This commit is contained in:
Essem 2021-09-01 00:21:13 -05:00
parent 2421c310e3
commit fec3d7303c
No known key found for this signature in database
GPG key ID: 7D497397CC3A2A8C
2 changed files with 12 additions and 3 deletions

View file

@ -119,7 +119,11 @@ httpServer.on("request", async (req, res) => {
const reqUrl = new URL(req.url, `http://${req.headers.host}`); const reqUrl = new URL(req.url, `http://${req.headers.host}`);
if (reqUrl.pathname === "/status" && req.method === "GET") { if (reqUrl.pathname === "/status" && req.method === "GET") {
log(`Sending server status to ${req.socket.remoteAddress}:${req.socket.remotePort} via HTTP`); log(`Sending server status to ${req.socket.remoteAddress}:${req.socket.remotePort} via HTTP`);
return res.end(Buffer.from((MAX_JOBS - jobAmount).toString())); const statusObject = {
load: MAX_JOBS - jobAmount,
queued: queue.length
};
return res.end(JSON.stringify(statusObject));
} else if (reqUrl.pathname === "/running" && req.method === "GET") { } else if (reqUrl.pathname === "/running" && req.method === "GET") {
log(`Sending currently running jobs to ${req.socket.remoteAddress}:${req.socket.remotePort} via HTTP`); log(`Sending currently running jobs to ${req.socket.remoteAddress}:${req.socket.remotePort} via HTTP`);
const keys = jobs.keys(); const keys = jobs.keys();

View file

@ -87,6 +87,10 @@ class ImageWorker extends BaseServiceWorker {
if (ideal.length === 0) throw "No available servers"; if (ideal.length === 0) throw "No available servers";
const sorted = ideal.sort((a, b) => { const sorted = ideal.sort((a, b) => {
return b.load - a.load; return b.load - a.load;
}).filter((e, i, array) => {
return !(e.load < array[0].load);
}).sort((a, b) => {
return a.queued - b.queued;
}); });
return sorted[0]; return sorted[0];
} }
@ -123,11 +127,12 @@ class ImageWorker extends BaseServiceWorker {
} }
}); });
clearTimeout(timeout); clearTimeout(timeout);
const status = await statusRequest.text(); const status = await statusRequest.json();
serversLeft--; serversLeft--;
idealServers.push({ idealServers.push({
addr: address, addr: address,
load: parseInt(status) load: status.load,
queued: status.queued
}); });
} catch (e) { } catch (e) {
if (e.name === "AbortError") { if (e.name === "AbortError") {