mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-23 17:45:03 +00:00
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
544363058e
commit
076d69b989
55 changed files with 883 additions and 366 deletions
68
backend/src/data/loops/expiringVCAlertsLoop.ts
Normal file
68
backend/src/data/loops/expiringVCAlertsLoop.ts
Normal file
|
@ -0,0 +1,68 @@
|
|||
import { lazyMemoize, MINUTES } from "../../utils";
|
||||
import moment from "moment-timezone";
|
||||
import { emitGuildEvent, hasGuildEventListener } from "../GuildEvents";
|
||||
import Timeout = NodeJS.Timeout;
|
||||
import { VCAlerts } from "../VCAlerts";
|
||||
import { VCAlert } from "../entities/VCAlert";
|
||||
|
||||
const LOOP_INTERVAL = 15 * MINUTES;
|
||||
const MAX_TRIES_PER_SERVER = 3;
|
||||
const getVCAlertsRepository = lazyMemoize(() => new VCAlerts());
|
||||
const timeouts = new Map<number, Timeout>();
|
||||
|
||||
function broadcastExpiredVCAlert(alert: VCAlert, tries = 0) {
|
||||
console.log(`[EXPIRING VCALERTS LOOP] Broadcasting expired vcalert: ${alert.guild_id}/${alert.user_id}`);
|
||||
if (!hasGuildEventListener(alert.guild_id, "expiredVCAlert")) {
|
||||
// If there are no listeners registered for the server yet, try again in a bit
|
||||
if (tries < MAX_TRIES_PER_SERVER) {
|
||||
timeouts.set(
|
||||
alert.id,
|
||||
setTimeout(() => broadcastExpiredVCAlert(alert, tries + 1), 1 * MINUTES),
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
emitGuildEvent(alert.guild_id, "expiredVCAlert", [alert]);
|
||||
}
|
||||
|
||||
export async function runExpiringVCAlertsLoop() {
|
||||
console.log("[EXPIRING VCALERTS LOOP] Clearing old timeouts");
|
||||
for (const timeout of timeouts.values()) {
|
||||
clearTimeout(timeout);
|
||||
}
|
||||
|
||||
console.log("[EXPIRING VCALERTS LOOP] Setting timeouts for expiring vcalerts");
|
||||
const expiringVCAlerts = await getVCAlertsRepository().getSoonExpiringAlerts(LOOP_INTERVAL);
|
||||
for (const alert of expiringVCAlerts) {
|
||||
const remaining = Math.max(0, moment.utc(alert.expires_at!).diff(moment.utc()));
|
||||
timeouts.set(
|
||||
alert.id,
|
||||
setTimeout(() => broadcastExpiredVCAlert(alert), remaining),
|
||||
);
|
||||
}
|
||||
|
||||
console.log("[EXPIRING VCALERTS LOOP] Scheduling next loop");
|
||||
setTimeout(() => runExpiringVCAlertsLoop(), LOOP_INTERVAL);
|
||||
}
|
||||
|
||||
export function registerExpiringVCAlert(alert: VCAlert) {
|
||||
clearExpiringVCAlert(alert);
|
||||
|
||||
console.log("[EXPIRING VCALERTS LOOP] Registering new expiring vcalert");
|
||||
const remaining = Math.max(0, moment.utc(alert.expires_at!).diff(moment.utc()));
|
||||
if (remaining > LOOP_INTERVAL) {
|
||||
return;
|
||||
}
|
||||
|
||||
timeouts.set(
|
||||
alert.id,
|
||||
setTimeout(() => broadcastExpiredVCAlert(alert), remaining),
|
||||
);
|
||||
}
|
||||
|
||||
export function clearExpiringVCAlert(alert: VCAlert) {
|
||||
console.log("[EXPIRING VCALERTS LOOP] Clearing expiring vcalert");
|
||||
if (timeouts.has(alert.id)) {
|
||||
clearTimeout(timeouts.get(alert.id)!);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue