From 2a3a9c641da29e92ddd321f6c1f6c62ffe3c040d Mon Sep 17 00:00:00 2001 From: Sam Wilkins <35748010+samwilkins333@users.noreply.github.com> Date: Thu, 12 Dec 2019 11:40:56 -0500 Subject: functional session --- src/server/ChildProcessUtilities/daemon/session.ts | 39 ++++++++++++---------- 1 file changed, 22 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/server/ChildProcessUtilities/daemon/session.ts b/src/server/ChildProcessUtilities/daemon/session.ts index 41eb6976e..d6c8ddb64 100644 --- a/src/server/ChildProcessUtilities/daemon/session.ts +++ b/src/server/ChildProcessUtilities/daemon/session.ts @@ -6,7 +6,7 @@ import { MailOptions } from "nodemailer/lib/json-transport"; import { writeFileSync, appendFileSync, existsSync, mkdirSync } from "fs"; import { resolve } from 'path'; import { ChildProcess, exec } from "child_process"; -import * as killport from "kill-port"; +const killport = require("kill-port"); const identifier = yellow("__daemon__:"); @@ -37,6 +37,7 @@ const recipient = "samuel_wilkins@brown.edu"; const frequency = 10; const { pid } = process; let restarting = false; +let count = 0; identifiedLog("Initializing daemon..."); @@ -68,7 +69,8 @@ async function listen() { try { await request.get(heartbeat); if (restarting) { - addLogEntry("Backup server successfully restarted", green); + addLogEntry("Backup server successfully " + count ? "restarted" : "started", green); + count++; } restarting = false; } catch (e) { @@ -77,23 +79,26 @@ async function listen() { if (error) { if (!restarting) { restarting = true; - addLogEntry("Detected a server crash", red); - current_backup?.kill(); - await killport(1050, 'tcp'); - await log_execution({ - startMessage: identifier + " Sending crash notification email", - endMessage: ({ error, result }) => { - const success = error === null && result === true; - return identifier + ` ${(success ? `Notification successfully sent to` : `Failed to notify`)} ${recipient} ${timestamp()}`; - }, - action: async () => notify(error || "Hmm, no error to report..."), - color: cyan - }); - identifiedLog(green("Initiating server restart...")); + if (count) { + addLogEntry("Detected a server crash", red); + current_backup?.kill("SIGTERM"); + identifiedLog(yellow("Cleaning up previous connections...")); + await killport(1050, 'tcp').catch((error: any) => identifiedLog(red(error))); + await killport(4321, 'tcp').catch((error: any) => identifiedLog(red(error))); + identifiedLog(yellow("Connections cleared.")); + await log_execution({ + startMessage: identifier + " Sending crash notification email", + endMessage: ({ error, result }) => { + const success = error === null && result === true; + return identifier + ` ${(success ? `Notification successfully sent to` : `Failed to notify`)} ${recipient} ${timestamp()}`; + }, + action: async () => notify(error || "Hmm, no error to report..."), + color: cyan + }); + identifiedLog(green("Initiating server restart...")); + } current_backup = exec('"C:\\Program Files\\Git\\git-bash.exe" -c "npm run start-release"', err => identifiedLog(err?.message || "Previous server process exited.")); writeLocalPidLog("server", `${(current_backup?.pid ?? -2) + 1} created ${timestamp()}`); - } else { - identifiedLog(yellow(`Callback ignored because restarting already initiated ${timestamp()}`)); } } } -- cgit v1.2.3-70-g09d2