Rework image API again, replaced many calls to replace with replaceAll
This commit is contained in:
parent
b2b8fd643a
commit
62346cbae4
15 changed files with 271 additions and 185 deletions
162
api/index.js
162
api/index.js
|
@ -4,8 +4,7 @@ require("dotenv").config();
|
|||
const os = require("os");
|
||||
const { run } = require("../utils/image-runner.js");
|
||||
const net = require("net");
|
||||
const dgram = require("dgram"); // for UDP servers
|
||||
const socket = dgram.createSocket("udp4"); // Our universal UDP socket, this might cause issues and we may have to use a seperate socket for each connection
|
||||
const http = require("http");
|
||||
|
||||
const start = process.hrtime();
|
||||
const log = (msg, jobNum) => {
|
||||
|
@ -22,74 +21,123 @@ const { v4: uuidv4 } = require("uuid");
|
|||
const MAX_JOBS = process.env.JOBS !== "" && process.env.JOBS !== undefined ? parseInt(process.env.JOBS) : os.cpus().length * 4; // Completely arbitrary, should usually be some multiple of your amount of cores
|
||||
let jobAmount = 0;
|
||||
|
||||
const acceptJob = async (uuid) => {
|
||||
const acceptJob = async (uuid, sock) => {
|
||||
jobAmount++;
|
||||
queue.shift();
|
||||
try {
|
||||
await runJob({
|
||||
uuid: uuid,
|
||||
msg: jobs[uuid].msg,
|
||||
addr: jobs[uuid].addr,
|
||||
port: jobs[uuid].port,
|
||||
num: jobs[uuid].num
|
||||
});
|
||||
}, sock);
|
||||
jobAmount--;
|
||||
if (queue.length > 0) {
|
||||
acceptJob(queue[0]);
|
||||
}
|
||||
delete jobs[uuid];
|
||||
log(`Job ${uuid} has finished`);
|
||||
} catch (err) {
|
||||
console.error(`Error on job ${uuid}:`, err);
|
||||
socket.send(Buffer.concat([Buffer.from([0x2]), Buffer.from(uuid), Buffer.from(err.toString())]), jobs[uuid].port, jobs[uuid].addr);
|
||||
jobAmount--;
|
||||
if (queue.length > 0) {
|
||||
acceptJob(queue[0]);
|
||||
}
|
||||
delete jobs[uuid];
|
||||
sock.write(Buffer.concat([Buffer.from([0x2]), Buffer.from(uuid), Buffer.from(err.toString())]));
|
||||
}
|
||||
};
|
||||
|
||||
const server = dgram.createSocket("udp4"); //Create a UDP server for listening to requests, we dont need tcp
|
||||
server.on("message", (msg, rinfo) => {
|
||||
const opcode = msg.readUint8(0);
|
||||
const req = msg.toString().slice(1,msg.length);
|
||||
// 0x0 == Cancel job
|
||||
// 0x1 == Queue job
|
||||
// 0x2 == Get CPU usage
|
||||
if (opcode == 0x0) {
|
||||
delete queue[queue.indexOf(req) - 1];
|
||||
delete jobs[req];
|
||||
} else if (opcode == 0x1) {
|
||||
const job = { addr: rinfo.address, port: rinfo.port, msg: req, num: jobAmount };
|
||||
const uuid = uuidv4();
|
||||
jobs[uuid] = job;
|
||||
queue.push(uuid);
|
||||
|
||||
if (jobAmount < MAX_JOBS) {
|
||||
log(`Got request for job ${job.msg} with id ${uuid}`, job.num);
|
||||
acceptJob(uuid);
|
||||
} else {
|
||||
log(`Got request for job ${job.msg} with id ${uuid}, queued in position ${queue.indexOf(uuid)}`, job.num);
|
||||
const httpServer = http.createServer((req, res) => {
|
||||
if (req.method !== "GET") {
|
||||
res.statusCode = 405;
|
||||
return res.end("405 Method Not Allowed");
|
||||
}
|
||||
const reqUrl = new URL(req.url, `http://${req.headers.host}`);
|
||||
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 === "/image") {
|
||||
if (!reqUrl.searchParams.has("id")) {
|
||||
res.statusCode = 400;
|
||||
return res.end("400 Bad Request");
|
||||
}
|
||||
|
||||
const newBuffer = Buffer.concat([Buffer.from([0x0]), Buffer.from(uuid)]);
|
||||
socket.send(newBuffer, rinfo.port, rinfo.address);
|
||||
} else if (opcode == 0x2) {
|
||||
socket.send(Buffer.concat([Buffer.from([0x3]), Buffer.from((MAX_JOBS - jobAmount).toString())]), rinfo.port, rinfo.address);
|
||||
const id = reqUrl.searchParams.get("id");
|
||||
if (!jobs[id]) {
|
||||
res.statusCode = 410;
|
||||
return res.end("410 Gone");
|
||||
}
|
||||
log(`Sending image data for job ${id} to ${req.socket.remoteAddress}:${req.socket.remotePort} via HTTP`);
|
||||
res.setHeader("ext", jobs[id].ext);
|
||||
return res.end(jobs[id].data, (err) => {
|
||||
if (err) console.error(err);
|
||||
delete jobs[id];
|
||||
});
|
||||
} else {
|
||||
log("Could not parse message");
|
||||
res.statusCode = 404;
|
||||
return res.end("404 Not Found");
|
||||
}
|
||||
});
|
||||
|
||||
server.on("listening", () => {
|
||||
const address = server.address();
|
||||
log(`server listening ${address.address}:${address.port}`);
|
||||
httpServer.on("error", (e) => {
|
||||
console.error("An HTTP error occurred: ", e);
|
||||
});
|
||||
|
||||
server.bind(8080); // ATTENTION: Always going to be bound to 0.0.0.0 !!!
|
||||
httpServer.listen(8081, () => {
|
||||
log("HTTP listening on port 8081");
|
||||
});
|
||||
|
||||
const runJob = (job) => {
|
||||
const server = net.createServer((sock) => { // Create a TCP socket/server to listen to requests
|
||||
log(`TCP client ${sock.remoteAddress}:${sock.remotePort} has connected`);
|
||||
|
||||
sock.on("error", (e) => {
|
||||
console.error(e);
|
||||
});
|
||||
|
||||
sock.on("data", (msg) => {
|
||||
const opcode = msg.readUint8(0);
|
||||
const req = msg.toString().slice(1,msg.length);
|
||||
console.log(req);
|
||||
// 0x00 == Cancel job
|
||||
// 0x01 == Queue job
|
||||
if (opcode == 0x00) {
|
||||
delete queue[queue.indexOf(req) - 1];
|
||||
delete jobs[req];
|
||||
} else if (opcode == 0x01) {
|
||||
const length = parseInt(req.slice(0, 1));
|
||||
const num = req.slice(1, length + 1);
|
||||
const obj = req.slice(length + 1);
|
||||
const job = { addr: sock.remoteAddress, port: sock.remotePort, msg: obj, num: jobAmount };
|
||||
const uuid = uuidv4();
|
||||
jobs[uuid] = job;
|
||||
queue.push(uuid);
|
||||
|
||||
const newBuffer = Buffer.concat([Buffer.from([0x00]), Buffer.from(uuid), Buffer.from(num)]);
|
||||
sock.write(newBuffer);
|
||||
|
||||
if (jobAmount < MAX_JOBS) {
|
||||
log(`Got TCP request for job ${job.msg} with id ${uuid}`, job.num);
|
||||
acceptJob(uuid, sock);
|
||||
} else {
|
||||
log(`Got TCP request for job ${job.msg} with id ${uuid}, queued in position ${queue.indexOf(uuid)}`, job.num);
|
||||
}
|
||||
} else {
|
||||
log("Could not parse TCP message");
|
||||
}
|
||||
});
|
||||
|
||||
sock.on("end", () => {
|
||||
log(`TCP client ${sock.remoteAddress}:${sock.remotePort} has disconnected`);
|
||||
});
|
||||
});
|
||||
|
||||
server.on("error", (e) => {
|
||||
console.error("A TCP error occurred: ", e);
|
||||
});
|
||||
|
||||
server.listen(8080, () => {
|
||||
log("TCP listening on port 8080");
|
||||
});
|
||||
|
||||
const runJob = (job, sock) => {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
log(`Job ${job.uuid} starting...`, job.num);
|
||||
|
||||
|
@ -100,29 +148,17 @@ const runJob = (job) => {
|
|||
}
|
||||
|
||||
log(`Job ${job.uuid} started`, job.num);
|
||||
const {buffer, fileExtension} = await run(object);
|
||||
|
||||
log(`Sending result of job ${job.uuid} back to the bot`, job.num);
|
||||
const server = net.createServer(function(tcpSocket) {
|
||||
tcpSocket.write(Buffer.concat([Buffer.from(fileExtension), Buffer.from("\n"), buffer]), (err) => {
|
||||
if (err) console.error(err);
|
||||
tcpSocket.end(() => {
|
||||
server.close();
|
||||
resolve(null);
|
||||
});
|
||||
try {
|
||||
const { buffer, fileExtension } = await run(object);
|
||||
log(`Sending result of job ${job.uuid} back to the bot`, job.num);
|
||||
jobs[job.uuid].data = buffer;
|
||||
jobs[job.uuid].ext = fileExtension;
|
||||
sock.write(Buffer.concat([Buffer.from([0x1]), Buffer.from(job.uuid)]), (e) => {
|
||||
if (e) return reject(e);
|
||||
return resolve();
|
||||
});
|
||||
});
|
||||
server.listen(job.port, job.addr);
|
||||
// handle address in use errors
|
||||
server.on("error", (e) => {
|
||||
if (e.code === "EADDRINUSE") {
|
||||
log("Address in use, retrying...", job.num);
|
||||
setTimeout(() => {
|
||||
server.close();
|
||||
server.listen(job.port, job.addr);
|
||||
}, 500);
|
||||
}
|
||||
});
|
||||
socket.send(Buffer.concat([Buffer.from([0x1]), Buffer.from(job.uuid), Buffer.from(job.port.toString())]), job.port, job.addr);
|
||||
} catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
});
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue