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:
Dragory 2021-09-25 21:33:59 +03:00
parent c84d1a0be1
commit c7751a9da1
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
55 changed files with 883 additions and 366 deletions

View file

@ -0,0 +1,72 @@
import { lazyMemoize, memoize, MINUTES } from "../../utils";
import { Mutes } from "../Mutes";
import Timeout = NodeJS.Timeout;
import moment from "moment-timezone";
import { Mute } from "../entities/Mute";
import { emitGuildEvent, hasGuildEventListener } from "../GuildEvents";
const LOOP_INTERVAL = 15 * MINUTES;
const MAX_TRIES_PER_SERVER = 3;
const getMutesRepository = lazyMemoize(() => new Mutes());
const timeouts = new Map<string, Timeout>();
function muteToKey(mute: Mute) {
return `${mute.guild_id}/${mute.user_id}`;
}
function broadcastExpiredMute(mute: Mute, tries = 0) {
console.log(`[EXPIRING MUTES LOOP] Broadcasting expired mute: ${mute.guild_id}/${mute.user_id}`);
if (!hasGuildEventListener(mute.guild_id, "expiredMute")) {
// If there are no listeners registered for the server yet, try again in a bit
if (tries < MAX_TRIES_PER_SERVER) {
timeouts.set(
muteToKey(mute),
setTimeout(() => broadcastExpiredMute(mute, tries + 1), 1 * MINUTES),
);
}
return;
}
emitGuildEvent(mute.guild_id, "expiredMute", [mute]);
}
export async function runExpiringMutesLoop() {
console.log("[EXPIRING MUTES LOOP] Clearing old timeouts");
for (const timeout of timeouts.values()) {
clearTimeout(timeout);
}
console.log("[EXPIRING MUTES LOOP] Setting timeouts for expiring mutes");
const expiringMutes = await getMutesRepository().getSoonExpiringMutes(LOOP_INTERVAL);
for (const mute of expiringMutes) {
const remaining = Math.max(0, moment.utc(mute.expires_at!).diff(moment.utc()));
timeouts.set(
muteToKey(mute),
setTimeout(() => broadcastExpiredMute(mute), remaining),
);
}
console.log("[EXPIRING MUTES LOOP] Scheduling next loop");
setTimeout(() => runExpiringMutesLoop(), LOOP_INTERVAL);
}
export function registerExpiringMute(mute: Mute) {
clearExpiringMute(mute);
console.log("[EXPIRING MUTES LOOP] Registering new expiring mute");
const remaining = Math.max(0, moment.utc(mute.expires_at!).diff(moment.utc()));
if (remaining > LOOP_INTERVAL) {
return;
}
timeouts.set(
muteToKey(mute),
setTimeout(() => broadcastExpiredMute(mute), remaining),
);
}
export function clearExpiringMute(mute: Mute) {
console.log("[EXPIRING MUTES LOOP] Clearing expiring mute");
if (timeouts.has(muteToKey(mute))) {
clearTimeout(timeouts.get(muteToKey(mute))!);
}
}

View 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))!);
}
}

View 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)!);
}
}

View 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)!);
}
}

View file

@ -0,0 +1,67 @@
import { lazyMemoize, MINUTES } from "../../utils";
import moment from "moment-timezone";
import { emitGuildEvent, hasGuildEventListener } from "../GuildEvents";
import { ScheduledPosts } from "../ScheduledPosts";
import { ScheduledPost } from "../entities/ScheduledPost";
import Timeout = NodeJS.Timeout;
const LOOP_INTERVAL = 15 * MINUTES;
const MAX_TRIES_PER_SERVER = 3;
const getScheduledPostsRepository = lazyMemoize(() => new ScheduledPosts());
const timeouts = new Map<number, Timeout>();
function broadcastScheduledPost(post: ScheduledPost, tries = 0) {
if (!hasGuildEventListener(post.guild_id, "scheduledPost")) {
// If there are no listeners registered for the server yet, try again in a bit
if (tries < MAX_TRIES_PER_SERVER) {
timeouts.set(
post.id,
setTimeout(() => broadcastScheduledPost(post, tries + 1), 1 * MINUTES),
);
}
return;
}
emitGuildEvent(post.guild_id, "scheduledPost", [post]);
}
export async function runUpcomingScheduledPostsLoop() {
console.log("[SCHEDULED POSTS LOOP] Clearing old timeouts");
for (const timeout of timeouts.values()) {
clearTimeout(timeout);
}
console.log("[SCHEDULED POSTS LOOP] Setting timeouts for upcoming scheduled posts");
const postsDueSoon = await getScheduledPostsRepository().getScheduledPostsDueSoon(LOOP_INTERVAL);
for (const post of postsDueSoon) {
const remaining = Math.max(0, moment.utc(post.post_at!).diff(moment.utc()));
timeouts.set(
post.id,
setTimeout(() => broadcastScheduledPost(post), remaining),
);
}
console.log("[SCHEDULED POSTS LOOP] Scheduling next loop");
setTimeout(() => runUpcomingScheduledPostsLoop(), LOOP_INTERVAL);
}
export function registerUpcomingScheduledPost(post: ScheduledPost) {
clearUpcomingScheduledPost(post);
console.log("[SCHEDULED POSTS LOOP] Registering new upcoming scheduled post");
const remaining = Math.max(0, moment.utc(post.post_at!).diff(moment.utc()));
if (remaining > LOOP_INTERVAL) {
return;
}
timeouts.set(
post.id,
setTimeout(() => broadcastScheduledPost(post), remaining),
);
}
export function clearUpcomingScheduledPost(post: ScheduledPost) {
console.log("[SCHEDULED POSTS LOOP] Clearing upcoming scheduled post");
if (timeouts.has(post.id)) {
clearTimeout(timeouts.get(post.id)!);
}
}