feat(automation): add daily briefing preset and cron backup scheduling
This commit is contained in:
+41
-6
@@ -2,6 +2,7 @@
|
||||
import { resolve } from 'path';
|
||||
import { homedir } from 'os';
|
||||
import { mkdirSync } from 'fs';
|
||||
import { Cron } from 'croner';
|
||||
|
||||
// ── Config & Types ──
|
||||
import type { Config } from '../config/index.js';
|
||||
@@ -106,7 +107,8 @@ export async function startDaemon(config: Config, options?: StartDaemonOptions):
|
||||
|
||||
if (config.backup.enabled) {
|
||||
const backupIntervalMs = parseDuration(config.backup.interval);
|
||||
if (!backupIntervalMs) {
|
||||
const backupSchedule = config.backup.schedule?.trim();
|
||||
if (!backupSchedule && !backupIntervalMs) {
|
||||
console.warn(`Backup enabled but interval is invalid: ${config.backup.interval}`);
|
||||
} else {
|
||||
let backupRunning = false;
|
||||
@@ -129,11 +131,44 @@ export async function startDaemon(config: Config, options?: StartDaemonOptions):
|
||||
}
|
||||
};
|
||||
|
||||
const backupInterval = setInterval(() => {
|
||||
void runScheduledBackup();
|
||||
}, backupIntervalMs);
|
||||
lifecycle.onShutdown(async () => { clearInterval(backupInterval); });
|
||||
console.log(`Backup scheduler enabled (${config.backup.interval})`);
|
||||
let backupCron: Cron | undefined;
|
||||
let backupInterval: ReturnType<typeof setInterval> | undefined;
|
||||
|
||||
if (backupSchedule) {
|
||||
try {
|
||||
backupCron = new Cron(backupSchedule, { paused: false }, () => {
|
||||
void runScheduledBackup();
|
||||
});
|
||||
console.log(`Backup scheduler enabled (cron: ${backupSchedule})`);
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
console.warn(`Backup cron schedule is invalid (${backupSchedule}): ${message}`);
|
||||
}
|
||||
}
|
||||
|
||||
if (!backupCron && backupIntervalMs) {
|
||||
backupInterval = setInterval(() => {
|
||||
void runScheduledBackup();
|
||||
}, backupIntervalMs);
|
||||
console.log(`Backup scheduler enabled (interval: ${config.backup.interval})`);
|
||||
}
|
||||
|
||||
if (!backupCron && !backupInterval) {
|
||||
console.warn('Backup scheduler disabled: no valid backup.schedule or backup.interval');
|
||||
} else {
|
||||
if (config.backup.run_on_start) {
|
||||
void runScheduledBackup();
|
||||
}
|
||||
|
||||
lifecycle.onShutdown(async () => {
|
||||
if (backupCron) {
|
||||
backupCron.stop();
|
||||
}
|
||||
if (backupInterval) {
|
||||
clearInterval(backupInterval);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user