mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-07-08 03:27:20 +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
c84d1a0be1
commit
c7751a9da1
55 changed files with 883 additions and 366 deletions
72
backend/src/data/loops/expiringTempbansLoop.ts
Normal file
72
backend/src/data/loops/expiringTempbansLoop.ts
Normal file
|
@ -0,0 +1,72 @@
|
|||
import { lazyMemoize, MINUTES } from "../../utils";
|
||||
import moment from "moment-timezone";
|
||||
import { emitGuildEvent, hasGuildEventListener } from "../GuildEvents";
|
||||
import { Tempbans } from "../Tempbans";
|
||||
import { Tempban } from "../entities/Tempban";
|
||||
import Timeout = NodeJS.Timeout;
|
||||
|
||||
const LOOP_INTERVAL = 15 * MINUTES;
|
||||
const MAX_TRIES_PER_SERVER = 3;
|
||||
const getBansRepository = lazyMemoize(() => new Tempbans());
|
||||
const timeouts = new Map<string, Timeout>();
|
||||
|
||||
function tempbanToKey(tempban: Tempban) {
|
||||
return `${tempban.guild_id}/${tempban.user_id}`;
|
||||
}
|
||||
|
||||
function broadcastExpiredTempban(tempban: Tempban, tries = 0) {
|
||||
console.log(`[EXPIRING TEMPBANS LOOP] Broadcasting expired tempban: ${tempban.guild_id}/${tempban.user_id}`);
|
||||
if (!hasGuildEventListener(tempban.guild_id, "expiredTempban")) {
|
||||
// If there are no listeners registered for the server yet, try again in a bit
|
||||
if (tries < MAX_TRIES_PER_SERVER) {
|
||||
timeouts.set(
|
||||
tempbanToKey(tempban),
|
||||
setTimeout(() => broadcastExpiredTempban(tempban, tries + 1), 1 * MINUTES),
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
emitGuildEvent(tempban.guild_id, "expiredTempban", [tempban]);
|
||||
}
|
||||
|
||||
export async function runExpiringTempbansLoop() {
|
||||
console.log("[EXPIRING TEMPBANS LOOP] Clearing old timeouts");
|
||||
for (const timeout of timeouts.values()) {
|
||||
clearTimeout(timeout);
|
||||
}
|
||||
|
||||
console.log("[EXPIRING TEMPBANS LOOP] Setting timeouts for expiring tempbans");
|
||||
const expiringTempbans = await getBansRepository().getSoonExpiringTempbans(LOOP_INTERVAL);
|
||||
for (const tempban of expiringTempbans) {
|
||||
const remaining = Math.max(0, moment.utc(tempban.expires_at!).diff(moment.utc()));
|
||||
timeouts.set(
|
||||
tempbanToKey(tempban),
|
||||
setTimeout(() => broadcastExpiredTempban(tempban), remaining),
|
||||
);
|
||||
}
|
||||
|
||||
console.log("[EXPIRING TEMPBANS LOOP] Scheduling next loop");
|
||||
setTimeout(() => runExpiringTempbansLoop(), LOOP_INTERVAL);
|
||||
}
|
||||
|
||||
export function registerExpiringTempban(tempban: Tempban) {
|
||||
clearExpiringTempban(tempban);
|
||||
|
||||
console.log("[EXPIRING TEMPBANS LOOP] Registering new expiring tempban");
|
||||
const remaining = Math.max(0, moment.utc(tempban.expires_at!).diff(moment.utc()));
|
||||
if (remaining > LOOP_INTERVAL) {
|
||||
return;
|
||||
}
|
||||
|
||||
timeouts.set(
|
||||
tempbanToKey(tempban),
|
||||
setTimeout(() => broadcastExpiredTempban(tempban), remaining),
|
||||
);
|
||||
}
|
||||
|
||||
export function clearExpiringTempban(tempban: Tempban) {
|
||||
console.log("[EXPIRING TEMPBANS LOOP] Clearing expiring tempban");
|
||||
if (timeouts.has(tempbanToKey(tempban))) {
|
||||
clearTimeout(timeouts.get(tempbanToKey(tempban))!);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue