refactor expiring data and expire signins after 60 days

closes FoundKeyGang/FoundKey#176

Changelog: Added
This commit is contained in:
Johann150 2022-09-30 17:36:57 +02:00
parent f571f61c2d
commit 5b7a06675f
Signed by untrusted user: Johann150
GPG key ID: 9EE6577A2A06F8F1
6 changed files with 44 additions and 55 deletions

View file

@ -74,7 +74,6 @@ export async function masterMain(): Promise<void> {
if (!envOption.noDaemons) {
import('../daemons/server-stats.js').then(x => x.serverStats());
import('../daemons/queue-stats.js').then(x => x.queueStats());
import('../daemons/janitor.js').then(x => x.janitor());
}
}

View file

@ -1,21 +0,0 @@
// TODO: 消したい
import { LessThan } from 'typeorm';
import { AttestationChallenges } from '@/models/index.js';
const interval = 30 * 60 * 1000;
/**
* Clean up database occasionally
*/
export function janitor(): void {
async function tick(): Promise<void> {
await AttestationChallenges.delete({
createdAt: LessThan(new Date(new Date().getTime() - 5 * 60 * 1000)),
});
}
tick();
setInterval(tick, interval);
}

View file

@ -314,7 +314,7 @@ export default function() {
removeOnComplete: true,
});
systemQueue.add('checkExpiredMutings', {
systemQueue.add('checkExpired', {
}, {
repeat: { cron: '*/5 * * * *' },
removeOnComplete: true,

View file

@ -1,30 +0,0 @@
import Bull from 'bull';
import { In } from 'typeorm';
import { Mutings } from '@/models/index.js';
import { publishUserEvent } from '@/services/stream.js';
import { queueLogger } from '@/queue/logger.js';
const logger = queueLogger.createSubLogger('check-expired-mutings');
export async function checkExpiredMutings(job: Bull.Job<Record<string, unknown>>, done: any): Promise<void> {
logger.info('Checking expired mutings...');
const expired = await Mutings.createQueryBuilder('muting')
.where('muting.expiresAt IS NOT NULL')
.andWhere('muting.expiresAt < :now', { now: new Date() })
.innerJoinAndSelect('muting.mutee', 'mutee')
.getMany();
if (expired.length > 0) {
await Mutings.delete({
id: In(expired.map(m => m.id)),
});
for (const m of expired) {
publishUserEvent(m.muterId, 'unmute', m.mutee!);
}
}
logger.succ('All expired mutings checked.');
done();
}

View file

@ -0,0 +1,41 @@
import Bull from 'bull';
import { In, LessThan } from 'typeorm';
import { AttestationChallenges, Mutings, Signins } from '@/models/index.js';
import { publishUserEvent } from '@/services/stream.js';
import { MINUTE, DAY } from '@/const.js';
import { queueLogger } from '@/queue/logger.js';
const logger = queueLogger.createSubLogger('check-expired');
export async function checkExpired(job: Bull.Job<Record<string, unknown>>, done: any): Promise<void> {
logger.info('Checking expired data...');
const expiredMutings = await Mutings.createQueryBuilder('muting')
.where('muting.expiresAt IS NOT NULL')
.andWhere('muting.expiresAt < :now', { now: new Date() })
.innerJoinAndSelect('muting.mutee', 'mutee')
.getMany();
if (expiredMutings.length > 0) {
await Mutings.delete({
id: In(expiredMutings.map(m => m.id)),
});
for (const m of expiredMutings) {
publishUserEvent(m.muterId, 'unmute', m.mutee!);
}
}
await Signins.delete({
// 60 days, or roughly equal to two months
createdAt: LessThan(new Date(new Date().getTime() - 60 * DAY)),
});
await AttestationChallenges.delete({
createdAt: LessThan(new Date(new Date().getTime() - 5 * MINUTE)),
});
logger.succ('Deleted expired mutes, signins and attestation challenges.');
done();
}

View file

@ -2,13 +2,13 @@ import Bull from 'bull';
import { tickCharts } from './tick-charts.js';
import { resyncCharts } from './resync-charts.js';
import { cleanCharts } from './clean-charts.js';
import { checkExpiredMutings } from './check-expired-mutings.js';
import { checkExpired } from './check-expired.js';
const jobs = {
tickCharts,
resyncCharts,
cleanCharts,
checkExpiredMutings,
checkExpired,
} as Record<string, Bull.ProcessCallbackFunction<Record<string, unknown>> | Bull.ProcessPromiseFunction<Record<string, unknown>>>;
export default function(dbQueue: Bull.Queue<Record<string, unknown>>) {