From 58168640666aa9ecff198d243314a9fbdf08265f Mon Sep 17 00:00:00 2001 From: Mylloon Date: Tue, 17 Sep 2024 20:54:49 +0200 Subject: [PATCH] chunk when large reminder --- src/utils/reminder.ts | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/utils/reminder.ts b/src/utils/reminder.ts index 1d065bf..c2a9912 100644 --- a/src/utils/reminder.ts +++ b/src/utils/reminder.ts @@ -202,8 +202,19 @@ export const setTimeoutReminder = ( option: OptionReminder, timeout: number, ) => { - return Number( - setTimeout(() => { + const setChunkedTimeout = (remainingTime: number) => { + // Maximum for setTimeout is Int32 + if (remainingTime > 2147483647) { + // Schedule a 24-hour delay (24 * 60 * 60 * 1000), then check again + const dayChunk = 86400000; + + return setTimeout(() => { + setChunkedTimeout(remainingTime - dayChunk); + }, dayChunk); + } + + // Final timeout when remaining time is within limit + return setTimeout(() => { deleteReminder(client, String(info.createdAt), info.userId).then((val) => { if (val != true) { throw val; @@ -211,8 +222,11 @@ export const setTimeoutReminder = ( sendReminder(client, info, option); }); - }, timeout * 1000), - ); + }, remainingTime); + }; + + // Convert to milliseconds + return Number(setChunkedTimeout(timeout * 1000)); }; /**