aboutsummaryrefslogtreecommitdiffstats
path: root/src/server
diff options
context:
space:
mode:
authorRblSb <msrblsb@gmail.com>2020-02-25 16:16:39 +0300
committerRblSb <msrblsb@gmail.com>2020-02-26 08:30:03 +0300
commitaadb867a613c44d14cad27189c20b8fc0ef49a2d (patch)
treed177ba86fd9082e75d6bb669be5af5f5e75fe8fc /src/server
parent1cc391ceb0cf5db62e0422390780e79de2597202 (diff)
Handle SIGTERM
Diffstat (limited to 'src/server')
-rw-r--r--src/server/Main.hx26
1 files changed, 21 insertions, 5 deletions
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;
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage