forked from MergeFi/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathidempotency-cleanup.service.ts
More file actions
32 lines (29 loc) · 1.13 KB
/
Copy pathidempotency-cleanup.service.ts
File metadata and controls
32 lines (29 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { Injectable, Logger } from '@nestjs/common';
import { Cron, CronExpression } from '@nestjs/schedule';
import { InjectRepository } from '@nestjs/typeorm';
import { LessThan, Repository } from 'typeorm';
import { IdempotencyKey } from '../entities/idempotency-key.entity';
/**
* Sweeps expired idempotency_keys rows (#16). Retention itself is set per
* row at insert time via IDEMPOTENCY_KEY_TTL_MS
* (IdempotencyInterceptor.claim) — this just reclaims storage for rows
* whose `expiresAt` has already passed; it plays no part in correctness
* (a request replaying an already-deleted key simply executes as new).
*/
@Injectable()
export class IdempotencyCleanupService {
private readonly logger = new Logger(IdempotencyCleanupService.name);
constructor(
@InjectRepository(IdempotencyKey)
private readonly repo: Repository<IdempotencyKey>,
) {}
@Cron(CronExpression.EVERY_HOUR)
async removeExpired(): Promise<void> {
const result = await this.repo.delete({
expiresAt: LessThan(new Date()),
});
if (result.affected) {
this.logger.log(`Removed ${result.affected} expired idempotency key(s)`);
}
}
}