mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-10 12:25:02 +00:00
Centralize archive deletion loop; stagger different centralized loops slightly
This commit is contained in:
parent
2d84a02d48
commit
617a4ea7fc
4 changed files with 44 additions and 18 deletions
21
backend/src/data/Archives.ts
Normal file
21
backend/src/data/Archives.ts
Normal file
|
@ -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<ArchiveEntry>;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.archives = getRepository(ArchiveEntry);
|
||||
}
|
||||
|
||||
public deleteExpiredArchives() {
|
||||
this.archives
|
||||
.createQueryBuilder()
|
||||
.andWhere("expires_at IS NOT NULL")
|
||||
.andWhere("expires_at <= NOW()")
|
||||
.delete()
|
||||
.execute();
|
||||
}
|
||||
}
|
|
@ -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<ArchiveEntry | undefined> {
|
||||
|
|
12
backend/src/data/loops/expiredArchiveDeletionLoop.ts
Normal file
12
backend/src/data/loops/expiredArchiveDeletionLoop.ts
Normal file
|
@ -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);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue