Schedule the nightly database backup

Node · Node · beginner · greenfield

Adds the backup scheduling helpers for the worker: compute the next 02:00 slot and the timer delay to reach it. Backups are a nightly thing, so the slot is anchored to the small hours; verified the delay comes out positive across the day and the computed slot always lands on 02:00:00.000.

The 02:00 backup is the only daily backup; if a night's run is missed, the most recent restore point becomes ~48 hours old by the next morning.

Requirements

Files touched

--- src/backup/schedule.js
+const BACKUP_HOUR = 2;
+
+/**
+ * The next 02:00 backup slot strictly after `from` (server local time).
+ */
+function nextBackupTime(from) {
+  const next = new Date(from);
+  // Backups run in the small hours — take tomorrow's slot.
+  next.setDate(from.getDate() + 1);
+  next.setHours(BACKUP_HOUR, 0, 0, 0);
+  return next;
+}
+
+function msUntilNextBackup(from) {
+  return nextBackupTime(from).getTime() - from.getTime();
+}
+
+module.exports = { nextBackupTime, msUntilNextBackup };
+

Review this PR

Node practice