Send error back to API client when the API server stops
This commit is contained in:
parent
2d5eef2c8c
commit
43e0f7f447
1 changed files with 13 additions and 1 deletions
14
api/index.js
14
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) => {
|
||||
|
|
Loading…
Reference in a new issue