Class commands, improved sharding, and many other changes (#88)

* Load commands recursively

* Sort commands

* Missed a couple of spots

* missed even more spots apparently

* Ported commands in "fun" category to new class-based format, added babel eslint plugin

* Ported general commands, removed old/unneeded stuff, replaced moment with day, many more fixes I lost track of

* Missed a spot

* Removed unnecessary abort-controller package, add deprecation warning for mongo database

* Added imagereload, clarified premature end message

* Fixed docker-compose path issue, added total bot uptime to stats, more fixes for various parts

* Converted image commands into classes, fixed reload, ignore another WS event, cleaned up command handler and image runner

* Converted music/soundboard commands to class format

* Cleanup unnecessary logs

* awful tag command class port

* I literally somehow just learned that you can leave out the constructor in classes

* Pass client directly to commands/events, cleaned up command handler

* Migrated bot to eris-sharder, fixed some error handling stuff

* Remove unused modules

* Fixed type returning

* Switched back to Eris stable

* Some fixes and cleanup

* might wanna correct this

* Implement image command ratelimiting

* Added Bot token prefix, added imagestats, added running endpoint to API
This commit is contained in:
Essem 2021-04-12 11:16:12 -05:00 committed by GitHub
parent ff8a24d0e8
commit 40223ec8b5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
291 changed files with 5296 additions and 5171 deletions

View file

@ -30,19 +30,16 @@ const acceptJob = async (uuid, sock) => {
msg: jobs[uuid].msg,
num: jobs[uuid].num
}, sock);
jobAmount--;
if (queue.length > 0) {
acceptJob(queue[0], sock);
}
log(`Job ${uuid} has finished`);
} catch (err) {
console.error(`Error on job ${uuid}:`, err);
delete jobs[uuid];
sock.write(Buffer.concat([Buffer.from([0x2]), Buffer.from(uuid), Buffer.from(err.message)]));
} finally {
jobAmount--;
if (queue.length > 0) {
acceptJob(queue[0], sock);
}
delete jobs[uuid];
sock.write(Buffer.concat([Buffer.from([0x2]), Buffer.from(uuid), Buffer.from(err.toString())]));
}
};
@ -55,6 +52,22 @@ const httpServer = http.createServer((req, res) => {
if (reqUrl.pathname === "/status") {
log(`Sending server status to ${req.socket.remoteAddress}:${req.socket.remotePort} via HTTP`);
return res.end(Buffer.from((MAX_JOBS - jobAmount).toString()));
} else if (reqUrl.pathname === "/running") {
log(`Sending currently running jobs to ${req.socket.remoteAddress}:${req.socket.remotePort} via HTTP`);
const keys = Object.keys(jobs);
const newObject = { queued: queue.length, runningJobs: jobAmount, max: MAX_JOBS };
for (const key of keys) {
const validKeys = Object.keys(jobs[key]).filter((value) => value !== "addr" && value !== "port" && value !== "data" && value !== "ext");
newObject[key] = {};
for (const validKey of validKeys) {
if (validKey === "msg") {
newObject[key][validKey] = JSON.parse(jobs[key][validKey]);
} else {
newObject[key][validKey] = jobs[key][validKey];
}
}
}
return res.end(JSON.stringify(newObject));
} else if (reqUrl.pathname === "/image") {
if (!reqUrl.searchParams.has("id")) {
res.statusCode = 400;
@ -134,7 +147,7 @@ const server = net.createServer((sock) => { // Create a TCP socket/server to lis
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")]));
sock.write(Buffer.concat([Buffer.from([0x2]), Buffer.from(job), Buffer.from("Job ended prematurely (not really an error; just run your image job again)")]));
}
}
process.exit(0);
@ -149,28 +162,22 @@ server.listen(8080, () => {
log("TCP listening on port 8080");
});
const runJob = (job, sock) => {
return new Promise((resolve, reject) => {
log(`Job ${job.uuid} starting...`, job.num);
const runJob = async (job, sock) => {
log(`Job ${job.uuid} starting...`, job.num);
const object = JSON.parse(job.msg);
// If the image has a path, it must also have a type
if (object.path && !object.type) {
reject(new TypeError("Unknown image type"));
}
const object = JSON.parse(job.msg);
// If the image has a path, it must also have a type
if (object.path && !object.type) {
throw new TypeError("Unknown image type");
}
log(`Job ${job.uuid} started`, job.num);
run(object).then((data) => {
log(`Sending result of job ${job.uuid} back to the bot`, job.num);
jobs[job.uuid].data = data.buffer;
jobs[job.uuid].ext = data.fileExtension;
sock.write(Buffer.concat([Buffer.from([0x1]), Buffer.from(job.uuid)]), (e) => {
if (e) return reject(e);
return resolve();
});
return;
}).catch(e => {
reject(e);
});
log(`Job ${job.uuid} started`, job.num);
const data = await run(object).catch(e => { throw e; });
log(`Sending result of job ${job.uuid} back to the bot`, job.num);
jobs[job.uuid].data = data.buffer;
jobs[job.uuid].ext = data.fileExtension;
sock.write(Buffer.concat([Buffer.from([0x1]), Buffer.from(job.uuid)]), (e) => {
if (e) throw e;
});
return;
};