mirror of
https://github.com/zkldi/Tachi.git
synced 2026-10-08 22:59:52 +03:00
Add graceful sigterm handling to workers
This commit is contained in:
@@ -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
@@ -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 });
|
||||
|
||||
Reference in New Issue
Block a user