Replit Commit
This commit is contained in:
commit
f49f965a42
41 changed files with 32720 additions and 0 deletions
55
lib/core/JobManager.js
Normal file
55
lib/core/JobManager.js
Normal 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
43
lib/core/Qualiteer.js
Normal 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
12
lib/core/internal-exec.js
Normal 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
13
lib/core/server.js
Normal 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);
|
19
lib/database/migrations/1_create_test_results_table.sql
Normal file
19
lib/database/migrations/1_create_test_results_table.sql
Normal file
|
@ -0,0 +1,19 @@
|
|||
CREATE SEQUENCE test_results_id_seq;
|
||||
CREATE TABLE test_results (
|
||||
id bigint NOT NULL DEFAULT nextval('test_results_seq') PRIMARY KEY,
|
||||
test_name varchar(255) DEFAULT NULL,
|
||||
test_class varchar(255) DEFAULT NULL,
|
||||
test_method varchar(255) DEFAULT NULL,
|
||||
test_path varchar(255) DEFAULT NULL,
|
||||
test_type varchar(32) DEFAULT NULL,
|
||||
test_timestamp timestamptz NOT NULL DEFAULT now(),
|
||||
test_retry BOOLEAN DEFAULT FALSE,
|
||||
origin varchar(255) DEFAULT NULL,
|
||||
failed BOOLEAN DEFAULT FALSE,
|
||||
failed_message varchar(2047) DEFAULT NULL,
|
||||
screenshot_url varchar(255) DEFAULT NULL,
|
||||
weblog_url varchar(255) DEFAULT NULL,
|
||||
);
|
||||
ALTER SEQUENCE test_results_id_seq OWNED BY test_results.id;
|
||||
|
||||
|
120
lib/database/pg-query.js
Normal file
120
lib/database/pg-query.js
Normal file
|
@ -0,0 +1,120 @@
|
|||
const buildPostgresEntry = (entry) => {
|
||||
const pgEntry = { ...entry };
|
||||
Object.keys(pgEntry).forEach((col) => {
|
||||
if (pgEntry[col] === undefined) delete pgEntry[col];
|
||||
});
|
||||
return pgEntry;
|
||||
};
|
||||
|
||||
export const buildPostgresValue = (jsVar) => {
|
||||
if (jsVar === null) return "null";
|
||||
if (typeof jsVar === "string") return buildPostgresString(jsVar);
|
||||
if (Array.isArray(jsVar) && jsVar.length === 0) return "null";
|
||||
if (Array.isArray(jsVar) && isTypeArray(jsVar, "string"))
|
||||
return buildPostgresStringArray(jsVar);
|
||||
return jsVar;
|
||||
};
|
||||
|
||||
const buildPostgresStringArray = (jsonArray) => {
|
||||
if (jsonArray.length === 0) return null;
|
||||
var pgArray = [...jsonArray];
|
||||
var arrayString = "ARRAY [";
|
||||
pgArray.forEach((e, i) => (pgArray[i] = `'${e}'`));
|
||||
arrayString += pgArray.join(",");
|
||||
arrayString += "]";
|
||||
return arrayString;
|
||||
};
|
||||
|
||||
const isTypeArray = (jsonArray, type) =>
|
||||
jsonArray.every((e) => typeof e === type);
|
||||
|
||||
const buildPostgresString = (jsonString) =>
|
||||
(jsonString && `'${jsonString.replaceAll("'", "''")}'`) || null;
|
||||
|
||||
export const insertQuery = (table, jsEntry) => {
|
||||
if (typeof jsEntry !== "object") throw Error("PG Inserts must be objects!");
|
||||
const entry = buildPostgresEntry(jsEntry);
|
||||
const cols = Object.keys(entry);
|
||||
cols.forEach((col, i) => {
|
||||
entry[col] = buildPostgresValue(entry[col]);
|
||||
cols[i] = `"${col}"`;
|
||||
});
|
||||
var query = `INSERT INTO ${table}(${cols.join(",")})\n`;
|
||||
query += `VALUES(${Object.values(entry).join(",")})`;
|
||||
return query;
|
||||
};
|
||||
|
||||
export const deleteQuery = (table, jsEntry) => {
|
||||
if (typeof jsEntry !== "object")
|
||||
throw Error("PG Delete conditionals must be an object!");
|
||||
const entry = buildPostgresEntry(jsEntry);
|
||||
const cols = Object.keys(entry);
|
||||
const conditionals = [];
|
||||
for (var col of cols) {
|
||||
entry[col] = buildPostgresValue(entry[col]);
|
||||
conditionals.push(`x.${col}=${entry[col]}`);
|
||||
}
|
||||
return `DELETE FROM ${table} x WHERE ${conditionals.join(" AND ")}`;
|
||||
};
|
||||
export const onConflictUpdate = (conflicts, updates) => {
|
||||
if (!Array.isArray(conflicts)) throw Error("PG Conflicts must be an array!");
|
||||
if (typeof updates !== "object") throw Error("PG Updates must be objects!");
|
||||
const entry = buildPostgresEntry(updates);
|
||||
var query = `ON CONFLICT (${conflicts.join(",")}) DO UPDATE SET\n`;
|
||||
const cols = Object.keys(entry);
|
||||
for (var col of cols) {
|
||||
entry[col] = buildPostgresValue(entry[col]);
|
||||
}
|
||||
query += cols.map((c) => `${c}=${entry[c]}`).join(",");
|
||||
return query;
|
||||
};
|
||||
export const clearTableQuery = (table) => {
|
||||
return `TRUNCATE ${table}`;
|
||||
};
|
||||
|
||||
export const selectWhereQuery = (table, jsEntry, joinWith) => {
|
||||
if (typeof jsEntry !== "object") throw Error("PG Where must be an object!");
|
||||
const where = buildPostgresEntry(jsEntry);
|
||||
const cols = Object.keys(where);
|
||||
var query = `SELECT * FROM ${table} AS x WHERE\n`;
|
||||
for (var col of cols) {
|
||||
where[col] = buildPostgresValue(where[col]);
|
||||
}
|
||||
return (query += cols.map((c) => `x.${c}=${where[c]}`).join(joinWith));
|
||||
};
|
||||
export const updateWhereQuery = (table, updates, wheres, joinWith) => {
|
||||
if (typeof updates !== "object") throw Error("PG Updates must be an object!");
|
||||
if (typeof wheres !== "object") throw Error("PG Wheres must be an object!");
|
||||
const update = buildPostgresEntry(updates);
|
||||
const where = buildPostgresEntry(wheres);
|
||||
const updateCols = Object.keys(update);
|
||||
const whereCols = Object.keys(where);
|
||||
var query = `UPDATE ${table}\n`;
|
||||
var updateQuery = updateCols
|
||||
.map((c) => `${c} = ${buildPostgresValue(update[c])}`)
|
||||
.join(",");
|
||||
var whereQuery = whereCols
|
||||
.map((c) => `${c} = ${buildPostgresValue(where[c])}`)
|
||||
.join(joinWith);
|
||||
return (query += `SET ${updateQuery} WHERE ${whereQuery}`);
|
||||
};
|
||||
export const updateWhereAnyQuery = (table, updates, wheres) =>
|
||||
updateWhereQuery(table, updates, wheres, " OR ");
|
||||
export const updateWhereAllQuery = (table, updates, wheres) =>
|
||||
updateWhereQuery(table, updates, wheres, " AND ");
|
||||
export const selectWhereAnyQuery = (table, where) =>
|
||||
selectWhereQuery(table, where, " OR ");
|
||||
export const selectWhereAllQuery = (table, where) =>
|
||||
selectWhereQuery(table, where, " AND ");
|
||||
|
||||
export default {
|
||||
selectWhereAnyQuery,
|
||||
selectWhereAllQuery,
|
||||
updateWhereAnyQuery,
|
||||
updateWhereAllQuery,
|
||||
insertQuery,
|
||||
deleteQuery,
|
||||
buildPostgresValue,
|
||||
onConflictUpdate,
|
||||
clearTableQuery,
|
||||
};
|
47
lib/database/postgres.js
Normal file
47
lib/database/postgres.js
Normal file
|
@ -0,0 +1,47 @@
|
|||
// Imports
|
||||
import { migrate } from "postgres-migrations";
|
||||
import createPgp from "pg-promise";
|
||||
import moment from "moment";
|
||||
import { WARN } from "../util/logging.js";
|
||||
// Environment Variables
|
||||
const {
|
||||
POSTGRES_DATABASE: database,
|
||||
POSTGRES_DISABLED: pgDisabled,
|
||||
POSTGRES_HOST: host,
|
||||
POSTGRES_PASSWORD: password,
|
||||
POSTGRES_PORT: port,
|
||||
POSTGRES_USER: user,
|
||||
} = process.env;
|
||||
|
||||
// Postgres-promise Configuration
|
||||
// Ensure dates get saved as UTC date strings
|
||||
// This prevents the parser from doing strange datetime operations
|
||||
const pgp = createPgp();
|
||||
pgp.pg.types.setTypeParser(1114, (str) => moment.utc(str).format());
|
||||
|
||||
// Database Config
|
||||
const dbConfig = {
|
||||
database: database ?? "qualiteer",
|
||||
user: user ?? "postgres",
|
||||
password: password ?? "postgres",
|
||||
host: host ?? "localhost",
|
||||
port: port ?? 5432,
|
||||
ensureDatabaseExists: true,
|
||||
};
|
||||
|
||||
const migrationsDir = "lib/database/migrations";
|
||||
|
||||
const configure = async () => {
|
||||
if (pgDisabled) {
|
||||
WARN("POSTGRES", "Postgres Disabled!");
|
||||
return { query: (str) => INFO("POSTGRES MOCK", str) };
|
||||
}
|
||||
await migrate(dbConfig, migrationsDir);
|
||||
// Override the global variable DB
|
||||
const pg = pgp(dbConfig);
|
||||
await pg.connect();
|
||||
OK("POSTGRES", `Connected to database ${database}!`);
|
||||
return pg;
|
||||
};
|
||||
|
||||
export default await configure();
|
51
lib/database/queries/test_results.js
Normal file
51
lib/database/queries/test_results.js
Normal file
|
@ -0,0 +1,51 @@
|
|||
import pg from "../postgres.js";
|
||||
// Imports
|
||||
import {
|
||||
insertQuery,
|
||||
selectWhereAnyQuery,
|
||||
updateWhereAnyQuery,
|
||||
} from "../pg-query.js";
|
||||
// Constants
|
||||
const table = "test_results";
|
||||
// Queries
|
||||
export const insertTestResult = (testResult) => {
|
||||
const {
|
||||
test_name,
|
||||
test_class,
|
||||
test_method,
|
||||
test_path,
|
||||
test_type,
|
||||
test_timestamp,
|
||||
test_retry,
|
||||
origin,
|
||||
failed,
|
||||
failed_message,
|
||||
screenshot_url,
|
||||
expected_screenshot_url,
|
||||
weblog_url,
|
||||
} = testResult;
|
||||
|
||||
var query = insertQuery(table, {
|
||||
test_name,
|
||||
test_class,
|
||||
test_method,
|
||||
test_path,
|
||||
test_type,
|
||||
test_timestamp,
|
||||
test_retry,
|
||||
origin,
|
||||
failed,
|
||||
failed_message,
|
||||
screenshot_url,
|
||||
expected_screenshot_url,
|
||||
weblog_url,
|
||||
});
|
||||
|
||||
query += "\n RETURNING *";
|
||||
return pg.query(query);
|
||||
};
|
||||
|
||||
export const getCurrentlyFailing = () => {
|
||||
const query = "SELECT *";
|
||||
return pg.query(query);
|
||||
};
|
1
lib/index.js
Normal file
1
lib/index.js
Normal file
|
@ -0,0 +1 @@
|
|||
export { default } from "./core/Qualiteer.js";
|
27
lib/rabbit/TestResultsConsumer.js
Normal file
27
lib/rabbit/TestResultsConsumer.js
Normal file
|
@ -0,0 +1,27 @@
|
|||
// Imports
|
||||
import { RabbitConsumer } from "rabiteer";
|
||||
// Class
|
||||
export default class TestResultsConsumer extends RabbitConsumer {
|
||||
constructor() {
|
||||
super("TestResults");
|
||||
}
|
||||
|
||||
/* Example Test Result
|
||||
{
|
||||
testName: “SomeTest”,
|
||||
testClass: “SomeClass”,
|
||||
testMethod: “SomeMethod”,
|
||||
testType: “API/UI”,
|
||||
testTimestamp: 123893024,
|
||||
origin: “TestSuite”,
|
||||
failed: true,
|
||||
failedMessage: “Some Failure”,
|
||||
screenshotUrl: “https://screenshot”,
|
||||
expectedScreenshotUrl: “https://expected”
|
||||
consoleLogUrl: “https://consolelog”
|
||||
}
|
||||
*/
|
||||
onMessage(testResult) {
|
||||
console.log(testResult);
|
||||
}
|
||||
}
|
9
lib/routes/results-route.js
Normal file
9
lib/routes/results-route.js
Normal file
|
@ -0,0 +1,9 @@
|
|||
import express from "express";
|
||||
import { getCurrentlyFailing } from "../database/queries/test_results.js";
|
||||
const router = express.Router();
|
||||
|
||||
router.get("/failing", (req, res) => {
|
||||
res.send([]);
|
||||
});
|
||||
|
||||
export default router;
|
75
lib/sockets/clients/Executor.js
Normal file
75
lib/sockets/clients/Executor.js
Normal file
|
@ -0,0 +1,75 @@
|
|||
import { Manager } from "socket.io-client";
|
||||
import cp from "child_process";
|
||||
|
||||
import modes from "../modes.js";
|
||||
import events from "../events.js";
|
||||
|
||||
export { default as events } from "../events.js";
|
||||
export { default as modes } from "../modes.js";
|
||||
|
||||
// Data Stream Types
|
||||
const ERR = "e";
|
||||
const OUT = "o";
|
||||
|
||||
export default class Executor {
|
||||
constructor(url, job, options = {}) {
|
||||
this.url = url;
|
||||
this.job = job;
|
||||
this.mode = modes.EXEC;
|
||||
|
||||
// Internal Buffer
|
||||
this.buf = {};
|
||||
this.buf[ERR] = "";
|
||||
this.buf[OUT] = "";
|
||||
|
||||
// Methods
|
||||
this.spawn = options.spawn ?? this.spawn.bind(this);
|
||||
this.report = options.report ?? this.report.bind(this);
|
||||
this.onProcClose = options.onProcClose ?? this.onProcClose.bind(this);
|
||||
this.onClose = options.onClose ?? this.onClose.bind(this);
|
||||
}
|
||||
|
||||
spawn() {
|
||||
const cmdArgs = this.job.command.split(" ");
|
||||
const cmd = cmdArgs.shift();
|
||||
this.proc = cp.spawn(cmd, cmdArgs);
|
||||
|
||||
// Set Encoding
|
||||
this.proc.stdout.setEncoding("utf8");
|
||||
this.proc.stderr.setEncoding("utf8");
|
||||
|
||||
// Process Events
|
||||
this.proc.stdout.on("data", (d) => this.report(d.toString(), OUT));
|
||||
this.proc.stderr.on("data", (d) => this.report(d.toString(), ERR));
|
||||
this.proc.on("close", this.onProcClose);
|
||||
}
|
||||
|
||||
runJob() {
|
||||
const mgr = new Manager(this.url, {
|
||||
query: { mode: this.mode, jobId: this.job.id },
|
||||
});
|
||||
this.socket = mgr.socket("/");
|
||||
this.socket.on("connect", this.spawn);
|
||||
this.socket.on("disconnect", this.onClose);
|
||||
}
|
||||
|
||||
onClose() {
|
||||
console.log("Server disconnected, terminating process.");
|
||||
this.proc.kill("SIGINT");
|
||||
}
|
||||
|
||||
onProcClose(code) {
|
||||
this.socket.emit(events.JOB_CLS, code);
|
||||
console.log(`Process finished with code ${code}`);
|
||||
this.socket.disconnect();
|
||||
}
|
||||
|
||||
report(d, dType) {
|
||||
this.buf[dType] += d;
|
||||
if (!this.buf[dType].includes("\n")) return;
|
||||
this.socket.emit(events.JOB_REP, this.buf[dType]);
|
||||
if (dType === ERR) console.error(`err: ${this.buf[dType]}`);
|
||||
else console.log(`out: ${this.buf[dType]}`);
|
||||
this.buf[dType] = "";
|
||||
}
|
||||
}
|
34
lib/sockets/clients/Initiator.js
Normal file
34
lib/sockets/clients/Initiator.js
Normal file
|
@ -0,0 +1,34 @@
|
|||
import { Manager } from "socket.io-client";
|
||||
import modes from "../modes.js";
|
||||
import events from "../events.js";
|
||||
|
||||
export { default as events } from "../events.js";
|
||||
export { default as modes } from "../modes.js";
|
||||
|
||||
export default class Initiator {
|
||||
constructor(url, options = {}) {
|
||||
this.url = url;
|
||||
this.mode = modes.INIT;
|
||||
this.onLog = options.onLog ?? ((d) => console.log(`job: ${d}`));
|
||||
this.onClose = options.onClose ?? (() => {});
|
||||
this.onCreate = options.onCreate ?? ((id) => console.log(`job id: ${id}`));
|
||||
}
|
||||
|
||||
async newJob(jobRequest, onLog, onClose, onCreate) {
|
||||
const mgr = new Manager(this.url, {
|
||||
query: { mode: this.mode, job: JSON.stringify(jobRequest) },
|
||||
});
|
||||
onLog = onLog ?? this.onLog.bind(this);
|
||||
onClose = onClose ?? this.onClose.bind(this);
|
||||
onCreate = onCreate ?? this.onCreate.bind(this);
|
||||
const sk = mgr.socket("/");
|
||||
sk.on(events.JOB_LOG, onLog);
|
||||
sk.on(events.JOB_CLS, onClose);
|
||||
return new Promise((res) =>
|
||||
sk.on(events.JOB_CRT, function onJobCreate(id) {
|
||||
onCreate(id);
|
||||
res({ ...jobRequest, id });
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
27
lib/sockets/clients/Viewer.js
Normal file
27
lib/sockets/clients/Viewer.js
Normal file
|
@ -0,0 +1,27 @@
|
|||
import { Manager } from "socket.io-client";
|
||||
import modes from "../modes.js";
|
||||
import events from "../events.js";
|
||||
|
||||
export { default as events } from "../events.js";
|
||||
export { default as modes } from "../modes.js";
|
||||
|
||||
export default class Viewer {
|
||||
constructor(url, options = {}) {
|
||||
this.url = url;
|
||||
this.mode = modes.VIEW;
|
||||
this.onLog = options.onLog ?? console.log;
|
||||
this.onClose = options.onClose ?? (() => {});
|
||||
}
|
||||
|
||||
viewJob(jobId, onLog, onClose) {
|
||||
const mgr = new Manager(this.url, {
|
||||
query: { mode: this.mode, jobId },
|
||||
});
|
||||
onLog = onLog ?? this.onLog.bind(this);
|
||||
onClose = onClose ?? this.onClose.bind(this);
|
||||
const sk = mgr.socket("/");
|
||||
sk.on(events.JOB_LOG, onLog);
|
||||
sk.on(events.JOB_CLS, onClose);
|
||||
return sk;
|
||||
}
|
||||
}
|
5
lib/sockets/clients/index.js
Normal file
5
lib/sockets/clients/index.js
Normal file
|
@ -0,0 +1,5 @@
|
|||
export { default as Initiator } from "./Initiator.js";
|
||||
|
||||
export { default as Viewer } from "./Viewer.js";
|
||||
|
||||
export { default as Executor } from "./Executor.js";
|
3
lib/sockets/clients/web.index.js
Normal file
3
lib/sockets/clients/web.index.js
Normal file
|
@ -0,0 +1,3 @@
|
|||
export { default as Initiator } from "./Initiator.js";
|
||||
|
||||
export { default as Viewer } from "./Viewer.js";
|
13
lib/sockets/events.js
Normal file
13
lib/sockets/events.js
Normal file
|
@ -0,0 +1,13 @@
|
|||
const JOB_REP = "jr"; // Job Report Event
|
||||
const JOB_LOG = "jl"; // Job Log Event
|
||||
const JOB_CLS = "jc"; // Job Close Event
|
||||
const JOB_CRT = "jcr"; // Job Create Event
|
||||
const ERR = "e"; // Socket Error
|
||||
|
||||
export default {
|
||||
JOB_REP,
|
||||
JOB_LOG,
|
||||
JOB_CLS,
|
||||
JOB_CRT,
|
||||
ERR,
|
||||
};
|
48
lib/sockets/handler.js
Normal file
48
lib/sockets/handler.js
Normal file
|
@ -0,0 +1,48 @@
|
|||
import { Server as Skio } from "socket.io";
|
||||
import evt from "./events.js";
|
||||
import modes from "./modes.js";
|
||||
|
||||
import { initiator, executor, viewer } from "./modifiers.js";
|
||||
|
||||
const socketDrop = (io, room, id) => {
|
||||
const { rooms } = io.of("/").adapter;
|
||||
const clients = rooms.get(room);
|
||||
if (clients.size > 1 || clients.size === 0) return;
|
||||
const socketId = Array.from(clients)[0];
|
||||
const s = io.sockets.sockets.get(socketId);
|
||||
s.disconnect();
|
||||
};
|
||||
|
||||
const socketConnect = (io, socket, jobs) => {
|
||||
const { mode } = socket.handshake.query;
|
||||
switch (mode) {
|
||||
case modes.INIT:
|
||||
initiator(socket, jobs);
|
||||
break;
|
||||
case modes.EXEC:
|
||||
executor(io, socket, jobs);
|
||||
break;
|
||||
case modes.VIEW:
|
||||
viewer(socket);
|
||||
break;
|
||||
default:
|
||||
socket.send(evt.ERR, "Invalid Mode!");
|
||||
socket.disconnect();
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
const socketAuth = (socket, next) => {
|
||||
const { token } = socket.handshake.auth;
|
||||
// next(new Error("Bad Token"));
|
||||
next();
|
||||
};
|
||||
|
||||
const applySockets = (server, jobs, options) => {
|
||||
const io = new Skio(server);
|
||||
io.on("connection", (socket) => socketConnect(io, socket, jobs));
|
||||
io.of("/").adapter.on("leave-room", (room, id) => socketDrop(io, room, id));
|
||||
return io;
|
||||
};
|
||||
|
||||
export default applySockets;
|
8
lib/sockets/modes.js
Normal file
8
lib/sockets/modes.js
Normal file
|
@ -0,0 +1,8 @@
|
|||
const INIT = "i"; // Intiator Socket
|
||||
const EXEC = "e"; // Execution Socket
|
||||
const VIEW = "v"; // View Socket
|
||||
export default {
|
||||
INIT,
|
||||
EXEC,
|
||||
VIEW,
|
||||
};
|
34
lib/sockets/modifiers.js
Normal file
34
lib/sockets/modifiers.js
Normal file
|
@ -0,0 +1,34 @@
|
|||
import evt from "./events.js";
|
||||
|
||||
export const initiator = (socket, jobs) => {
|
||||
const jobStr = socket.handshake.query.job;
|
||||
const jobReq = JSON.parse(jobStr);
|
||||
|
||||
if (!jobReq || !(jobReq instanceof Object))
|
||||
throw Error("No 'job' was included in the handshake query");
|
||||
|
||||
const job = jobs.newJob(jobReq, socket.id);
|
||||
socket.join(job.id);
|
||||
socket.emit(evt.JOB_CRT, job.id);
|
||||
};
|
||||
|
||||
export const executor = (io, socket, jobs) => {
|
||||
const jobId = socket.handshake.query.jobId;
|
||||
if (!jobId) throw Error("No 'jobId' was included in the handshake query");
|
||||
|
||||
socket.join(jobId);
|
||||
socket.on(evt.JOB_REP, function onReport(log) {
|
||||
jobs.pushLog(jobId, log);
|
||||
io.to(jobId).emit(evt.JOB_LOG, log);
|
||||
});
|
||||
socket.on(evt.JOB_CLS, function onClose(code) {
|
||||
jobs.closeJob(jobId, code);
|
||||
io.to(jobId).emit(evt.JOB_CLS, code);
|
||||
});
|
||||
};
|
||||
|
||||
export const viewer = (socket) => {
|
||||
const jobId = socket.handshake.query.jobId;
|
||||
if (!jobId) throw Error("No 'jobId' was included in the handshake query");
|
||||
socket.join(jobId);
|
||||
};
|
28
lib/util/logging.js
Normal file
28
lib/util/logging.js
Normal file
|
@ -0,0 +1,28 @@
|
|||
// Imports
|
||||
import { Chalk } from "chalk";
|
||||
const { redBright, greenBright, yellowBright, cyanBright, magentaBright } =
|
||||
new Chalk({ level: 2 });
|
||||
|
||||
// Logging
|
||||
const logColor = (color, header, ...args) =>
|
||||
console.log(color(header), ...args);
|
||||
|
||||
export const logError = (...args) => logColor(redBright, ...args);
|
||||
|
||||
export const logConfirm = (...args) => logColor(greenBright, ...args);
|
||||
|
||||
export const logWarn = (...args) => logColor(yellowBright, ...args);
|
||||
|
||||
export const logInfo = (...args) => logColor(cyanBright, ...args);
|
||||
|
||||
export const logVerbose = (...args) => logColor(magentaBright, ...args);
|
||||
|
||||
export const ERR = (header, ...args) => logError(`[${header}]`, ...args);
|
||||
|
||||
export const OK = (header, ...args) => logConfirm(`[${header}]`, ...args);
|
||||
|
||||
export const WARN = (header, ...args) => logWarn(`[${header}]`, ...args);
|
||||
|
||||
export const INFO = (header, ...args) => logInfo(`[${header}]`, ...args);
|
||||
|
||||
export const VERB = (header, ...args) => logVerbose(`[${header}]`, ...args);
|
Loading…
Add table
Add a link
Reference in a new issue