diff --git a/backend/src/data/Archives.ts b/backend/src/data/Archives.ts new file mode 100644 index 00000000..06ed0760 --- /dev/null +++ b/backend/src/data/Archives.ts @@ -0,0 +1,21 @@ +import { getRepository, Repository } from "typeorm"; +import { ArchiveEntry } from "./entities/ArchiveEntry"; +import { BaseRepository } from "./BaseRepository"; + +export class Archives extends BaseRepository { + protected archives: Repository; + + constructor() { + super(); + this.archives = getRepository(ArchiveEntry); + } + + public deleteExpiredArchives() { + this.archives + .createQueryBuilder() + .andWhere("expires_at IS NOT NULL") + .andWhere("expires_at <= NOW()") + .delete() + .execute(); + } +} diff --git a/backend/src/data/GuildArchives.ts b/backend/src/data/GuildArchives.ts index a84817ae..6bff7eaa 100644 --- a/backend/src/data/GuildArchives.ts +++ b/backend/src/data/GuildArchives.ts @@ -27,20 +27,6 @@ export class GuildArchives extends BaseGuildRepository { constructor(guildId) { super(guildId); this.archives = getRepository(ArchiveEntry); - - // Clean expired archives at start and then every hour - this.deleteExpiredArchives(); - setInterval(() => this.deleteExpiredArchives(), 1000 * 60 * 60); - } - - private deleteExpiredArchives() { - this.archives - .createQueryBuilder() - .where("guild_id = :guild_id", { guild_id: this.guildId }) - .andWhere("expires_at IS NOT NULL") - .andWhere("expires_at <= NOW()") - .delete() - .execute(); } async find(id: string): Promise { diff --git a/backend/src/data/loops/expiredArchiveDeletionLoop.ts b/backend/src/data/loops/expiredArchiveDeletionLoop.ts new file mode 100644 index 00000000..c3f3c61b --- /dev/null +++ b/backend/src/data/loops/expiredArchiveDeletionLoop.ts @@ -0,0 +1,12 @@ +import { lazyMemoize, MINUTES } from "../../utils"; +import { Archives } from "../Archives"; +import moment from "moment-timezone"; + +const LOOP_INTERVAL = 15 * MINUTES; +const getArchivesRepository = lazyMemoize(() => new Archives()); + +export async function runExpiredArchiveDeletionLoop() { + console.log("[EXPIRED ARCHIVE DELETION LOOP] Deleting expired archives"); + await getArchivesRepository().deleteExpiredArchives(); + setTimeout(() => runExpiredArchiveDeletionLoop(), LOOP_INTERVAL); +} diff --git a/backend/src/index.ts b/backend/src/index.ts index 76ec4c1c..f360be4b 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -17,7 +17,7 @@ import { RecoverablePluginError } from "./RecoverablePluginError"; import { SimpleError } from "./SimpleError"; import { ZeppelinGlobalConfig, ZeppelinGuildConfig } from "./types"; import { startUptimeCounter } from "./uptime"; -import { errorMessage, isDiscordAPIError, isDiscordHTTPError, SECONDS, successMessage } from "./utils"; +import { errorMessage, isDiscordAPIError, isDiscordHTTPError, SECONDS, sleep, successMessage } from "./utils"; import { loadYamlSafely } from "./utils/loadYamlSafely"; import { DecayingCounter } from "./utils/DecayingCounter"; import { PluginNotLoadedError } from "knub/dist/plugins/PluginNotLoadedError"; @@ -28,6 +28,7 @@ import { runUpcomingRemindersLoop } from "./data/loops/upcomingRemindersLoop"; import { runUpcomingScheduledPostsLoop } from "./data/loops/upcomingScheduledPostsLoop"; import { runExpiringTempbansLoop } from "./data/loops/expiringTempbansLoop"; import { runExpiringVCAlertsLoop } from "./data/loops/expiringVCAlertsLoop"; +import { runExpiredArchiveDeletionLoop } from "./data/loops/expiredArchiveDeletionLoop"; if (!process.env.KEY) { // tslint:disable-next-line:no-console @@ -327,12 +328,18 @@ connect().then(async () => { logRateLimit(data); }); - bot.on("loadingFinished", () => { + bot.on("loadingFinished", async () => { runExpiringMutesLoop(); + await sleep(10 * SECONDS); runExpiringTempbansLoop(); - runExpiringVCAlertsLoop(); - runUpcomingRemindersLoop(); + await sleep(10 * SECONDS); runUpcomingScheduledPostsLoop(); + await sleep(10 * SECONDS); + runUpcomingRemindersLoop(); + await sleep(10 * SECONDS); + runExpiringVCAlertsLoop(); + await sleep(10 * SECONDS); + runExpiredArchiveDeletionLoop(); }); bot.initialize();