3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-10 12:25:02 +00:00

Add username/nickname history retention periods

This commit is contained in:
Dragory 2020-06-02 00:26:06 +03:00
parent a6e650810c
commit de71520747
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
8 changed files with 253 additions and 111 deletions

View file

@ -7,68 +7,16 @@ import moment from "moment-timezone";
import { DAYS, DBDateFormat, MINUTES, SECONDS } from "../utils";
import { isAPI } from "../globals";
import { connection } from "./db";
const CLEANUP_INTERVAL = 5 * MINUTES;
/**
* How long message edits, deletions, etc. will include the original message content.
* This is very heavy storage-wise, so keeping it as low as possible is ideal.
*/
const RETENTION_PERIOD = 1 * DAYS;
const BOT_MESSAGE_RETENTION_PERIOD = 30 * MINUTES;
const CLEAN_PER_LOOP = 250;
async function cleanup() {
const repo = getRepository(SavedMessage);
const deletedAtThreshold = moment()
.subtract(CLEANUP_INTERVAL, "ms")
.format(DBDateFormat);
const postedAtThreshold = moment()
.subtract(RETENTION_PERIOD, "ms")
.format(DBDateFormat);
const botPostedAtThreshold = moment()
.subtract(BOT_MESSAGE_RETENTION_PERIOD, "ms")
.format(DBDateFormat);
// SELECT + DELETE messages in batches
// This is to avoid deadlocks that happened frequently when deleting with the same criteria as the select below
// when a message was being inserted at the same time
let rows;
do {
rows = await connection.query(
`
SELECT id
FROM messages
WHERE (
deleted_at IS NOT NULL
AND deleted_at <= ?
)
OR (
posted_at <= ?
AND is_permanent = 0
)
OR (
is_bot = 1
AND posted_at <= ?
AND is_permanent = 0
)
LIMIT ${CLEAN_PER_LOOP}
`,
[deletedAtThreshold, postedAtThreshold, botPostedAtThreshold],
);
if (rows.length > 0) {
await repo.delete({
id: In(rows.map(r => r.id)),
});
}
} while (rows.length === CLEAN_PER_LOOP);
setTimeout(cleanup, CLEANUP_INTERVAL);
}
import { cleanupMessages } from "./cleanup/messages";
if (!isAPI()) {
const CLEANUP_INTERVAL = 5 * MINUTES;
async function cleanup() {
await cleanupMessages();
setTimeout(cleanup, CLEANUP_INTERVAL);
}
// Start first cleanup 30 seconds after startup
setTimeout(cleanup, 30 * SECONDS);
}