From 43e0f7f4476abccf0ea8d6be44093cdcf2d2de9d Mon Sep 17 00:00:00 2001 From: TheEssem Date: Wed, 31 Mar 2021 21:44:13 -0500 Subject: [PATCH] Send error back to API client when the API server stops --- api/index.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/api/index.js b/api/index.js index b83c50f..518974a 100644 --- a/api/index.js +++ b/api/index.js @@ -12,7 +12,7 @@ const log = (msg, jobNum) => { }; const jobs = {}; -// Should look like UUID : { addr : "someaddr", port: someport msg: "request" } +// Should look like UUID : { addr : "someaddr", port: someport, msg: "request" } const queue = []; // Array of UUIDs @@ -127,6 +127,18 @@ const server = net.createServer((sock) => { // Create a TCP socket/server to lis sock.on("end", () => { log(`TCP client ${sock.remoteAddress}:${sock.remotePort} has disconnected`); }); + + // handle ctrl+c and pm2 stop + process.on("SIGINT", () => { + console.log("SIGINT detected, shutting down..."); + for (const job of Object.keys(jobs)) { + if (jobs[job].addr === sock.remoteAddress && jobs[job].port === sock.remotePort) { + delete jobs[job]; + sock.write(Buffer.concat([Buffer.from([0x2]), Buffer.from(job), Buffer.from("Job ended prematurely")])); + } + } + process.exit(0); + }); }); server.on("error", (e) => {