supposedly async promises are bad lmao

This commit is contained in:
TheEssem 2021-03-15 22:29:48 -05:00
parent f2e81b7eeb
commit 4c83c55344

View file

@ -72,7 +72,7 @@ exports.connect = (server) => {
}; };
const getIdeal = () => { const getIdeal = () => {
return new Promise(async (resolve, reject) => { return new Promise((resolve, reject) => {
let serversLeft = connections.length; let serversLeft = connections.length;
const idealServers = []; const idealServers = [];
const timeout = setTimeout(async () => { const timeout = setTimeout(async () => {
@ -85,9 +85,7 @@ const getIdeal = () => {
}, 5000); }, 5000);
for (const connection of connections) { for (const connection of connections) {
if (!connection.remoteAddress) continue; if (!connection.remoteAddress) continue;
try { fetch(`http://${connection.remoteAddress}:8081/status`).then(statusRequest => statusRequest.text()).then(async (status) => {
const statusRequest = await fetch(`http://${connection.remoteAddress}:8081/status`);
const status = await statusRequest.text();
serversLeft--; serversLeft--;
idealServers.push({ idealServers.push({
addr: connection.remoteAddress, addr: connection.remoteAddress,
@ -98,41 +96,43 @@ const getIdeal = () => {
const server = await chooseServer(idealServers); const server = await chooseServer(idealServers);
resolve(connections.find(val => val.remoteAddress === server.addr)); resolve(connections.find(val => val.remoteAddress === server.addr));
} }
} catch (e) { }).catch(e => reject(e));
reject(e);
}
} }
}); });
}; };
const start = (object, num) => { const start = (object, num) => {
return new Promise(async (resolve, reject) => { return getIdeal().then(async (currentServer) => {
try { const data = Buffer.concat([Buffer.from([0x01 /* queue job */]), Buffer.from(num.length.toString()), Buffer.from(num), Buffer.from(JSON.stringify(object))]);
const currentServer = await getIdeal(); return new Promise((resolve, reject) => {
const data = Buffer.concat([Buffer.from([0x01 /* queue job */]), Buffer.from(num.length.toString()), Buffer.from(num), Buffer.from(JSON.stringify(object))]);
currentServer.write(data, (err) => { currentServer.write(data, (err) => {
if (err) { if (err) {
if (err.code === "EPIPE") { if (err.code === "EPIPE") {
logger.log(`Lost connection to ${currentServer.remoteAddress}, attempting to reconnect...`); logger.log(`Lost connection to ${currentServer.remoteAddress}, attempting to reconnect...`);
currentServer.connect(8080, currentServer.remoteAddress, async () => { currentServer.connect(8080, currentServer.remoteAddress, () => {
const res = start(object, num); const res = start(object, num);
resolve(res); reject(res); // this is done to differentiate the result from a step
}); });
} else { } else {
reject(err); reject(err);
} }
} else {
resolve();
} }
}); });
const event = new EventEmitter(); });
event.once("uuid", (uuid) => { }).then(() => {
delete jobs[num]; const event = new EventEmitter();
jobs[uuid] = event; return new Promise((resolve) => {
resolve({ uuid, event }); event.once("uuid", (uuid) => resolve({ event, uuid }));
});
jobs[num] = event; jobs[num] = event;
} catch (e) { });
reject(e); }, (result) => {
} return Promise.reject(result);
}).then(data => {
delete jobs[num];
jobs[data.uuid] = data.event;
return { uuid: data.uuid, event: data.event };
}); });
}; };
@ -176,32 +176,32 @@ exports.getType = async (image) => {
}; };
exports.run = object => { exports.run = object => {
return new Promise(async (resolve, reject) => { return new Promise((resolve, reject) => {
if (process.env.API === "true") { if (process.env.API === "true") {
try { // Connect to best image server
// Connect to best image server const num = Math.floor(Math.random() * 100000).toString().slice(0, 5);
const num = Math.floor(Math.random() * 100000).toString().slice(0, 5); const timeout = setTimeout(() => {
const timeout = setTimeout(() => { if (jobs[num]) delete jobs[num];
if (jobs[num]) delete jobs[num]; reject("Request timed out");
reject("Request timed out"); }, 25000);
}, 25000); start(object, num).catch(err => { // incredibly hacky code incoming
const { uuid, event } = await start(object, num); if (err instanceof Error) return reject(err);
return err;
}).then((data) => {
clearTimeout(timeout); clearTimeout(timeout);
event.once("image", (image, type) => { data.event.once("image", (image, type) => {
delete jobs[uuid]; delete jobs[data.uuid];
const payload = { const payload = {
// Take just the image data // Take just the image data
buffer: image, buffer: image,
type: type type: type
}; };
resolve(payload); resolve(payload);
}); });
event.once("error", (err) => { data.event.once("error", (err) => {
reject(err); reject(err);
}); });
} catch (e) { }).catch(err => reject(err));
reject(e);
}
} else { } else {
// Called from command (not using image API) // Called from command (not using image API)
const worker = new Worker(path.join(__dirname, "image-runner.js"), { const worker = new Worker(path.join(__dirname, "image-runner.js"), {