mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-03-16 14:11:50 +00:00
Fix deletion limit in message cleanup
This commit is contained in:
parent
bf3cae2201
commit
d781c6c3b4
1 changed files with 17 additions and 25 deletions
|
@ -6,6 +6,7 @@ import { GuildChannel, Message } from "eris";
|
||||||
import moment from "moment-timezone";
|
import moment from "moment-timezone";
|
||||||
import { DAYS, MINUTES } from "../utils";
|
import { DAYS, MINUTES } from "../utils";
|
||||||
import { isAPI } from "../globals";
|
import { isAPI } from "../globals";
|
||||||
|
import { connection } from "./db";
|
||||||
|
|
||||||
const CLEANUP_INTERVAL = 1 * MINUTES;
|
const CLEANUP_INTERVAL = 1 * MINUTES;
|
||||||
|
|
||||||
|
@ -17,34 +18,25 @@ const RETENTION_PERIOD = 1 * DAYS;
|
||||||
const BOT_MESSAGE_RETENTION_PERIOD = 30 * MINUTES;
|
const BOT_MESSAGE_RETENTION_PERIOD = 30 * MINUTES;
|
||||||
|
|
||||||
async function cleanup() {
|
async function cleanup() {
|
||||||
const repository = getRepository(SavedMessage);
|
const query = `
|
||||||
await repository
|
DELETE FROM messages
|
||||||
.createQueryBuilder("messages")
|
WHERE (
|
||||||
.where(
|
deleted_at IS NOT NULL
|
||||||
// Clear deleted messages
|
AND deleted_at <= (NOW() - INTERVAL ${CLEANUP_INTERVAL}000 MICROSECOND)
|
||||||
new Brackets(qb => {
|
|
||||||
qb.where("deleted_at IS NOT NULL");
|
|
||||||
qb.andWhere(`deleted_at <= (NOW() - INTERVAL ${CLEANUP_INTERVAL}000 MICROSECOND)`);
|
|
||||||
}),
|
|
||||||
)
|
)
|
||||||
.orWhere(
|
OR (
|
||||||
// Clear old messages
|
posted_at <= (NOW() - INTERVAL ${RETENTION_PERIOD}000 MICROSECOND)
|
||||||
new Brackets(qb => {
|
AND is_permanent = 0
|
||||||
qb.where(`posted_at <= (NOW() - INTERVAL ${RETENTION_PERIOD}000 MICROSECOND)`);
|
|
||||||
qb.andWhere("is_permanent = 0");
|
|
||||||
}),
|
|
||||||
)
|
)
|
||||||
.orWhere(
|
OR (
|
||||||
// Clear old bot messages
|
is_bot = 1
|
||||||
new Brackets(qb => {
|
AND posted_at <= (NOW() - INTERVAL ${BOT_MESSAGE_RETENTION_PERIOD}000 MICROSECOND)
|
||||||
qb.where("is_bot = 1");
|
AND is_permanent = 0
|
||||||
qb.andWhere(`posted_at <= (NOW() - INTERVAL ${BOT_MESSAGE_RETENTION_PERIOD}000 MICROSECOND)`);
|
|
||||||
qb.andWhere("is_permanent = 0");
|
|
||||||
}),
|
|
||||||
)
|
)
|
||||||
.limit(10_000) // To avoid long table locks, limit the amount of messages deleted at once
|
LIMIT ${50_000}
|
||||||
.delete()
|
`;
|
||||||
.execute();
|
|
||||||
|
await connection.query(query);
|
||||||
|
|
||||||
setTimeout(cleanup, CLEANUP_INTERVAL);
|
setTimeout(cleanup, CLEANUP_INTERVAL);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue