Files
Homelab-manager/server/src/services/weeklyDigestScheduler.ts
T
bobbanandClaude Sonnet 5.5 aa94d84f6c Add a weekly digest and report which version of the agent each server runs
- Weekly digest through the notification channels: alerts now, a week of uptime, what happened, disks
  heading for full, expiring secrets and domains, updates waiting, planned maintenance. Off until switched
  on in Settings -> Notifications; Preview and Send it now there. Setting key 'digest'.
- Agents say their version in every report (Linux and Windows scripts are version 2); the server compares
  it with the scripts it serves and marks agents that are behind on the Servers, server and Updates pages.
  servers.agent_version, migration 0026.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-17 19:49:36 +02:00

34 lines
1.3 KiB
TypeScript

import schedule from "node-schedule";
import { getSettings } from "./settingsStore.js";
import { sendDigest } from "./weeklyDigest.js";
let currentJob: schedule.Job | null = null;
/** The cron rule for "this weekday (0 = Sunday) at HH:MM". */
export function cronForDigest(day: number, time: string): string {
const [h, m] = time.split(":").map(Number);
const d = Number.isInteger(day) && day >= 0 && day <= 6 ? day : 1;
return `${Number.isFinite(m) ? m : 0} ${Number.isFinite(h) ? h : 8} * * ${d}`;
}
/** (Re)schedules the weekly digest per the current settings. Call again after they change. */
export async function scheduleWeeklyDigest(): Promise<void> {
if (currentJob) {
currentJob.cancel();
currentJob = null;
}
const { digest, notifications } = await getSettings();
if (!digest.enabled) {
console.log("[digest] weekly digest is off, nothing scheduled");
return;
}
currentJob = schedule.scheduleJob({ rule: cronForDigest(digest.day, digest.time), tz: notifications.timezone }, () => {
sendDigest().catch((err) => console.error("[digest] couldn't send the weekly digest:", err));
});
console.log(`Weekly digest scheduled: ${cronForDigest(digest.day, digest.time)} (${notifications.timezone})`);
}
export async function initWeeklyDigestScheduler(): Promise<void> {
await scheduleWeeklyDigest();
}