Skip to content

Commit 545a914

Browse files
authored
better shutdown logic to capture waveapp logs from wavesrv shutdown (#931)
1 parent 76ccd83 commit 545a914

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

cmd/server/main-server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ func doShutdown(reason string) {
6565
watcher.Close()
6666
}
6767
time.Sleep(500 * time.Millisecond)
68+
log.Printf("shutdown complete\n")
6869
os.Exit(0)
6970
})
7071
}

emain/emain.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import * as util from "util";
1515
import winston from "winston";
1616
import { initGlobal } from "../frontend/app/store/global";
1717
import * as services from "../frontend/app/store/services";
18-
import { initElectronWshrpc } from "../frontend/app/store/wshrpcutil";
18+
import { initElectronWshrpc, shutdownWshrpc } from "../frontend/app/store/wshrpcutil";
1919
import { WSServerEndpointVarName, WebServerEndpointVarName, getWebServerEndpoint } from "../frontend/util/endpoints";
2020
import { fetch } from "../frontend/util/fetchutil";
2121
import * as keyutil from "../frontend/util/keyutil";
@@ -41,6 +41,8 @@ import { configureAutoUpdater, updater } from "./updater";
4141
const electronApp = electron.app;
4242
let WaveVersion = "unknown"; // set by WAVESRV-ESTART
4343
let WaveBuildTime = 0; // set by WAVESRV-ESTART
44+
let forceQuit = false;
45+
let isWaveSrvDead = false;
4446

4547
const WaveAppPathVarName = "WAVETERM_APP_PATH";
4648
const WaveSrvReadySignalPidVarName = "WAVETERM_READY_SIGNAL_PID";
@@ -167,10 +169,12 @@ function runWaveSrv(): Promise<boolean> {
167169
env: envCopy,
168170
});
169171
proc.on("exit", (e) => {
170-
if (globalIsQuitting || updater?.status == "installing") {
172+
if (updater?.status == "installing") {
171173
return;
172174
}
173175
console.log("wavesrv exited, shutting down");
176+
forceQuit = true;
177+
isWaveSrvDead = true;
174178
electronApp.quit();
175179
});
176180
proc.on("spawn", (e) => {
@@ -935,9 +939,30 @@ electronApp.on("window-all-closed", () => {
935939
electronApp.quit();
936940
}
937941
});
938-
electronApp.on("before-quit", () => {
942+
electronApp.on("before-quit", (e) => {
939943
globalIsQuitting = true;
940944
updater?.stop();
945+
waveSrvProc?.kill("SIGINT");
946+
shutdownWshrpc();
947+
if (forceQuit) {
948+
return;
949+
}
950+
e.preventDefault();
951+
const allWindows = electron.BrowserWindow.getAllWindows();
952+
for (const window of allWindows) {
953+
window.hide();
954+
}
955+
if (isWaveSrvDead) {
956+
console.log("wavesrv is dead, quitting immediately");
957+
forceQuit = true;
958+
electronApp.quit();
959+
return;
960+
}
961+
setTimeout(() => {
962+
console.log("waiting for wavesrv to exit...");
963+
forceQuit = true;
964+
electronApp.quit();
965+
}, 3000);
941966
});
942967
process.on("SIGINT", () => {
943968
console.log("Caught SIGINT, shutting down");

frontend/app/store/ws.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class WSControl {
4444
baseHostPort: string;
4545
lastReconnectTime: number = 0;
4646
eoOpts: ElectronOverrideOpts;
47+
noReconnect: boolean = false;
4748

4849
constructor(
4950
baseHostPort: string,
@@ -59,8 +60,13 @@ class WSControl {
5960
setInterval(this.sendPing.bind(this), 5000);
6061
}
6162

63+
shutdown() {
64+
this.noReconnect = true;
65+
this.wsConn.close();
66+
}
67+
6268
connectNow(desc: string) {
63-
if (this.open) {
69+
if (this.open || this.noReconnect) {
6470
return;
6571
}
6672
this.lastReconnectTime = Date.now();
@@ -82,6 +88,9 @@ class WSControl {
8288
}
8389

8490
reconnect(forceClose?: boolean) {
91+
if (this.noReconnect) {
92+
return;
93+
}
8594
if (this.open) {
8695
if (forceClose) {
8796
this.wsConn.close(); // this will force a reconnect

frontend/app/store/wshrpcutil.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ function initElectronWshrpc(electronClient: WshClient, eoOpts: ElectronOverrideO
128128
addWSReconnectHandler(wpsReconnectHandler);
129129
}
130130

131+
function shutdownWshrpc() {
132+
globalWS?.shutdown();
133+
}
134+
131135
function initWshrpc(windowId: string): WSControl {
132136
DefaultRouter = new WshRouter(new UpstreamWshRpcProxy());
133137
const handleFn = (event: WSEventType) => {
@@ -163,5 +167,6 @@ export {
163167
sendRpcCommand,
164168
sendRpcResponse,
165169
sendWSCommand,
170+
shutdownWshrpc,
166171
WindowRpcClient,
167172
};

0 commit comments

Comments
 (0)