diff options
Diffstat (limited to 'src/server/DashSession.ts')
-rw-r--r-- | src/server/DashSession.ts | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/src/server/DashSession.ts b/src/server/DashSession.ts new file mode 100644 index 000000000..a0e00adda --- /dev/null +++ b/src/server/DashSession.ts @@ -0,0 +1,73 @@ +import { Session } from "./Session/session"; +import { Email } from "./ActionUtilities"; +import { red, yellow, green } from "colors"; +import { get } from "request-promise"; +import { Utils } from "../Utils"; +import { WebSocket } from "./Websocket/Websocket"; +import { MessageStore } from "./Message"; +import { launchServer, onWindows } from "."; + +/** +* If we're the monitor (master) thread, we should launch the monitor logic for the session. +* Otherwise, we must be on a worker thread that was spawned *by* the monitor (master) thread, and thus +* our job should be to run the server. +*/ +export class DashSessionAgent extends Session.AppliedSessionAgent { + + private readonly notificationRecipients = ["samuel_wilkins@brown.edu"]; + private readonly signature = "-Dash Server Session Manager"; + + protected async launchMonitor() { + const monitor = Session.Monitor.Create({ + key: async key => { + // this sends a pseudorandomly generated guid to the configuration's recipients, allowing them alone + // to kill the server via the /kill/:key route + const content = `The key for this session (started @ ${new Date().toUTCString()}) is ${key}.\n\n${this.signature}`; + const failures = await Email.dispatchAll(this.notificationRecipients, "Server Termination Key", content); + if (failures) { + failures.map(({ recipient, error: { message } }) => monitor.mainLog(red(`dispatch failure @ ${recipient} (${yellow(message)})`))); + return false; + } + return true; + }, + crash: async ({ name, message, stack }) => { + const body = [ + "You, as a Dash Administrator, are being notified of a server crash event. Here's what we know:", + `name:\n${name}`, + `message:\n${message}`, + `stack:\n${stack}`, + "The server is already restarting itself, but if you're concerned, use the Remote Desktop Connection to monitor progress.", + ].join("\n\n"); + const content = `${body}\n\n${this.signature}`; + const failures = await Email.dispatchAll(this.notificationRecipients, "Dash Web Server Crash", content); + if (failures) { + failures.map(({ recipient, error: { message } }) => monitor.mainLog(red(`dispatch failure @ ${recipient} (${yellow(message)})`))); + return false; + } + return true; + } + }); + monitor.addReplCommand("pull", [], () => monitor.exec("git pull")); + monitor.addReplCommand("solr", [/start|stop/], async args => { + const command = `${onWindows ? "solr.cmd" : "solr"} ${args[0] === "start" ? "start" : "stop -p 8983"}`; + await monitor.exec(command, { cwd: "./solr-8.3.1/bin" }); + try { + await get("http://localhost:8983"); + monitor.mainLog(green("successfully connected to 8983 after running solr initialization")); + } catch { + monitor.mainLog(red("unable to connect at 8983 after running solr initialization")); + } + }); + return monitor; + } + + protected async launchServerWorker() { + const worker = Session.ServerWorker.Create(launchServer); // server initialization delegated to worker + worker.addExitHandler(() => { + const { _socket } = WebSocket; + _socket && Utils.Emit(_socket, MessageStore.ConnectionTerminated, "Manual"); + }); + return worker; + } + +}
\ No newline at end of file |