diff --git a/src/bundle/Server.ts b/src/bundle/Server.ts new file mode 100644 index 0000000..f796c4a --- /dev/null +++ b/src/bundle/Server.ts @@ -0,0 +1,46 @@ +process.on("unhandledRejection", console.error); +process.on("uncaughtException", console.error); + +import http from "http"; +import * as Api from "@valkyrie/api"; +import * as Gateway from "@valkyrie/gateway"; +import { CDNServer } from "@valkyrie/cdn"; +import express from "express"; +import { green, bold } from "picocolors"; +import { Config, initDatabase, Sentry } from "@valkyrie/util"; + +const app = express(); +const server = http.createServer(); +const port = Number(process.env.PORT) || 3001; +const production = process.env.NODE_ENV == "development" ? false : true; +server.on("request", app); + +const api = new Api.ValkyrieServer({ server, port, production, app }); +const cdn = new CDNServer({ server, port, production, app }); +const gateway = new Gateway.Server({ server, port, production }); + +process.on("SIGTERM", async () => { + console.log("Shutting down due to SIGTERM"); + await gateway.stop(); + await cdn.stop(); + await api.stop(); + server.close(); + Sentry.close(); +}); + +async function main() { + await initDatabase(); + await Config.init(); + await Sentry.init(app); + + await new Promise((resolve) => + server.listen({ port }, () => resolve(undefined)), + ); + await Promise.all([api.start(), cdn.start(), gateway.start()]); + + Sentry.errorHandler(app); + + console.log(`[Server] ${green(`listening on port ${bold(port)}`)}`); +} + +main().catch(console.error); diff --git a/src/bundle/index.ts b/src/bundle/index.ts new file mode 100644 index 0000000..9363f8f --- /dev/null +++ b/src/bundle/index.ts @@ -0,0 +1,4 @@ +export * from "@valkyrie/api"; +export * from "@valkyrie/util"; +export * from "@valkyrie/gateway"; +export * from "@valkyrie/cdn"; diff --git a/src/bundle/start.ts b/src/bundle/start.ts new file mode 100644 index 0000000..ca218ed --- /dev/null +++ b/src/bundle/start.ts @@ -0,0 +1,93 @@ +// process.env.MONGOMS_DEBUG = "true"; +require("module-alias/register"); +import "reflect-metadata"; +import cluster, { Worker } from "cluster"; +import os from "os"; +import { red, bold, yellow, cyan } from "picocolors"; +import { initStats } from "./stats"; +import { config } from "dotenv"; +config(); +import { execSync } from "child_process"; + +const cores = process.env.THREADS ? parseInt(process.env.THREADS) : 1; + +function getCommitOrFail() { + try { + return execSync("git rev-parse HEAD").toString().trim(); + } catch (e) { + return null; + } +} + +if (cluster.isPrimary) { + const commit = getCommitOrFail(); + + console.log( + bold(` +██╗ ██╗ █████╗ ██╗ ██╗ ██╗██╗ ██╗██████╗ ██╗███████╗ ██████╗██╗ ██╗ █████╗ ████████╗ +██║ ██║██╔══██╗██║ ██║ ██╔╝╚██╗ ██╔╝██╔══██╗██║██╔════╝██╔════╝██║ ██║██╔══██╗╚══██╔══╝ +██║ ██║███████║██║ █████╔╝ ╚████╔╝ ██████╔╝██║█████╗ ██║ ███████║███████║ ██║ +╚██╗ ██╔╝██╔══██║██║ ██╔═██╗ ╚██╔╝ ██╔══██╗██║██╔══╝ ██║ ██╔══██║██╔══██║ ██║ + ╚████╔╝ ██║ ██║███████╗██║ ██╗ ██║ ██║ ██║██║███████╗╚██████╗██║ ██║██║ ██║ ██║ + ╚═══╝ ╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝╚═╝╚══════╝ ╚═════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ + + valkyriechat-server | ${yellow( + `Pre-release (${ + commit !== null + ? commit.slice(0, 7) + : "Unknown (Git cannot be found)" + })`, + )} + +Commit Hash: ${ + commit !== null + ? `${cyan(commit)} (${yellow(commit.slice(0, 7))})` + : "Unknown (Git cannot be found)" + } +Cores: ${cyan(os.cpus().length)} (Using ${cores} thread(s).) +`), + ); + + if (commit == null) { + console.log(yellow(`Warning: Git is not installed or not in PATH.`)); + } + + initStats(); + + console.log(`[Process] starting with ${cores} threads`); + + if (cores === 1) { + require("./Server"); + } else { + process.env.EVENT_TRANSMISSION = "process"; + + // Fork workers. + for (let i = 0; i < cores; i++) { + // Delay each worker start if using sqlite database to prevent locking it + const delay = process.env.DATABASE?.includes("://") ? 0 : i * 1000; + setTimeout(() => { + cluster.fork(); + console.log(`[Process] worker ${cyan(i)} started.`); + }, delay); + } + + cluster.on("message", (sender: Worker, message) => { + for (const id in cluster.workers) { + const worker = cluster.workers[id]; + if (worker === sender || !worker) continue; + worker.send(message); + } + }); + + cluster.on("exit", (worker) => { + console.log( + `[Worker] ${red( + `died with PID: ${worker.process.pid} , restarting ...`, + )}`, + ); + cluster.fork(); + }); + } +} else { + require("./Server"); +} diff --git a/src/bundle/stats.ts b/src/bundle/stats.ts new file mode 100644 index 0000000..a4daecc --- /dev/null +++ b/src/bundle/stats.ts @@ -0,0 +1,43 @@ +import os from "os"; +import osu from "node-os-utils"; +import { red } from "picocolors"; + +export function initStats() { + console.log(`[Path] running in ${__dirname}`); + try { + console.log(`[CPU] ${osu.cpu.model()} Cores x${osu.cpu.count()}`); + } catch { + console.log("[CPU] Failed to get cpu model!"); + } + + console.log(`[System] ${os.platform()} ${os.arch()}`); + console.log(`[Process] running with PID: ${process.pid}`); + if (process.getuid && process.getuid() === 0) { + console.warn( + red( + `[Process] Warning ValkyrieChat is running as root, this highly discouraged and might expose your system vulnerable to attackers.` + + `Please run ValkyrieChat as a user without root privileges.`, + ), + ); + } + + // TODO: node-os-utils might have a memory leak, more investigation needed + // TODO: doesn't work if spawned with multiple threads + // setInterval(async () => { + // const [cpuUsed, memory, network] = await Promise.all([ + // osu.cpu.usage(), + // osu.mem.info(), + // osu.netstat.inOut(), + // ]); + // var networkUsage = ""; + // if (typeof network === "object") { + // networkUsage = `| [Network]: in ${network.total.inputMb}mb | out ${network.total.outputMb}mb`; + // } + + // console.log( + // `[CPU] ${cpuUsed.toPrecision(3)}% | [Memory] ${Math.round( + // process.memoryUsage().rss / 1024 / 1024 + // )}mb/${memory.totalMemMb.toFixed(0)}mb ${networkUsage}` + // ); + // }, 1000 * 60 * 5); +}