diff options
Diffstat (limited to 'packages/backend/src/daemons/JanitorService.ts')
| -rw-r--r-- | packages/backend/src/daemons/JanitorService.ts | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/packages/backend/src/daemons/JanitorService.ts b/packages/backend/src/daemons/JanitorService.ts new file mode 100644 index 0000000000..dacbb8cca6 --- /dev/null +++ b/packages/backend/src/daemons/JanitorService.ts @@ -0,0 +1,37 @@ +import { Inject, Injectable } from '@nestjs/common'; +import { LessThan } from 'typeorm'; +import { DI } from '@/di-symbols.js'; +import { AttestationChallengesRepository } from '@/models/index.js'; +import type { OnApplicationShutdown } from '@nestjs/common'; + +const interval = 30 * 60 * 1000; + +@Injectable() +export class JanitorService implements OnApplicationShutdown { + #intervalId: NodeJS.Timer; + + constructor( + @Inject(DI.attestationChallengesRepository) + private attestationChallengesRepository: AttestationChallengesRepository, + ) { + } + + /** + * Clean up database occasionally + */ + public start(): void { + const tick = async () => { + await this.attestationChallengesRepository.delete({ + createdAt: LessThan(new Date(new Date().getTime() - 5 * 60 * 1000)), + }); + }; + + tick(); + + this.#intervalId = setInterval(tick, interval); + } + + public onApplicationShutdown(signal?: string | undefined) { + clearInterval(this.#intervalId); + } +} |