1
0
mirror of https://github.com/MisskeyIO/misskey synced 2024-12-23 19:18:44 +09:00
MisskeyIO/packages/backend/src/daemons/JanitorService.ts

41 lines
1.0 KiB
TypeScript
Raw Normal View History

2022-09-18 03:27:08 +09:00
import { Inject, Injectable } from '@nestjs/common';
import { LessThan } from 'typeorm';
import { DI } from '@/di-symbols.js';
2022-09-21 05:33:11 +09:00
import type { AttestationChallengesRepository } from '@/models/index.js';
import { bindThis } from '@/decorators.js';
2022-09-18 03:27:08 +09:00
import type { OnApplicationShutdown } from '@nestjs/common';
const interval = 30 * 60 * 1000;
@Injectable()
export class JanitorService implements OnApplicationShutdown {
2022-09-19 03:11:50 +09:00
private intervalId: NodeJS.Timer;
2022-09-18 03:27:08 +09:00
constructor(
@Inject(DI.attestationChallengesRepository)
private attestationChallengesRepository: AttestationChallengesRepository,
) {
}
/**
* Clean up database occasionally
*/
@bindThis
2022-09-18 03:27:08 +09:00
public start(): void {
const tick = async () => {
await this.attestationChallengesRepository.delete({
createdAt: LessThan(new Date(new Date().getTime() - 5 * 60 * 1000)),
});
};
tick();
2022-09-19 03:11:50 +09:00
this.intervalId = setInterval(tick, interval);
2022-09-18 03:27:08 +09:00
}
@bindThis
2022-09-18 03:27:08 +09:00
public onApplicationShutdown(signal?: string | undefined) {
2022-09-19 03:11:50 +09:00
clearInterval(this.intervalId);
2022-09-18 03:27:08 +09:00
}
}