Add graceful sigterm handling to workers

This commit is contained in:
zkldi
2021-11-12 03:39:17 +00:00
parent 647835a7f5
commit b2be2fc841
3 changed files with 40 additions and 18 deletions
+35
View File
@@ -0,0 +1,35 @@
import { monkDB } from "external/mongo/db";
import { CloseRedisConnection } from "external/redis/redis";
import http from "http";
import https from "https";
import { rootLogger } from "lib/logger/logger";
import { CloseScoreImportQueue } from "lib/score-import/worker/queue";
const logger = rootLogger;
export function HandleSIGTERMGracefully(instance?: http.Server | https.Server) {
logger.info("SIGTERM Received, closing program.", { shutdownInfo: true });
if (instance) {
instance.close(() => {
CloseEverythingElse();
});
} else {
CloseEverythingElse();
}
}
async function CloseEverythingElse() {
logger.info("Closing Mongo Database.", { shutdownInfo: true });
await monkDB.close();
logger.info("Closing Redis Connection.", { shutdownInfo: true });
CloseRedisConnection();
logger.info("Closing Score Import Queue.", { shutdownInfo: true });
CloseScoreImportQueue();
logger.info("Everything closed. Waiting for process to exit naturally.", {
shutdownInfo: true,
});
}
@@ -12,6 +12,7 @@
// Explicitly set this before importing anything!
process.env.IS_SCORE_WORKER_SERVER = "true";
import { HandleSIGTERMGracefully } from "lib/handlers/sigterm";
import CreateLogCtx from "lib/logger/logger";
import { ImportTypes } from "tachi-common";
import { FormatUserDoc, GetUserWithID } from "utils/user";
@@ -67,3 +68,5 @@ ScoreImportQueue.process(async <I extends ImportTypes>(job: ScoreImportJob<I>) =
return importDocument;
});
process.on("SIGTERM", HandleSIGTERMGracefully);
+2 -18
View File
@@ -19,6 +19,7 @@ import fetch from "utils/fetch";
import http from "http";
import { CloseRedisConnection } from "external/redis/redis";
import { CloseScoreImportQueue } from "lib/score-import/worker/queue";
import { HandleSIGTERMGracefully } from "lib/handlers/sigterm";
const logger = CreateLogCtx(__filename);
@@ -74,24 +75,7 @@ if (ServerConfig.ENABLE_SERVER_HTTPS) {
logger.info(`HTTP Listening on port ${Environment.port}`, { bootInfo: true });
}
process.on("SIGTERM", () => {
logger.info("SIGTERM Received, closing program.", { shutdownInfo: true });
instance.close(async () => {
logger.info("Closing Mongo Database.", { shutdownInfo: true });
await monkDB.close();
logger.info("Closing Redis Connection.", { shutdownInfo: true });
CloseRedisConnection();
logger.info("Closing Score Import Queue.", { shutdownInfo: true });
CloseScoreImportQueue();
logger.info("Everything closed. Waiting for process to exit naturally.", {
shutdownInfo: true,
});
});
});
process.on("SIGTERM", HandleSIGTERMGracefully);
if (process.env.INVOKE_JOB_RUNNER) {
logger.info(`Spawning a tachi-server job runner inline.`, { bootInfo: true });