From aadb867a613c44d14cad27189c20b8fc0ef49a2d Mon Sep 17 00:00:00 2001 From: RblSb Date: Tue, 25 Feb 2020 16:16:39 +0300 Subject: Handle SIGTERM --- src/server/Main.hx | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) (limited to 'src/server/Main.hx') diff --git a/src/server/Main.hx b/src/server/Main.hx index 9f7534c..5d82ef1 100644 --- a/src/server/Main.hx +++ b/src/server/Main.hx @@ -38,18 +38,16 @@ class Main { final envPort = (process.env : Dynamic).PORT; if (envPort != null) port = envPort; statePath = '$rootDir/user/state.json'; - config = getUserConfig(); - loadState(); function exit() { saveState(); process.exit(); } - process.on("exit", exit); + // process.on("exit", exit); process.on("SIGINT", exit); // ctrl+c process.on("SIGUSR1", exit); // kill pid process.on("SIGUSR2", exit); + process.on("SIGTERM", exit); process.on("uncaughtException", err -> { - trace(err); logError("uncaughtException", { message: err.message, stack: err.stack @@ -57,10 +55,12 @@ class Main { exit(); }); process.on("unhandledRejection", (reason, promise) -> { - trace("Unhandled Rejection at:", reason); logError("unhandledRejection", reason); exit(); }); + initIntergationHandlers(); + loadState(); + config = getUserConfig(); localIp = Utils.getLocalIp(); globalIp = localIp; this.port = port; @@ -96,6 +96,7 @@ class Main { } function saveState():Void { + trace("Saving state..."); final data:ServerState = { videoList: videoList, messages: messages, @@ -110,6 +111,7 @@ class Main { function loadState():Void { if (!FileSystem.exists(statePath)) return; + trace("Loading state..."); final data:ServerState = Json.parse(File.getContent(statePath)); videoList.resize(0); messages.resize(0); @@ -121,12 +123,26 @@ class Main { } function logError(type:String, data:Dynamic):Void { + trace(type, data); final crashesFolder = '$rootDir/user/crashes'; final name = new Date().toISOString() + "-" + type; if (!FileSystem.exists(crashesFolder)) FileSystem.createDirectory(crashesFolder); File.saveContent('$crashesFolder/$name.json', Json.stringify(data, "\t")); } + function initIntergationHandlers():Void { + // Prevent heroku idle when clients online (needs APP_URL env var) + if (process.env["_"] != null && process.env["_"].contains("heroku") + && process.env["APP_URL"] != null) { + new Timer(10 * 60 * 1000).run = function() { + if (clients.length == 0) return; + final url = 'http://${process.env["APP_URL"]}'; + trace('Ping $url'); + Http.get(url, r -> {}); + } + } + } + function onConnect(ws:WebSocket, req:IncomingMessage):Void { final ip = req.connection.remoteAddress; final id = freeIds.length > 0 ? freeIds.shift() : clients.length; -- cgit v1.2.3