aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSam Wilkins <35748010+samwilkins333@users.noreply.github.com>2019-12-12 11:40:56 -0500
committerSam Wilkins <35748010+samwilkins333@users.noreply.github.com>2019-12-12 11:40:56 -0500
commit2a3a9c641da29e92ddd321f6c1f6c62ffe3c040d (patch)
tree570a47f98a5cdf6b1203556e1a4835d9306c3070 /src
parent85e7ca251beefa0b5417c8e6e6d28f9aaa5d886c (diff)
functional session
Diffstat (limited to 'src')
-rw-r--r--src/server/ChildProcessUtilities/daemon/session.ts39
1 files changed, 22 insertions, 17 deletions
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()}`));
}
}
}