Centralize periodic checks for mutes, tempbans, vcalerts, reminders, and scheduled posts
This should result in a significant performance improvement. The new method is also more precise than the old one, allowing the aforementioned checks to be performed with second-precision.
This commit is contained in:
parent
c84d1a0be1
commit
c7751a9da1
55 changed files with 883 additions and 366 deletions
67
backend/src/data/loops/upcomingRemindersLoop.ts
Normal file
67
backend/src/data/loops/upcomingRemindersLoop.ts
Normal file
|
@ -0,0 +1,67 @@
|
|||
import { lazyMemoize, MINUTES } from "../../utils";
|
||||
import moment from "moment-timezone";
|
||||
import { emitGuildEvent, hasGuildEventListener } from "../GuildEvents";
|
||||
import { Reminder } from "../entities/Reminder";
|
||||
import { Reminders } from "../Reminders";
|
||||
import Timeout = NodeJS.Timeout;
|
||||
|
||||
const LOOP_INTERVAL = 15 * MINUTES;
|
||||
const MAX_TRIES_PER_SERVER = 3;
|
||||
const getRemindersRepository = lazyMemoize(() => new Reminders());
|
||||
const timeouts = new Map<number, Timeout>();
|
||||
|
||||
function broadcastReminder(reminder: Reminder, tries = 0) {
|
||||
if (!hasGuildEventListener(reminder.guild_id, "reminder")) {
|
||||
// If there are no listeners registered for the server yet, try again in a bit
|
||||
if (tries < MAX_TRIES_PER_SERVER) {
|
||||
timeouts.set(
|
||||
reminder.id,
|
||||
setTimeout(() => broadcastReminder(reminder, tries + 1), 1 * MINUTES),
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
emitGuildEvent(reminder.guild_id, "reminder", [reminder]);
|
||||
}
|
||||
|
||||
export async function runUpcomingRemindersLoop() {
|
||||
console.log("[REMINDERS LOOP] Clearing old timeouts");
|
||||
for (const timeout of timeouts.values()) {
|
||||
clearTimeout(timeout);
|
||||
}
|
||||
|
||||
console.log("[REMINDERS LOOP] Setting timeouts for upcoming reminders");
|
||||
const remindersDueSoon = await getRemindersRepository().getRemindersDueSoon(LOOP_INTERVAL);
|
||||
for (const reminder of remindersDueSoon) {
|
||||
const remaining = Math.max(0, moment.utc(reminder.remind_at!).diff(moment.utc()));
|
||||
timeouts.set(
|
||||
reminder.id,
|
||||
setTimeout(() => broadcastReminder(reminder), remaining),
|
||||
);
|
||||
}
|
||||
|
||||
console.log("[REMINDERS LOOP] Scheduling next loop");
|
||||
setTimeout(() => runUpcomingRemindersLoop(), LOOP_INTERVAL);
|
||||
}
|
||||
|
||||
export function registerUpcomingReminder(reminder: Reminder) {
|
||||
clearUpcomingReminder(reminder);
|
||||
|
||||
console.log("[REMINDERS LOOP] Registering new upcoming reminder");
|
||||
const remaining = Math.max(0, moment.utc(reminder.remind_at!).diff(moment.utc()));
|
||||
if (remaining > LOOP_INTERVAL) {
|
||||
return;
|
||||
}
|
||||
|
||||
timeouts.set(
|
||||
reminder.id,
|
||||
setTimeout(() => broadcastReminder(reminder), remaining),
|
||||
);
|
||||
}
|
||||
|
||||
export function clearUpcomingReminder(reminder: Reminder) {
|
||||
console.log("[REMINDERS LOOP] Clearing upcoming reminder");
|
||||
if (timeouts.has(reminder.id)) {
|
||||
clearTimeout(timeouts.get(reminder.id)!);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue