summary refs log tree commit diff
path: root/bundle/src/start.ts
blob: 2bfb0069eaf23685b07dc59ed005d57d0b88c456 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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";

// TODO: add tcp socket event transmission
const cores = 1 || Number(process.env.threads) || 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");
}