From d6c367f2832bb676ef9d187687f9b35dd112ffe1 Mon Sep 17 00:00:00 2001 From: sup39 Date: Fri, 19 Jan 2024 03:41:45 +0900 Subject: [PATCH] fix: shutdown gracefully --- packages/backend/src/misc/before-shutdown.ts | 5 +++-- packages/backend/src/services/chart/index.ts | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/backend/src/misc/before-shutdown.ts b/packages/backend/src/misc/before-shutdown.ts index 08204183..6d10b1d2 100644 --- a/packages/backend/src/misc/before-shutdown.ts +++ b/packages/backend/src/misc/before-shutdown.ts @@ -6,6 +6,7 @@ * @callback BeforeShutdownListener * @param {string} [signalOrEvent] The exit signal or event name received on the process. */ +type BeforeShutdownListener = (signalOrEvent: string) => Promise; /** * System signals the app will listen to initiate shutdown. @@ -24,7 +25,7 @@ const SHUTDOWN_TIMEOUT = 15000; * down the process. * @type {BeforeShutdownListener[]} */ -const shutdownListeners: ((signalOrEvent: string) => void)[] = []; +const shutdownListeners: BeforeShutdownListener[] = []; /** * Listen for signals and execute given `fn` function once. @@ -89,7 +90,7 @@ async function shutdownHandler(signalOrEvent: string) { * @param {BeforeShutdownListener} listener The shutdown listener to register. * @returns {BeforeShutdownListener} Echoes back the supplied `listener`. */ -export function beforeShutdown(listener: () => void) { +export function beforeShutdown(listener: BeforeShutdownListener): BeforeShutdownListener { shutdownListeners.push(listener); return listener; } diff --git a/packages/backend/src/services/chart/index.ts b/packages/backend/src/services/chart/index.ts index 4b8a9278..b084941f 100644 --- a/packages/backend/src/services/chart/index.ts +++ b/packages/backend/src/services/chart/index.ts @@ -6,4 +6,4 @@ export const activeUsersChart = new ActiveUsersChart(); // 20分おきにメモリ情報をDBに書き込み setInterval(() => activeUsersChart.save(), 1000 * 60 * 20); -beforeShutdown(async () => await new Promise(() => activeUsersChart.save())); +beforeShutdown(() => activeUsersChart.save());