-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathauditLogPruner.ts
More file actions
71 lines (54 loc) · 2.19 KB
/
auditLogPruner.ts
File metadata and controls
71 lines (54 loc) · 2.19 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { PrismaClient } from "@sourcebot/db";
import { createLogger, env } from "@sourcebot/shared";
import { setIntervalAsync } from "../utils.js";
const BATCH_SIZE = 10_000;
const ONE_DAY_MS = 24 * 60 * 60 * 1000;
const logger = createLogger('audit-log-pruner');
export class AuditLogPruner {
private interval?: NodeJS.Timeout;
constructor(private db: PrismaClient) {}
startScheduler() {
if (env.SOURCEBOT_EE_AUDIT_LOGGING_ENABLED !== 'true') {
logger.info('Audit logging is disabled, skipping audit log pruner.');
return;
}
if (env.SOURCEBOT_EE_AUDIT_RETENTION_DAYS <= 0) {
logger.info('SOURCEBOT_EE_AUDIT_RETENTION_DAYS is 0, audit log pruning is disabled.');
return;
}
logger.info(`Audit log pruner started. Retaining logs for ${env.SOURCEBOT_EE_AUDIT_RETENTION_DAYS} days.`);
// Run immediately on startup, then every 24 hours
this.pruneOldAuditLogs();
this.interval = setIntervalAsync(() => this.pruneOldAuditLogs(), ONE_DAY_MS);
}
async dispose() {
if (this.interval) {
clearInterval(this.interval);
this.interval = undefined;
}
}
private async pruneOldAuditLogs() {
const cutoff = new Date(Date.now() - env.SOURCEBOT_EE_AUDIT_RETENTION_DAYS * ONE_DAY_MS);
let totalDeleted = 0;
logger.info(`Pruning audit logs older than ${cutoff.toISOString()}...`);
// Delete in batches to avoid long-running transactions
while (true) {
const batch = await this.db.audit.findMany({
where: { timestamp: { lt: cutoff } },
select: { id: true },
take: BATCH_SIZE,
});
if (batch.length === 0) break;
const result = await this.db.audit.deleteMany({
where: { id: { in: batch.map(r => r.id) } },
});
totalDeleted += result.count;
if (batch.length < BATCH_SIZE) break;
}
if (totalDeleted > 0) {
logger.info(`Pruned ${totalDeleted} audit log records.`);
} else {
logger.info('No audit log records to prune.');
}
}
}