From 4856d477e41579335be7767c0c2db0678087ee85 Mon Sep 17 00:00:00 2001 From: jusax23 Date: Tue, 4 Apr 2023 20:32:00 +0200 Subject: [PATCH] fix, close keep-alive connections on shutdown --- src/html/log.ts | 38 ++++++++++++++++++++++---------------- src/main.ts | 9 ++++++--- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/html/log.ts b/src/html/log.ts index e06957f..7b7ded0 100644 --- a/src/html/log.ts +++ b/src/html/log.ts @@ -170,23 +170,29 @@ export default /*html*/ ` } async function refresh(){ - let resp = await fetch("json"); - if(resp.status != 200){ - pre.innerText = "Outbag Log Api is disabled
"; - return; + try{ + let resp = await fetch("json"); + if(resp.status != 200){ + throw new Error("can not get Data"); + } + let json = await resp.json(); + + let scrollPosition = document.documentElement.getBoundingClientRect().bottom; + let heigth = window.innerHeight; + + let scroll = heigth + 50 >= scrollPosition; + if(loaded.length == 0) pre.innerText = ""; + json.forEach(d=>{ + if(loaded.includes(d[0])) return; + loaded.push(d[0]); + parse(d); + }); + if(scroll) document.documentElement.scrollIntoView(false); + }catch(_){ + pre.innerText = "Outbag Log Api is disabled or Server is down."; + loaded = []; } - let json = await resp.json(); - - let scrollPosition = document.documentElement.getBoundingClientRect().bottom; - let heigth = window.innerHeight; - - let scroll = heigth + 50 >= scrollPosition; - json.forEach(d=>{ - if(loaded.includes(d[0])) return; - loaded.push(d[0]); - parse(d); - }); - if(scroll) document.documentElement.scrollIntoView(false); + } setInterval(refresh, 1000); diff --git a/src/main.ts b/src/main.ts index deb4d4e..8ad318f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -64,11 +64,14 @@ program.parse(); const activeRequest = false; -var serverclose = { close: (d: () => void) => d() }; +var serverclose: (https.Server | http.Server | null) = null; nman.addShutdownTask(() => new Promise(async (res, rej) => { await closeWebSocket(); - serverclose.close(() => res()); -}), 30000); + if (serverclose != null) { + serverclose.keepAliveTimeout = 100; + serverclose.close(() => res()); + } +}), 10000); async function startServer() { await connectToDB();