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

53 lines
1.7 KiB
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-10-04 21:01:08 +02:00
import { red, bold, yellow, cyan } from "chalk";
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-10-04 21:01:08 +02:00
const commit = require('child_process').execSync('git rev-parse HEAD').toString().trim();
console.log(bold(`
fosscord-server | ${yellow(`Pre-relase (${commit.slice(0, 7)})`)}
Current commit: ${cyan(commit)} (${yellow(commit.slice(0, 7))})
`))
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) {
2021-09-19 18:45:09 +02:00
require("./Server");
2021-08-13 20:54:52 +02:00
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) => {
2021-09-19 18:45:09 +02:00
console.log(
2021-10-04 21:01:08 +02:00
`[Worker] ${red(`died with pid: ${worker.process.pid} , restarting ...`)}`
2021-09-19 18:45:09 +02:00
);
2021-08-13 12:58:18 +02:00
cluster.fork();
});
})();
} else {
2021-09-19 18:45:09 +02:00
require("./Server");
2021-08-13 12:58:18 +02:00
}