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
- The database backup runs once a day at 02:00, server local time. The worker image bakes in `TZ=UTC`, so server local time is UTC and has no daylight-saving transitions — every day contains exactly one 02:00. Add the scheduling helpers the worker uses to arm its timer.
- `nextBackupTime(from)` returns the first 02:00 occurrence strictly after `from`, as a `Date` with time 02:00:00.000 exactly. In particular: if `from` is before 02:00 on some day, the next backup is at 02:00 that SAME day; if `from` is at or after 02:00, it is 02:00 the following day.
- `msUntilNextBackup(from)` returns the number of milliseconds from `from` until that occurrence (always > 0); the worker passes it straight to `setTimeout`.
- The worker process restarts whenever the service is deployed, and deploys commonly happen between midnight and 02:00 — the helper must schedule correctly from any time of day.
Files touched
- src/backup/schedule.js
--- 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 };
+