Fixed potential issue where all jobs could be deleted

This commit is contained in:
TheEssem 2021-05-06 16:58:35 -05:00
parent e8834c072a
commit 198b6dbcac

View file

@ -71,7 +71,7 @@ exports.connect = (server) => {
const uuid = msg.slice(1, 37).toString(); const uuid = msg.slice(1, 37).toString();
if (opcode === 0x00) { // Job queued if (opcode === 0x00) { // Job queued
if (jobs[req]) { if (jobs[req]) {
jobs[req].emit("uuid", uuid); jobs[req].event.emit("uuid", uuid);
} }
} else if (opcode === 0x01) { // Job completed successfully } else if (opcode === 0x01) { // Job completed successfully
// the image API sends all job responses over the same socket; make sure this is ours // the image API sends all job responses over the same socket; make sure this is ours
@ -81,11 +81,11 @@ exports.connect = (server) => {
// The response data is given as the file extension/ImageMagick type of the image (e.g. "png"), followed // The response data is given as the file extension/ImageMagick type of the image (e.g. "png"), followed
// by a newline, followed by the image data. // by a newline, followed by the image data.
jobs[uuid].emit("image", image, imageReq.headers.get("ext")); jobs[uuid].event.emit("image", image, imageReq.headers.get("ext"));
} }
} else if (opcode === 0x02) { // Job errored } else if (opcode === 0x02) { // Job errored
if (jobs[uuid]) { if (jobs[uuid]) {
jobs[uuid].emit("error", new Error(req)); jobs[uuid].event.emit("error", new Error(req));
} }
} else if (opcode === 0x03) { } else if (opcode === 0x03) {
// we use the uuid part here because queue info requests don't respond with one // we use the uuid part here because queue info requests don't respond with one
@ -97,7 +97,7 @@ exports.connect = (server) => {
}); });
connection.once("close", () => { connection.once("close", () => {
for (const uuid of Object.keys(jobs)) { for (const uuid of Object.keys(jobs)) {
jobs[uuid].emit("error", new Error("Job ended prematurely due to a closed connection; please run your image job again")); if (jobs[uuid].addr === connection.remoteAddress) jobs[uuid].event.emit("error", new Error("Job ended prematurely due to a closed connection; please run your image job again"));
} }
this.connections.filter((val) => val !== connection); this.connections.filter((val) => val !== connection);
}); });
@ -111,7 +111,7 @@ exports.disconnect = async () => {
connection.destroy(); connection.destroy();
} }
for (const uuid of Object.keys(jobs)) { for (const uuid of Object.keys(jobs)) {
jobs[uuid].emit("error", "Job ended prematurely (not really an error; just run your image job again)"); jobs[uuid].event.emit("error", "Job ended prematurely (not really an error; just run your image job again)");
} }
this.connections = []; this.connections = [];
return; return;
@ -164,21 +164,21 @@ const start = (object, num) => {
reject(err); reject(err);
} }
} else { } else {
resolve(); resolve(currentServer.remoteAddress);
} }
}); });
}); });
}).then(() => { }).then((addr) => {
const event = new EventEmitter(); const event = new EventEmitter();
return new Promise((resolve) => { return new Promise((resolve) => {
event.once("uuid", (uuid) => resolve({ event, uuid })); event.once("uuid", (uuid) => resolve({ event, uuid, addr }));
jobs[num] = event; jobs[num] = { event, addr };
}); });
}, (result) => { }, (result) => {
throw result; throw result;
}).then(data => { }).then(data => {
delete jobs[num]; delete jobs[num];
jobs[data.uuid] = data.event; jobs[data.uuid] = { event: data.event, addr: data.addr };
return { uuid: data.uuid, event: data.event }; return { uuid: data.uuid, event: data.event };
}); });
}; };