diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/server/DashSession/DashSessionAgent.ts | 4 | ||||
-rw-r--r-- | src/server/session/agents/monitor.ts | 29 | ||||
-rw-r--r-- | src/server/session/agents/server_worker.ts | 4 |
3 files changed, 15 insertions, 22 deletions
diff --git a/src/server/DashSession/DashSessionAgent.ts b/src/server/DashSession/DashSessionAgent.ts index f3f0a3c3d..0d9486757 100644 --- a/src/server/DashSession/DashSessionAgent.ts +++ b/src/server/DashSession/DashSessionAgent.ts @@ -33,8 +33,8 @@ export class DashSessionAgent extends AppliedSessionAgent { monitor.addReplCommand("debug", [/active|passive/, /\S+\@\S+/], async ([mode, recipient]) => this.dispatchZippedDebugBackup(mode, recipient)); monitor.addServerMessageListener("backup", this.backup); monitor.addServerMessageListener("debug", ({ args: { mode, recipient } }) => this.dispatchZippedDebugBackup(mode, recipient)); - monitor.on(Monitor.IntrinsicEvents.KeyGenerated, this.dispatchSessionPassword); - monitor.on(Monitor.IntrinsicEvents.CrashDetected, this.dispatchCrashReport); + monitor.onKeyGenerated(this.dispatchSessionPassword); + monitor.onCrashDetected(this.dispatchCrashReport); } /** diff --git a/src/server/session/agents/monitor.ts b/src/server/session/agents/monitor.ts index f6738a23f..cd09c9e41 100644 --- a/src/server/session/agents/monitor.ts +++ b/src/server/session/agents/monitor.ts @@ -48,6 +48,10 @@ export class Monitor extends EventEmitter { } } + public onCrashDetected = (listener: (...args: any[]) => void) => this.on(Monitor.IntrinsicEvents.CrashDetected, listener); + public onKeyGenerated = (listener: (...args: any[]) => void) => this.on(Monitor.IntrinsicEvents.KeyGenerated, listener); + public onServerRunning = (listener: (...args: any[]) => void) => this.on(Monitor.IntrinsicEvents.ServerRunning, listener); + /** * Kill this session and its active child * server process, either gracefully (may wait @@ -317,30 +321,19 @@ export class Monitor extends EventEmitter { }); Monitor.childIPCManager = new PromisifiedIPCManager(this.activeWorker); this.mainLog(cyan(`spawned new server worker with process id ${this.activeWorker?.process.pid}`)); + + this.addServerMessageListener("kill", ({ args: { reason, graceful, errorCode } }) => this.killSession(reason, graceful, errorCode)); + this.addServerMessageListener(`notify_${Monitor.IntrinsicEvents.CrashDetected}`, ({ args: { error } }) => this.emit(Monitor.IntrinsicEvents.CrashDetected, error)); + this.addServerMessageListener(`notify_${Monitor.IntrinsicEvents.ServerRunning}`, ({ args: { firstTime } }) => this.emit(Monitor.IntrinsicEvents.ServerRunning, firstTime)); + // an IPC message handler that executes actions on the master thread when prompted by the active worker Monitor.childIPCManager.addMessagesHandler(async ({ lifecycle, action }) => { if (action) { const { message, args } = action as Monitor.Action; console.log(this.timestamp(), `${this.config.identifiers.worker.text} action requested (${cyan(message)})`); - switch (message) { - case "kill": - const { reason, graceful, errorCode } = args; - this.killSession(reason, graceful, errorCode); - break; - case "notify_crash": - this.emit(Monitor.IntrinsicEvents.CrashDetected, args.error); - break; - case Monitor.IntrinsicEvents.ServerRunning: - this.emit(Monitor.IntrinsicEvents.ServerRunning, args.firstTime); - break; - case "set_port": - const { port, value, immediateRestart } = args; - this.setPort(port, value, immediateRestart); - break; - } const handlers = this.onMessage[message]; if (handlers) { - handlers.forEach(handler => handler({ message, args })); + await Promise.all(handlers.map(handler => handler({ message, args }))); } } if (lifecycle) { @@ -358,7 +351,7 @@ export namespace Monitor { args: any; } - export type ServerMessageHandler = (action: Action) => void | Promise<void>; + export type ServerMessageHandler = (action: Action) => any | Promise<any>; export enum IntrinsicEvents { KeyGenerated = "key_generated", diff --git a/src/server/session/agents/server_worker.ts b/src/server/session/agents/server_worker.ts index 278cbb42f..b279a19d8 100644 --- a/src/server/session/agents/server_worker.ts +++ b/src/server/session/agents/server_worker.ts @@ -118,7 +118,7 @@ export class ServerWorker { private proactiveUnplannedExit = async (error: Error): Promise<void> => { this.shouldServerBeResponsive = false; // communicates via IPC to the master thread that it should dispatch a crash notification email - this.sendMonitorAction("notify_crash", { error }); + this.sendMonitorAction(`notify_${Monitor.IntrinsicEvents.CrashDetected}`, { error }); await this.executeExitHandlers(error); // notify master thread (which will log update in the console) of crash event via IPC this.lifecycleNotification(red(`crash event detected @ ${new Date().toUTCString()}`)); @@ -138,7 +138,7 @@ export class ServerWorker { if (!this.shouldServerBeResponsive) { // notify monitor thread that the server is up and running this.lifecycleNotification(green(`listening on ${this.serverPort}...`)); - this.sendMonitorAction(Monitor.IntrinsicEvents.ServerRunning, { firstTime: !this.isInitialized }); + this.sendMonitorAction(`notify_${Monitor.IntrinsicEvents.ServerRunning}`, { firstTime: !this.isInitialized }); this.isInitialized = true; } this.shouldServerBeResponsive = true; |