1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-14 06:32:36 +01:00
server/bundle/src/start.ts

33 lines
722 B
TypeScript
Raw Normal View History

// process.env.MONGOMS_DEBUG = "true";
2021-08-13 12:58:18 +02:00
import cluster from "cluster";
import os from "os";
2021-08-14 23:15:19 +02:00
import { initStats } from "./stats";
2021-08-13 12:58:18 +02:00
// TODO: add tcp socket event transmission
const cores = 1 || Number(process.env.threads) || os.cpus().length;
2021-08-13 12:58:18 +02:00
if (cluster.isMaster && !process.env.masterStarted) {
process.env.masterStarted = "true";
(async () => {
2021-08-14 23:15:19 +02:00
initStats();
2021-08-13 12:58:18 +02:00
2021-08-13 20:54:52 +02:00
if (cores === 1) {
require("./Server.js");
return;
}
2021-08-13 12:58:18 +02:00
// Fork workers.
for (let i = 0; i < cores; i++) {
cluster.fork();
}
cluster.on("exit", (worker: any, code: any, signal: any) => {
console.log(`[Worker] died with pid: ${worker.process.pid} , restarting ...`);
cluster.fork();
});
})();
} else {
require("./Server.js");
}