Properly handle closing an image server connection, switch some "on" instances to "once"

This commit is contained in:
TheEssem 2021-05-06 16:40:05 -05:00
parent 8e7764fc57
commit e8834c072a
3 changed files with 12 additions and 7 deletions

View File

@ -185,11 +185,11 @@ const runJob = (job, sock) => {
reject(new TypeError("Unknown image type"));
}
log(`Job ${job.uuid} started`, job.num);
const worker = new Worker(path.join(__dirname, "../utils/image-runner.js"), {
workerData: object
});
worker.on("message", (data) => {
log(`Job ${job.uuid} started`, job.num);
worker.once("message", (data) => {
log(`Sending result of job ${job.uuid} back to the bot`, job.num);
const jobObject = jobs.get(job.uuid);
jobObject.data = data.buffer;
@ -200,7 +200,7 @@ const runJob = (job, sock) => {
return resolve();
});
});
worker.on("error", reject);
worker.once("error", reject);
/*run(object).then((data) => {
log(`Sending result of job ${job.uuid} back to the bot`, job.num);
const jobObject = jobs.get(job.uuid);

View File

@ -4,7 +4,6 @@ const Command = require("../../classes/command.js");
class ChannelCommand extends Command {
async run() {
if (!this.message.channel.guild) return `${this.message.author.mention}, this command only works in servers!`;
console.log(this.message.member.permission);
if (!this.message.member.permission.has("administrator") && this.message.member.id !== process.env.OWNER) return `${this.message.author.mention}, you need to be an administrator to enable/disable me!`;
if (this.args.length === 0) return `${this.message.author.mention}, you need to provide whether I should be enabled or disabled in this channel!`;
if (this.args[0] !== "disable" && this.args[0] !== "enable") return `${this.message.author.mention}, that's not a valid option!`;

View File

@ -93,7 +93,13 @@ exports.connect = (server) => {
}
});
connection.on("error", (e) => {
console.error(e);
logger.error(e);
});
connection.once("close", () => {
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"));
}
this.connections.filter((val) => val !== connection);
});
this.connections.push(connection);
resolve();
@ -251,13 +257,13 @@ exports.run = object => {
const worker = new Worker(path.join(__dirname, "image-runner.js"), {
workerData: object
});
worker.on("message", (data) => {
worker.once("message", (data) => {
resolve({
buffer: Buffer.from([...data.buffer]),
type: data.fileExtension
});
});
worker.on("error", reject);
worker.once("error", reject);
}
});
};