Skip to content

Commit

Permalink
Add a max reschedule
Browse files Browse the repository at this point in the history
  • Loading branch information
ttbowen committed Oct 20, 2024
1 parent fd5a947 commit 1932a75
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/mrwhale-discord/src/client/activity/activity-scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;

Expand All @@ -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);
Expand Down

0 comments on commit 1932a75

Please sign in to comment.