Replit Commit

This commit is contained in:
Dunemask 2022-05-05 12:35:47 +00:00
commit f49f965a42
41 changed files with 32720 additions and 0 deletions

55
lib/core/JobManager.js Normal file
View file

@ -0,0 +1,55 @@
import { v4 } from "uuid";
import doExec from "./internal-exec.js";
export default class JobManager {
constructor(clientMaxJobs) {
this.clientMaxJobs = clientMaxJobs;
this.clients = {};
}
getJob(clientId, jobId) {
return this.clients[clientId].jobs.find((j) => j.id === jobId);
}
getJobById(jobId) {
for (var client of Object.values(this.clients)) {
const job = client.jobs.find((j) => j.id === jobId);
if (!job) continue;
return job;
}
}
pushLog(jobId, log) {
const job = this.getJobById(jobId);
if (log instanceof Array) job.log.push(...log);
else job.log.push(log);
}
closeJob(jobId, exitcode) {
const job = this.getJobById(jobId);
job.exitcode = exitcode;
}
newJob(jobRequest, id) {
if (!jobRequest) throw Error("Request Must Be Object!");
if (!this.clients[id]) this.clients[id] = { jobs: [] };
const client = this.clients[id];
if (
client.jobs.filter((j) => j.exitcode === undefined).length >=
this.clientMaxJobs
)
throw Error("Client's Active Jobs Exceeded!");
const job = { ...jobRequest };
job.id = v4();
job.log = [];
this.clients[id].jobs.push(job);
if (process.env.INTERNAL_EXECUTOR === "true") doExec(job);
return { ...job };
}
removeJob(clientId, id) {
this.clients[clientId].jobs = this.clients[clientId].jobs.filter(
(j) => j.id !== id
);
}
}

43
lib/core/Qualiteer.js Normal file
View file

@ -0,0 +1,43 @@
import "dotenv/config"; // Load Envars
// Imports
import fig from "figlet";
import http from "http";
import { INFO, OK, logInfo } from "../util/logging.js";
// import "../utils/preload.js"; // Load Globals
// Import Core Modules
import applySockets from "../sockets/handler.js";
import JobManager from "./JobManager.js";
// Constants
const title = "QLTR";
const port = process.env.QUALITEER_DEV_PORT ?? 52000;
// Class
export default class Qualiteer {
constructor(options = {}) {
for (var k in options) this[k] = options[k];
this.jobs = new JobManager(options.maxClientJobs ?? 3);
this.port = options.port ?? port;
}
async _preinitialize() {
logInfo(fig.textSync(title, "Cosmike"));
INFO("INIT", "Initializing...");
this.app = (await import("./server.js")).default;
this.server = http.createServer(this.app);
this.sockets = applySockets(this.server, this.jobs);
}
start() {
const qt = this;
return new Promise(async function init(res) {
await qt._preinitialize();
qt.server.listen(qt.port, function onStart() {
OK("SERVER", `Running on ${qt.port}`);
res();
});
});
}
}

12
lib/core/internal-exec.js Normal file
View file

@ -0,0 +1,12 @@
import { INFO } from "../util/logging.js";
import Executor from "../sockets/clients/Executor.js";
export default (job) => {
INFO("EXEC", "Starting Internal Executor");
try {
const exec = new Executor("http://localhost:52000", job);
exec.runJob();
} catch (err) {
ERR("EXEC", err);
}
};

13
lib/core/server.js Normal file
View file

@ -0,0 +1,13 @@
// Imports
import express from "express";
// Routes
import results from "../routes/results-route.js";
const app = express();
app.all("/", (req, res) => res.redirect("/qualiteer"));
// Middlewares
// Routes
app.use("/api/results", results);