| 1 | /** Shared helpers for midnight deploy / restart scheduling (server-local calendar). */ |
| 2 | |
| 3 | /** @deprecated Warning is active for the whole scheduled window; kept for callers/tests. */ |
| 4 | export const RESTART_WARN_BEFORE_MS = 3 * 60 * 60 * 1000; |
| 5 | export const DEFAULT_DEPLOY_CHECK_MS = 5 * 60 * 1000; |
| 6 | |
| 7 | /** Next local calendar midnight strictly after `now`. */ |
| 8 | export function nextLocalMidnightMs (now = Date.now()) |
| 9 | { |
| 10 | const d = new Date(now); |
| 11 | const next = new Date(d.getFullYear(), d.getMonth(), d.getDate() + 1, 0, 0, 0, 0); |
| 12 | return next.getTime(); |
| 13 | } |
| 14 | |
| 15 | export function restartWarningStartsAt (scheduledAt, _warnBeforeMs = RESTART_WARN_BEFORE_MS) |
| 16 | { |
| 17 | const at = Number(scheduledAt); |
| 18 | if (!Number.isFinite(at)) |
| 19 | { return 0; } |
| 20 | // Show the countdown for the entire time a restart is scheduled (not only the last N hours). |
| 21 | return 0; |
| 22 | } |
| 23 | |
| 24 | export function isInRestartWarningWindow (now, scheduledAt, warnBeforeMs = RESTART_WARN_BEFORE_MS) |
| 25 | { |
| 26 | if (scheduledAt == null) |
| 27 | { return false; } |
| 28 | const at = Number(scheduledAt); |
| 29 | if (!Number.isFinite(at)) |
| 30 | { return false; } |
| 31 | const t = Number(now); |
| 32 | return t >= restartWarningStartsAt(at, warnBeforeMs) && t < at; |
| 33 | } |
| 34 | |
| 35 | /** Human countdown like `2h 05m 03s` or `45s`. */ |
| 36 | export function formatRestartCountdown (remainingMs) |
| 37 | { |
| 38 | const ms = Math.max(0, Math.ceil(Number(remainingMs) || 0)); |
| 39 | const totalSec = Math.floor(ms / 1000); |
| 40 | const h = Math.floor(totalSec / 3600); |
| 41 | const m = Math.floor((totalSec % 3600) / 60); |
| 42 | const s = totalSec % 60; |
| 43 | if (h > 0) |
| 44 | { return `${h}h ${String(m).padStart(2, "0")}m ${String(s).padStart(2, "0")}s`; } |
| 45 | if (m > 0) |
| 46 | { return `${m}m ${String(s).padStart(2, "0")}s`; } |
| 47 | return `${s}s`; |
| 48 | } |
| 49 | |
| 50 | export function restartCountdownMessage (remainingMs) |
| 51 | { |
| 52 | return `Server restart for a game update in ${formatRestartCountdown(remainingMs)}.`; |
| 53 | } |