summary refs log tree commit diff
path: root/bundle/src/start.ts
diff options
context:
space:
mode:
Diffstat (limited to 'bundle/src/start.ts')
-rw-r--r--bundle/src/start.ts74
1 files changed, 74 insertions, 0 deletions
diff --git a/bundle/src/start.ts b/bundle/src/start.ts
new file mode 100644
index 00000000..2ae2ce87
--- /dev/null
+++ b/bundle/src/start.ts
@@ -0,0 +1,74 @@
+import fs from "fs";
+import { MongoMemoryServer } from "mongodb-memory-server-global-4.4";
+import path from "path";
+import cluster from "cluster";
+import os from "os";
+import osu from "node-os-utils";
+import exitHook from "async-exit-hook";
+
+const cores = Number(process.env.threads) || 1 || os.cpus().length;
+
+if (cluster.isMaster && !process.env.masterStarted) {
+	const dbPath = path.join(__dirname, "..", "..", "db");
+	const dbName = "fosscord";
+	const storageEngine = "wiredTiger";
+	const port = 27020;
+	const ip = "127.0.0.1";
+	var mongod: MongoMemoryServer;
+	fs.mkdirSync(dbPath, { recursive: true });
+
+	exitHook((callback: any) => {
+		(async () => {
+			console.log(`Stopping MongoDB ...`);
+			await mongod.stop();
+			console.log(`Stopped MongoDB`);
+			callback();
+		})();
+	});
+
+	process.env.masterStarted = "true";
+
+	setInterval(async () => {
+		const [cpuUsed, memory, network] = await Promise.all([osu.cpu.usage(), osu.mem.info(), osu.netstat.inOut()]);
+		if (typeof network === "object") {
+			console.log(`Network: in ${network.total.inputMb}mb | out ${network.total.outputMb}mb`);
+		}
+
+		console.log(
+			`[CPU] ${cpuUsed.toFixed(2)}% | [Memory] ${memory.usedMemMb.toFixed(0)}mb/${memory.totalMemMb.toFixed(0)}mb`
+		);
+	}, 1000 * 60);
+
+	(async () => {
+		console.log(`[Database] starting ...`);
+		mongod = new MongoMemoryServer({
+			instance: {
+				port,
+				ip,
+				dbName,
+				dbPath,
+				storageEngine,
+				auth: false, // by default `mongod` is started with '--noauth', start `mongod` with '--auth'
+			},
+		});
+		await mongod.start();
+		process.env.MONGO_URL = mongod.getUri(dbName);
+
+		console.log(`[CPU] ${osu.cpu.model()} Cores x${osu.cpu.count()}`);
+		console.log(`[System] ${await osu.os.oos()} ${os.arch()}`);
+		console.log(`[Database] started`);
+		console.log(`[Process] running with pid: ${process.pid}`);
+
+		// 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");
+}