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";
|
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);
|
|
|
|
|
|
|
|
if (process.env.API === "true") {
|
|
|
|
this.jobs = {};
|
|
|
|
this.connections = new Map();
|
|
|
|
this.servers = JSON.parse(fs.readFileSync("./servers.json", { 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() {
|
|
|
|
const data = await fs.promises.readFile("./servers.json", { encoding: "utf8" });
|
|
|
|
this.servers = JSON.parse(data).image;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
async getRunning() {
|
|
|
|
const statuses = [];
|
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-11-27 05:05:09 +00:00
|
|
|
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) => {
|
|
|
|
return b.load - a.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-10 04:31:52 +00:00
|
|
|
if (object.params.type && !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;
|
2021-11-29 21:27:13 +00:00
|
|
|
const currentServer = await this.getIdeal(object);
|
2021-11-27 05:05:09 +00:00
|
|
|
await currentServer.queue(num, object);
|
|
|
|
await currentServer.wait(num);
|
|
|
|
const output = await currentServer.getOutput(num);
|
|
|
|
return output;
|
|
|
|
} 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;
|