From 1932a7506e484e13711ddb62b2bba0f67772394d Mon Sep 17 00:00:00 2001 From: Thomas Bowen <6600748+ttbowen@users.noreply.github.com> Date: Mon, 21 Oct 2024 00:21:06 +0100 Subject: [PATCH] Add a max reschedule --- .../src/client/activity/activity-scheduler.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/mrwhale-discord/src/client/activity/activity-scheduler.ts b/packages/mrwhale-discord/src/client/activity/activity-scheduler.ts index 92e5183..6880353 100644 --- a/packages/mrwhale-discord/src/client/activity/activity-scheduler.ts +++ b/packages/mrwhale-discord/src/client/activity/activity-scheduler.ts @@ -4,6 +4,7 @@ import { Activities } from "../../types/activities/activities"; const NEXT_ACTIVITY_RUN_INTERVAL = 1000; const ONE_HOUR_IN_MS = 60 * 60 * 1000; +const MAX_RESCHEDULES = 5; // Limit the number of reschedules to prevent infinite loops /** * The activity scheduler is responsible for scheduling and running events. @@ -85,6 +86,7 @@ export class ActivityScheduler { // Loop to check and reschedule until no overlap exists with previous or next activities let hasConflict: boolean; + let rescheduleCount = 0; do { hasConflict = false; @@ -95,6 +97,14 @@ export class ActivityScheduler { if (this.handleNextActivityConflict(activity)) { hasConflict = true; } + + rescheduleCount++; + if (rescheduleCount >= MAX_RESCHEDULES) { + this.botClient.logger.error( + `Failed to schedule activity ${activity.name} in guild ${activity.guildId} after ${MAX_RESCHEDULES} reschedules.` + ); + return false; + } } while (hasConflict); this.activities.push(activity);