feat: add periodic database cleanup every 6 hours (TDD)
All checks were successful
Build & Deploy to Staging / Build & Deploy to Staging (push) Successful in 18m15s
All checks were successful
Build & Deploy to Staging / Build & Deploy to Staging (push) Successful in 18m15s
- Cleans expired verifications and orphaned usage rows - Previously only ran once on startup (13d+ uptime = accumulation) - Interval uses .unref() to not block graceful shutdown - Stopped during shutdown before pool.end() - Idempotent start (safe to call multiple times) - 6 TDD tests added (periodic-cleanup.test.ts) - 663 tests total, all passing
This commit is contained in:
parent
75c6a6ce58
commit
cc7de5ef49
3 changed files with 135 additions and 0 deletions
30
src/utils/periodic-cleanup.ts
Normal file
30
src/utils/periodic-cleanup.ts
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
import { cleanupStaleData } from "../services/db.js";
|
||||
import logger from "../services/logger.js";
|
||||
|
||||
export const CLEANUP_INTERVAL_MS = 6 * 60 * 60 * 1000; // 6 hours
|
||||
|
||||
let intervalHandle: ReturnType<typeof setInterval> | null = null;
|
||||
|
||||
export function startPeriodicCleanup(): void {
|
||||
// Idempotent — don't create duplicate intervals
|
||||
if (intervalHandle !== null) return;
|
||||
|
||||
intervalHandle = setInterval(async () => {
|
||||
try {
|
||||
logger.info("Running periodic database cleanup...");
|
||||
await cleanupStaleData();
|
||||
} catch (err: unknown) {
|
||||
logger.error({ err }, "Periodic database cleanup failed (non-fatal)");
|
||||
}
|
||||
}, CLEANUP_INTERVAL_MS);
|
||||
|
||||
// Don't prevent graceful shutdown
|
||||
intervalHandle.unref();
|
||||
}
|
||||
|
||||
export function stopPeriodicCleanup(): void {
|
||||
if (intervalHandle !== null) {
|
||||
clearInterval(intervalHandle);
|
||||
intervalHandle = null;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue