From b55ce381c905e87421bf011f5cd0cf423f858bb8 Mon Sep 17 00:00:00 2001 From: Sam Wilkins <35748010+samwilkins333@users.noreply.github.com> Date: Thu, 12 Dec 2019 10:51:47 -0500 Subject: session --- src/server/ChildProcessUtilities/daemon/session.ts | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/server/ChildProcessUtilities/daemon/session.ts b/src/server/ChildProcessUtilities/daemon/session.ts index c41657cc9..9e082e665 100644 --- a/src/server/ChildProcessUtilities/daemon/session.ts +++ b/src/server/ChildProcessUtilities/daemon/session.ts @@ -5,7 +5,7 @@ import * as nodemailer from "nodemailer"; import { MailOptions } from "nodemailer/lib/json-transport"; import { writeFileSync, appendFileSync, existsSync, mkdirSync } from "fs"; import { resolve } from 'path'; -import { ChildProcess } from "child_process"; +import { ChildProcess, exec } from "child_process"; import { ProcessFactory } from "../ProcessFactory"; const identifier = yellow("__daemon__:"); @@ -21,11 +21,11 @@ if (!existsSync(crashPath)) { mkdirSync(crashPath); } -const crashLogPath = resolve(crashPath, `./session_crashes_${timestamp()}.log`); +const crashLogPath = resolve(crashPath, `./session_crashes_${new Date().toISOString()}.log`); function addLogEntry(message: string, color: Color) { const formatted = color(`${message} ${timestamp()}.`); identifiedLog(formatted); - appendFileSync(crashLogPath, `${formatted}\n`); + // appendFileSync(crashLogPath, `${formatted}\n`); } function identifiedLog(message?: any, ...optionalParams: any[]) { @@ -88,13 +88,24 @@ async function listen() { action: async () => notify(error || "Hmm, no error to report..."), color: cyan }); - current_backup = await log_execution({ + await log_execution({ startMessage: identifier + " Initiating server restart", endMessage: ({ result, error }) => { const success = error === null && result !== undefined; return identifier + success ? " Child process spawned..." : ` An error occurred while attempting to restart the server:\n${error}`; }, - action: () => ProcessFactory.createWorker('npm', ['run', 'start'], "inherit"), + action: async () => { + return new Promise(resolve => { + exec('"C:\\Program Files\\Git\\git-bash.exe" -c "npm run start-release"', err => { + if (err) { + identifiedLog(err.message); + return; + } + resolve(); + }); + + }); + }, color: green }); writeLocalPidLog("server", `${(current_backup?.pid ?? -2) + 1} created ${timestamp()}`); -- cgit v1.2.3-70-g09d2 From 85e7ca251beefa0b5417c8e6e6d28f9aaa5d886c Mon Sep 17 00:00:00 2001 From: Sam Wilkins Date: Thu, 12 Dec 2019 11:05:36 -0500 Subject: killport --- package.json | 3 ++- src/server/ChildProcessUtilities/daemon/session.ts | 25 ++++------------------ src/typings/index.d.ts | 1 + 3 files changed, 7 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/package.json b/package.json index 77a521e27..c58f542fc 100644 --- a/package.json +++ b/package.json @@ -161,6 +161,7 @@ "js-datepicker": "^4.6.6", "jsonwebtoken": "^8.5.0", "jsx-to-string": "^1.4.0", + "kill-port": "^1.6.0", "lodash": "^4.17.15", "mobile-detect": "^1.4.3", "mobx": "^5.15.0", @@ -231,4 +232,4 @@ "xoauth2": "^1.2.0", "youtube": "^0.1.0" } -} \ No newline at end of file +} diff --git a/src/server/ChildProcessUtilities/daemon/session.ts b/src/server/ChildProcessUtilities/daemon/session.ts index 9e082e665..41eb6976e 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 { ProcessFactory } from "../ProcessFactory"; +import * as killport from "kill-port"; const identifier = yellow("__daemon__:"); @@ -79,6 +79,7 @@ async function listen() { 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 }) => { @@ -88,26 +89,8 @@ async function listen() { action: async () => notify(error || "Hmm, no error to report..."), color: cyan }); - await log_execution({ - startMessage: identifier + " Initiating server restart", - endMessage: ({ result, error }) => { - const success = error === null && result !== undefined; - return identifier + success ? " Child process spawned..." : ` An error occurred while attempting to restart the server:\n${error}`; - }, - action: async () => { - return new Promise(resolve => { - exec('"C:\\Program Files\\Git\\git-bash.exe" -c "npm run start-release"', err => { - if (err) { - identifiedLog(err.message); - return; - } - resolve(); - }); - - }); - }, - color: green - }); + 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()}`)); diff --git a/src/typings/index.d.ts b/src/typings/index.d.ts index 9e8ace962..674278126 100644 --- a/src/typings/index.d.ts +++ b/src/typings/index.d.ts @@ -2,6 +2,7 @@ declare module 'googlephotos'; declare module 'react-image-lightbox-with-rotate'; +declare module 'kill-port'; declare module '@react-pdf/renderer' { import * as React from 'react'; -- cgit v1.2.3-70-g09d2 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