BlueGit

timfa questward

Files

questward / shared / deploy / schedule.js

schedule.js

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