2021-08-19 14:19:14 +00:00
|
|
|
import { BaseServiceWorker } from "eris-fleet";
|
|
|
|
import * as logger from "../logger.js";
|
|
|
|
import fs from "fs";
|
|
|
|
import path from "path";
|
|
|
|
import { fileURLToPath } from "url";
|
|
|
|
import { Worker } from "worker_threads";
|
2022-01-29 22:14:27 +00:00
|
|
|
import { createRequire } from "module";
|
2021-11-27 05:05:09 +00:00
|
|
|
|
2022-01-29 21:08:27 +00:00
|
|
|
// only requiring this to work around an issue regarding worker threads
|
|
|
|
const nodeRequire = createRequire(import.meta.url);
|
2022-01-29 22:14:27 +00:00
|
|
|
nodeRequire(`../../build/${process.env.DEBUG && process.env.DEBUG === "true" ? "Debug" : "Release"}/image.node`);
|
2022-01-29 21:08:27 +00:00
|
|
|
|
2021-11-27 05:05:09 +00:00
|
|
|
import ImageConnection from "../imageConnection.js";
|
2021-07-06 00:20:21 +00:00
|
|
|
|
|
|
|
class ImageWorker extends BaseServiceWorker {
|
|
|
|
constructor(setup) {
|
|
|
|
super(setup);
|
|
|
|
|
2022-01-23 05:16:55 +00:00
|
|
|
console.info = (str) => this.ipc.sendToAdmiral("info", str);
|
|
|
|
|
2021-07-06 00:20:21 +00:00
|
|
|
if (process.env.API === "true") {
|
|
|
|
this.jobs = {};
|
|
|
|
this.connections = new Map();
|
2022-03-12 02:28:35 +00:00
|
|
|
this.servers = JSON.parse(fs.readFileSync(new URL("../../servers.json", import.meta.url), { encoding: "utf8" })).image;
|
2021-11-27 05:05:09 +00:00
|
|
|
this.nextID = 0;
|
2021-07-06 00:20:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.begin().then(() => this.serviceReady());
|
|
|
|
}
|
|
|
|
|
|
|
|
async begin() {
|
|
|
|
// connect to image api if enabled
|
|
|
|
if (process.env.API === "true") {
|
|
|
|
for (const server of this.servers) {
|
|
|
|
try {
|
2021-08-06 22:13:29 +00:00
|
|
|
await this.connect(server.server, server.auth);
|
2021-07-06 00:20:21 +00:00
|
|
|
} catch (e) {
|
|
|
|
logger.error(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async repopulate() {
|
2022-03-12 02:28:35 +00:00
|
|
|
const data = await fs.promises.readFile(new URL("../../servers.json", import.meta.url), { encoding: "utf8" });
|
2021-07-06 00:20:21 +00:00
|
|
|
this.servers = JSON.parse(data).image;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
async getRunning() {
|
|
|
|
const statuses = [];
|
2022-01-17 02:12:54 +00:00
|
|
|
if (process.env.API === "true") {
|
|
|
|
for (const [address, connection] of this.connections) {
|
|
|
|
if (connection.conn.readyState !== 0 && connection.conn.readyState !== 1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
statuses.push({
|
|
|
|
address,
|
|
|
|
runningJobs: connection.njobs,
|
|
|
|
max: connection.max
|
|
|
|
});
|
2021-07-06 00:20:21 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-27 05:05:09 +00:00
|
|
|
return statuses;
|
2021-07-06 00:20:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async chooseServer(ideal) {
|
|
|
|
if (ideal.length === 0) throw "No available servers";
|
|
|
|
const sorted = ideal.sort((a, b) => {
|
2022-01-05 16:17:59 +00:00
|
|
|
return a.load - b.load;
|
2021-09-01 05:21:13 +00:00
|
|
|
}).filter((e, i, array) => {
|
|
|
|
return !(e.load < array[0].load);
|
2021-07-06 00:20:21 +00:00
|
|
|
});
|
|
|
|
return sorted[0];
|
|
|
|
}
|
|
|
|
|
2021-11-29 21:27:13 +00:00
|
|
|
async getIdeal(object) {
|
2021-07-06 00:20:21 +00:00
|
|
|
const idealServers = [];
|
2021-11-27 05:05:09 +00:00
|
|
|
for (const [address, connection] of this.connections) {
|
|
|
|
if (connection.conn.readyState !== 0 && connection.conn.readyState !== 1) {
|
2021-07-06 00:20:21 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-12-23 22:09:50 +00:00
|
|
|
if (object.params.type && connection.formats[object.cmd] && !connection.formats[object.cmd].includes(object.params.type)) continue;
|
2021-11-27 05:05:09 +00:00
|
|
|
idealServers.push({
|
|
|
|
addr: address,
|
|
|
|
load: connection.njobs / connection.max
|
|
|
|
});
|
2021-07-06 00:20:21 +00:00
|
|
|
}
|
2021-11-27 05:05:09 +00:00
|
|
|
const server = await this.chooseServer(idealServers);
|
|
|
|
return this.connections.get(server.addr);
|
2021-07-06 00:20:21 +00:00
|
|
|
}
|
|
|
|
|
2021-08-06 22:13:29 +00:00
|
|
|
async connect(server, auth) {
|
2021-12-03 00:01:33 +00:00
|
|
|
const connection = new ImageConnection(server, auth);
|
2021-07-06 00:20:21 +00:00
|
|
|
this.connections.set(server, connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
async disconnect() {
|
|
|
|
for (const connection of this.connections.values()) {
|
|
|
|
connection.close();
|
|
|
|
}
|
|
|
|
this.connections.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-27 05:05:09 +00:00
|
|
|
waitForWorker(worker) {
|
2021-07-06 00:20:21 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2021-11-27 05:05:09 +00:00
|
|
|
worker.once("message", (data) => {
|
|
|
|
resolve({
|
|
|
|
buffer: Buffer.from([...data.buffer]),
|
|
|
|
type: data.fileExtension
|
2021-07-06 00:20:21 +00:00
|
|
|
});
|
2021-11-27 05:05:09 +00:00
|
|
|
});
|
|
|
|
worker.once("error", reject);
|
2021-07-06 00:20:21 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-11-27 05:05:09 +00:00
|
|
|
async run(object) {
|
|
|
|
if (process.env.API === "true") {
|
2021-12-06 07:25:38 +00:00
|
|
|
let num = this.nextID++;
|
|
|
|
if (num > 4294967295) num = this.nextID = 0;
|
2022-01-26 21:24:10 +00:00
|
|
|
for (let i = 0; i < 3; i++) {
|
|
|
|
const currentServer = await this.getIdeal(object);
|
|
|
|
try {
|
|
|
|
await currentServer.queue(num, object);
|
|
|
|
await currentServer.wait(num);
|
|
|
|
const output = await currentServer.getOutput(num);
|
|
|
|
return output;
|
|
|
|
} catch (e) {
|
|
|
|
if (i < 2 && e === "Request ended prematurely due to a closed connection") {
|
|
|
|
continue;
|
|
|
|
} else {
|
2022-03-10 20:24:03 +00:00
|
|
|
if (e === "No available servers" && i >= 2) throw "Request ended prematurely due to a closed connection";
|
2022-03-13 19:42:27 +00:00
|
|
|
throw e;
|
2022-01-26 21:24:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-27 05:05:09 +00:00
|
|
|
} else {
|
|
|
|
// Called from command (not using image API)
|
|
|
|
const worker = new Worker(path.join(path.dirname(fileURLToPath(import.meta.url)), "../image-runner.js"), {
|
|
|
|
workerData: object
|
|
|
|
});
|
|
|
|
return await this.waitForWorker(worker);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-06 00:20:21 +00:00
|
|
|
async handleCommand(data) {
|
|
|
|
try {
|
|
|
|
if (data.type === "run") {
|
2021-07-31 18:36:00 +00:00
|
|
|
const result = await this.run(data.obj);
|
|
|
|
return result;
|
2021-07-06 00:20:21 +00:00
|
|
|
} else if (data.type === "reload") {
|
|
|
|
await this.disconnect();
|
|
|
|
await this.repopulate();
|
|
|
|
let amount = 0;
|
|
|
|
for (const server of this.servers) {
|
|
|
|
try {
|
2021-08-06 22:13:29 +00:00
|
|
|
await this.connect(server.server, server.auth);
|
2021-07-06 00:20:21 +00:00
|
|
|
amount += 1;
|
|
|
|
} catch (e) {
|
|
|
|
logger.error(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return amount;
|
|
|
|
} else if (data.type === "stats") {
|
|
|
|
return await this.getRunning();
|
|
|
|
}
|
|
|
|
} catch (err) {
|
2021-07-31 18:36:00 +00:00
|
|
|
return { err: typeof err === "string" ? err : err.message };
|
2021-07-06 00:20:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
shutdown(done) {
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-19 14:19:14 +00:00
|
|
|
export default ImageWorker;
|