2023-01-13 04:40:33 +00:00
|
|
|
import cluster from "node:cluster";
|
|
|
|
import chalk from "chalk";
|
|
|
|
import Xev from "xev";
|
2019-04-07 12:50:36 +00:00
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
import Logger from "@/services/logger.js";
|
|
|
|
import { envOption } from "../env.js";
|
2019-04-07 12:50:36 +00:00
|
|
|
|
|
|
|
// for typeorm
|
2023-01-13 04:40:33 +00:00
|
|
|
import "reflect-metadata";
|
|
|
|
import { masterMain } from "./master.js";
|
|
|
|
import { workerMain } from "./worker.js";
|
2019-04-07 12:50:36 +00:00
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
const logger = new Logger("core", "cyan");
|
|
|
|
const clusterLogger = logger.createSubLogger("cluster", "orange", false);
|
2022-04-17 05:42:13 +00:00
|
|
|
const ev = new Xev();
|
2019-04-07 12:50:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Init process
|
|
|
|
*/
|
2023-01-13 04:40:33 +00:00
|
|
|
export default async function () {
|
|
|
|
process.title = `Calckey (${cluster.isPrimary ? "master" : "worker"})`;
|
|
|
|
|
|
|
|
if (cluster.isPrimary || envOption.disableClustering) {
|
2022-12-16 00:09:00 +00:00
|
|
|
await masterMain();
|
|
|
|
if (cluster.isPrimary) {
|
2023-01-13 04:40:33 +00:00
|
|
|
ev.mount();
|
2022-12-16 00:09:00 +00:00
|
|
|
}
|
2023-01-13 04:40:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cluster.isWorker || envOption.disableClustering) {
|
2022-12-16 00:09:00 +00:00
|
|
|
await workerMain();
|
2023-01-13 04:40:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// For when Calckey is started in a child process during unit testing.
|
|
|
|
// Otherwise, process.send cannot be used, so start it.
|
|
|
|
if (process.send) {
|
|
|
|
process.send("ok");
|
|
|
|
}
|
2019-04-07 12:50:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//#region Events
|
|
|
|
|
|
|
|
// Listen new workers
|
2023-01-13 04:40:33 +00:00
|
|
|
cluster.on("fork", (worker) => {
|
|
|
|
clusterLogger.debug(`Process forked: [${worker.id}]`);
|
2019-04-07 12:50:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Listen online workers
|
2023-01-13 04:40:33 +00:00
|
|
|
cluster.on("online", (worker) => {
|
|
|
|
clusterLogger.debug(`Process is now online: [${worker.id}]`);
|
2019-04-07 12:50:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Listen for dying workers
|
2023-01-13 04:40:33 +00:00
|
|
|
cluster.on("exit", (worker) => {
|
|
|
|
// Replace the dead worker,
|
|
|
|
// we're not sentimental
|
|
|
|
clusterLogger.error(chalk.red(`[${worker.id}] died :(`));
|
|
|
|
cluster.fork();
|
2019-04-07 12:50:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Display detail of unhandled promise rejection
|
2021-10-08 12:24:05 +00:00
|
|
|
if (!envOption.quiet) {
|
2023-01-13 04:40:33 +00:00
|
|
|
process.on("unhandledRejection", console.dir);
|
2019-04-07 12:50:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Display detail of uncaught exception
|
2023-01-13 04:40:33 +00:00
|
|
|
process.on("uncaughtException", (err) => {
|
|
|
|
try {
|
2022-12-16 00:09:00 +00:00
|
|
|
logger.error(err);
|
2023-01-13 04:40:33 +00:00
|
|
|
} catch {}
|
2019-04-07 12:50:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Dying away...
|
2023-01-13 04:40:33 +00:00
|
|
|
process.on("exit", (code) => {
|
|
|
|
logger.info(`The process is going to exit with code ${code}`);
|
2019-04-07 12:50:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
//#endregion
|