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

79 lines
2.5 KiB
TypeScript
Raw Normal View History

// process.env.MONGOMS_DEBUG = "true";
2021-10-08 10:43:43 +02:00
import "reflect-metadata";
2021-08-13 12:58:18 +02:00
import cluster from "cluster";
import os from "os";
2021-10-04 21:39:50 +02:00
import { red, bold, yellow, cyan } from "nanocolors";
2021-08-14 23:15:19 +02:00
import { initStats } from "./stats";
2021-10-04 21:57:24 +02:00
import { config } from "dotenv";
config();
import { execSync } from "child_process";
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
function getCommitOrFail() {
try {
return execSync("git rev-parse HEAD").toString().trim();
} catch (e) {
return null;
}
}
const commit = getCommitOrFail();
console.log(
bold(`
2021-10-04 21:01:08 +02:00
fosscord-server | ${yellow(
`Pre-relase (${
commit !== null
? commit.slice(0, 7)
: "Unknown (Git cannot be found)"
})`
)}
2021-10-04 21:01:08 +02:00
Current commit: ${
commit !== null
? `${cyan(commit)} (${yellow(commit.slice(0, 7))})`
: "Unknown (Git cannot be found)"
}
`)
);
2021-10-04 21:01:08 +02:00
if (commit == null)
console.log(yellow(`Warning: Git is not installed or not in PATH.`));
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(
`[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
}