diff options
Diffstat (limited to 'src/server/index.ts')
-rw-r--r-- | src/server/index.ts | 41 |
1 files changed, 37 insertions, 4 deletions
diff --git a/src/server/index.ts b/src/server/index.ts index 2cc35ccec..83413c23c 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -21,10 +21,17 @@ import UploadManager from "./ApiManagers/UploadManager"; import { log_execution } from "./ActionUtilities"; import GeneralGoogleManager from "./ApiManagers/GeneralGoogleManager"; import GooglePhotosManager from "./ApiManagers/GooglePhotosManager"; -import { yellow, red } from "colors"; +import { yellow, red, cyan } from "colors"; import { disconnect } from "../server/Initialization"; -import { ProcessFactory, Logger } from "./ProcessFactory"; +import { Logger } from "./ProcessFactory"; +import { isMaster, on, fork, workers } from "cluster"; +import { identifier } from "./session_manager/config"; +import InputManager from "./session_manager/input_manager"; +import { execSync } from "child_process"; +import { CrashEmail } from "./session_manager/crash_email"; +const killport = require("kill-port"); +export const onWindows = process.platform === "win32"; export const publicDirectory = path.resolve(__dirname, "public"); export const filesDirectory = path.resolve(publicDirectory, "files"); @@ -128,11 +135,37 @@ function routeSetter({ isRelease, addSupervisedRoute, logRegistrationOutcome }: WebSocket.initialize(serverPort, isRelease); } -(async function start() { +async function start() { await log_execution({ startMessage: "\nstarting execution of preliminary functions", endMessage: "completed preliminary functions\n", action: preliminaryFunctions }); await initializeServer({ serverPort: 1050, routeSetter }); -})(); +} + +const admin = ["samuel_wilkins@brown.edu"]; +if (isMaster) { + fork(); + on("exit", ({ process: { pid } }, code, signal) => { + const prompt = `Server worker with process id ${pid} has died with code ${code}${signal === null ? "" : `, having encountered signal ${signal}`}.\n`; + console.log(cyan(prompt)); + fork(); + }); + const { registerCommand } = new InputManager({ identifier }); + registerCommand("exit", [], () => execSync(onWindows ? "taskkill /f /im node.exe" : "killall -9 node")); + registerCommand("restart", [], () => { + for (const id in workers) { + workers[id]?.kill(); + } + fork(); + }); +} else { + process.on('uncaughtException', async error => { + await CrashEmail.dispatch(error, admin); + console.error(red(`Crash event detected @ ${new Date().toUTCString()}`)); + console.error(error.message); + process.exit(1); + }); + start(); +}
\ No newline at end of file |