feat(backup): scheduled snapshot worker wired into server lifecycle

This commit is contained in:
Jannis Braun
2026-06-20 02:32:17 +02:00
parent 154feb2dd6
commit 9407e48293
3 changed files with 71 additions and 0 deletions
+33
View File
@@ -0,0 +1,33 @@
import { config } from '../config.js';
import { getRawDb } from '../db/index.js';
import { createSnapshot, pruneSnapshots } from './backup.js';
let timer: ReturnType<typeof setInterval> | null = null;
export function startBackupWorker(): void {
if (config.backup.disabled) {
console.log('[backup] scheduled worker disabled via BACKUP_DISABLED');
return;
}
if (timer) return;
const intervalMs = config.backup.intervalHours * 60 * 60 * 1000;
timer = setInterval(() => {
try {
const snap = createSnapshot(getRawDb(), 'scheduled');
pruneSnapshots();
console.log(`[backup] scheduled snapshot written: ${snap}`);
} catch (err) {
console.error(`[backup] scheduled snapshot failed: ${(err as Error).message}`);
}
}, intervalMs);
// Do not keep the event loop alive solely for backups.
if (typeof timer.unref === 'function') timer.unref();
console.log(`[backup] scheduled worker started (every ${config.backup.intervalHours}h)`);
}
export function stopBackupWorker(): void {
if (timer) {
clearInterval(timer);
timer = null;
}
}