BlueGit

timfa questward

Show the restart countdown for the whole scheduled window.

Players see the update toast as soon as a midnight restart is planned, not only in the last three hours.

timfa Tim Falken · pushed by timfa

Authored 2026-08-31 · 2 months ago

Hash
89e7b5c1fca8b3fdd11b4d7051597fd77e076190
Parent
489ba160
3 files changed +15 −22
server/src/deploy/scheduler.js +6 −15
@@ -1,14 +1,13 @@
1 1 import {
2 2 RESTART_WARN_BEFORE_MS,
3 3 DEFAULT_DEPLOY_CHECK_MS,
4 nextLocalMidnightMs,
5 isInRestartWarningWindow
4 nextLocalMidnightMs
6 5 } from "../../../shared/deploy/schedule.js";
7 6 import { checkGitRemoteAhead } from "./gitRemote.js";
8 7
9 8 /**
10 9 * Schedules a graceful midnight restart only when origin/<branch> is ahead of HEAD.
11 * Three hours before midnight, clients enter the permanent countdown toast window.
10 * Clients see a permanent countdown toast for the whole time a restart is scheduled.
12 11 */
13 12 export class DeployScheduler
14 13 {
@@ -86,9 +85,7 @@ export class DeployScheduler
86 85 {
87 86 const now = this.now();
88 87 const scheduled = this.scheduledAt != null;
89 const warning = scheduled && (
90 this.warningActive || isInRestartWarningWindow(now, this.scheduledAt, this.warnBeforeMs)
91 );
88 const warning = scheduled && now < this.scheduledAt;
92 89 return {
93 90 scheduled,
94 91 scheduledAt: this.scheduledAt,
@@ -129,16 +126,10 @@ export class DeployScheduler
129 126 if (!this.enabled || this.restartFired || this.scheduledAt == null)
130 127 { return; }
131 128 const now = this.now();
132 const warning = isInRestartWarningWindow(now, this.scheduledAt, this.warnBeforeMs)
133 || now >= this.scheduledAt;
134 if (warning !== this.warningActive)
135 {
136 this.warningActive = warning;
137 this.onStatusChange(this.status());
138 }
139 129 if (now >= this.scheduledAt)
140 130 {
141 131 this.restartFired = true;
132 this.warningActive = true;
142 133 this.onRestartDue(this.status());
143 134 }
144 135 }
@@ -148,7 +139,7 @@ export class DeployScheduler
148 139 {
149 140 this.scheduledAt = scheduledAt;
150 141 this.reasons = [...reasons];
151 this.warningActive = isInRestartWarningWindow(this.now(), scheduledAt, this.warnBeforeMs);
142 this.warningActive = scheduledAt != null && this.now() < scheduledAt;
152 143 this.onStatusChange(this.status());
153 144 }
154 145
@@ -158,7 +149,7 @@ export class DeployScheduler
158 149 { return; }
159 150 this.scheduledAt = nextLocalMidnightMs(this.now());
160 151 this.reasons = ["code-update"];
161 this.warningActive = isInRestartWarningWindow(this.now(), this.scheduledAt, this.warnBeforeMs);
152 this.warningActive = this.now() < this.scheduledAt;
162 153 console.log(
163 154 `[deploy] origin/${this.branch} is ahead; restart scheduled for ${new Date(this.scheduledAt).toString()}`
164 155 );
@@ -1,14 +1,13 @@
1import {
1import {
2 RESTART_WARN_BEFORE_MS,
2 RESTART_WARN_BEFORE_MS,
3 DEFAULT_DEPLOY_CHECK_MS,
3 DEFAULT_DEPLOY_CHECK_MS,
4 nextLocalMidnightMs,
4 nextLocalMidnightMs
5 isInRestartWarningWindow
6} from "../../../shared/deploy/schedule.js";
5} from "../../../shared/deploy/schedule.js";
7import { checkGitRemoteAhead } from "./gitRemote.js";
6import { checkGitRemoteAhead } from "./gitRemote.js";
8
7
9/**
8/**
10 * Schedules a graceful midnight restart only when origin/<branch> is ahead of HEAD.
9 * Schedules a graceful midnight restart only when origin/<branch> is ahead of HEAD.
11 * Three hours before midnight, clients enter the permanent countdown toast window.
10 * Clients see a permanent countdown toast for the whole time a restart is scheduled.
12 */
11 */
13export class DeployScheduler
12export class DeployScheduler
14{
13{
@@ -86,9 +85,7 @@ export class DeployScheduler
86 {
85 {
87 const now = this.now();
86 const now = this.now();
88 const scheduled = this.scheduledAt != null;
87 const scheduled = this.scheduledAt != null;
89 const warning = scheduled && (
88 const warning = scheduled && now < this.scheduledAt;
90 this.warningActive || isInRestartWarningWindow(now, this.scheduledAt, this.warnBeforeMs)
91 );
92 return {
89 return {
93 scheduled,
90 scheduled,
94 scheduledAt: this.scheduledAt,
91 scheduledAt: this.scheduledAt,
@@ -129,16 +126,10 @@ export class DeployScheduler
129 if (!this.enabled || this.restartFired || this.scheduledAt == null)
126 if (!this.enabled || this.restartFired || this.scheduledAt == null)
130 { return; }
127 { return; }
131 const now = this.now();
128 const now = this.now();
132 const warning = isInRestartWarningWindow(now, this.scheduledAt, this.warnBeforeMs)
133 || now >= this.scheduledAt;
134 if (warning !== this.warningActive)
135 {
136 this.warningActive = warning;
137 this.onStatusChange(this.status());
138 }
139 if (now >= this.scheduledAt)
129 if (now >= this.scheduledAt)
140 {
130 {
141 this.restartFired = true;
131 this.restartFired = true;
132 this.warningActive = true;
142 this.onRestartDue(this.status());
133 this.onRestartDue(this.status());
143 }
134 }
144 }
135 }
@@ -148,7 +139,7 @@ export class DeployScheduler
148 {
139 {
149 this.scheduledAt = scheduledAt;
140 this.scheduledAt = scheduledAt;
150 this.reasons = [...reasons];
141 this.reasons = [...reasons];
151 this.warningActive = isInRestartWarningWindow(this.now(), scheduledAt, this.warnBeforeMs);
142 this.warningActive = scheduledAt != null && this.now() < scheduledAt;
152 this.onStatusChange(this.status());
143 this.onStatusChange(this.status());
153 }
144 }
154
145
@@ -158,7 +149,7 @@ export class DeployScheduler
158 { return; }
149 { return; }
159 this.scheduledAt = nextLocalMidnightMs(this.now());
150 this.scheduledAt = nextLocalMidnightMs(this.now());
160 this.reasons = ["code-update"];
151 this.reasons = ["code-update"];
161 this.warningActive = isInRestartWarningWindow(this.now(), this.scheduledAt, this.warnBeforeMs);
152 this.warningActive = this.now() < this.scheduledAt;
162 console.log(
153 console.log(
163 `[deploy] origin/${this.branch} is ahead; restart scheduled for ${new Date(this.scheduledAt).toString()}`
154 `[deploy] origin/${this.branch} is ahead; restart scheduled for ${new Date(this.scheduledAt).toString()}`
164 );
155 );
server/tests/deploy.test.js +5 −4
@@ -31,12 +31,13 @@ test("nextLocalMidnightMs is the following calendar midnight", () =>
31 31 assert.equal(d.getMinutes(), 0);
32 32 });
33 33
34 test("warning window opens three hours before midnight", () =>
34 test("warning window stays open for the whole scheduled period", () =>
35 35 {
36 36 const scheduledAt = new Date(2026, 7, 28, 0, 0, 0, 0).getTime();
37 assert.equal(restartWarningStartsAt(scheduledAt), scheduledAt - RESTART_WARN_BEFORE_MS);
37 assert.equal(restartWarningStartsAt(scheduledAt), 0);
38 38 assert.equal(isInRestartWarningWindow(scheduledAt - RESTART_WARN_BEFORE_MS, scheduledAt), true);
39 assert.equal(isInRestartWarningWindow(scheduledAt - RESTART_WARN_BEFORE_MS - 1, scheduledAt), false);
39 assert.equal(isInRestartWarningWindow(scheduledAt - RESTART_WARN_BEFORE_MS - 1, scheduledAt), true);
40 assert.equal(isInRestartWarningWindow(scheduledAt - 12 * 60 * 60 * 1000, scheduledAt), true);
40 41 assert.equal(isInRestartWarningWindow(scheduledAt, scheduledAt), false);
41 42 });
42 43
@@ -89,7 +90,7 @@ test("DeployScheduler schedules only when remote is ahead", async () =>
89 90 await scheduler.poll();
90 91 assert.equal(scheduler.status().scheduled, true);
91 92 assert.equal(scheduler.status().scheduledAt, nextLocalMidnightMs(now));
92 assert.equal(scheduler.status().warning, false);
93 assert.equal(scheduler.status().warning, true);
93 94
94 95 now = scheduler.status().scheduledAt - RESTART_WARN_BEFORE_MS;
95 96 scheduler.tick();
@@ -31,12 +31,13 @@ test("nextLocalMidnightMs is the following calendar midnight", () =>
31 assert.equal(d.getMinutes(), 0);
31 assert.equal(d.getMinutes(), 0);
32});
32});
33
33
34test("warning window opens three hours before midnight", () =>
34test("warning window stays open for the whole scheduled period", () =>
35{
35{
36 const scheduledAt = new Date(2026, 7, 28, 0, 0, 0, 0).getTime();
36 const scheduledAt = new Date(2026, 7, 28, 0, 0, 0, 0).getTime();
37 assert.equal(restartWarningStartsAt(scheduledAt), scheduledAt - RESTART_WARN_BEFORE_MS);
37 assert.equal(restartWarningStartsAt(scheduledAt), 0);
38 assert.equal(isInRestartWarningWindow(scheduledAt - RESTART_WARN_BEFORE_MS, scheduledAt), true);
38 assert.equal(isInRestartWarningWindow(scheduledAt - RESTART_WARN_BEFORE_MS, scheduledAt), true);
39 assert.equal(isInRestartWarningWindow(scheduledAt - RESTART_WARN_BEFORE_MS - 1, scheduledAt), false);
39 assert.equal(isInRestartWarningWindow(scheduledAt - RESTART_WARN_BEFORE_MS - 1, scheduledAt), true);
40 assert.equal(isInRestartWarningWindow(scheduledAt - 12 * 60 * 60 * 1000, scheduledAt), true);
40 assert.equal(isInRestartWarningWindow(scheduledAt, scheduledAt), false);
41 assert.equal(isInRestartWarningWindow(scheduledAt, scheduledAt), false);
41});
42});
42
43
@@ -89,7 +90,7 @@ test("DeployScheduler schedules only when remote is ahead", async () =>
89 await scheduler.poll();
90 await scheduler.poll();
90 assert.equal(scheduler.status().scheduled, true);
91 assert.equal(scheduler.status().scheduled, true);
91 assert.equal(scheduler.status().scheduledAt, nextLocalMidnightMs(now));
92 assert.equal(scheduler.status().scheduledAt, nextLocalMidnightMs(now));
92 assert.equal(scheduler.status().warning, false);
93 assert.equal(scheduler.status().warning, true);
93
94
94 now = scheduler.status().scheduledAt - RESTART_WARN_BEFORE_MS;
95 now = scheduler.status().scheduledAt - RESTART_WARN_BEFORE_MS;
95 scheduler.tick();
96 scheduler.tick();
shared/deploy/schedule.js +4 −3
@@ -1,5 +1,6 @@
1 1 /** Shared helpers for midnight deploy / restart scheduling (server-local calendar). */
2 2
3 /** @deprecated Warning is active for the whole scheduled window; kept for callers/tests. */
3 4 export const RESTART_WARN_BEFORE_MS = 3 * 60 * 60 * 1000;
4 5 export const DEFAULT_DEPLOY_CHECK_MS = 5 * 60 * 1000;
5 6
@@ -11,13 +12,13 @@ export function nextLocalMidnightMs (now = Date.now())
11 12 return next.getTime();
12 13 }
13 14
14 export function restartWarningStartsAt (scheduledAt, warnBeforeMs = RESTART_WARN_BEFORE_MS)
15 export function restartWarningStartsAt (scheduledAt, _warnBeforeMs = RESTART_WARN_BEFORE_MS)
15 16 {
16 17 const at = Number(scheduledAt);
17 const warn = Number(warnBeforeMs);
18 18 if (!Number.isFinite(at))
19 19 { return 0; }
20 return Math.max(0, at - (Number.isFinite(warn) ? warn : RESTART_WARN_BEFORE_MS));
20 // Show the countdown for the entire time a restart is scheduled (not only the last N hours).
21 return 0;
21 22 }
22 23
23 24 export function isInRestartWarningWindow (now, scheduledAt, warnBeforeMs = RESTART_WARN_BEFORE_MS)
@@ -1,5 +1,6 @@
1/** Shared helpers for midnight deploy / restart scheduling (server-local calendar). */
1/** Shared helpers for midnight deploy / restart scheduling (server-local calendar). */
2
2
3/** @deprecated Warning is active for the whole scheduled window; kept for callers/tests. */
3export const RESTART_WARN_BEFORE_MS = 3 * 60 * 60 * 1000;
4export const RESTART_WARN_BEFORE_MS = 3 * 60 * 60 * 1000;
4export const DEFAULT_DEPLOY_CHECK_MS = 5 * 60 * 1000;
5export const DEFAULT_DEPLOY_CHECK_MS = 5 * 60 * 1000;
5
6
@@ -11,13 +12,13 @@ export function nextLocalMidnightMs (now = Date.now())
11 return next.getTime();
12 return next.getTime();
12}
13}
13
14
14export function restartWarningStartsAt (scheduledAt, warnBeforeMs = RESTART_WARN_BEFORE_MS)
15export function restartWarningStartsAt (scheduledAt, _warnBeforeMs = RESTART_WARN_BEFORE_MS)
15{
16{
16 const at = Number(scheduledAt);
17 const at = Number(scheduledAt);
17 const warn = Number(warnBeforeMs);
18 if (!Number.isFinite(at))
18 if (!Number.isFinite(at))
19 { return 0; }
19 { return 0; }
20 return Math.max(0, at - (Number.isFinite(warn) ? warn : RESTART_WARN_BEFORE_MS));
20 // Show the countdown for the entire time a restart is scheduled (not only the last N hours).
21 return 0;
21}
22}
22
23
23export function isInRestartWarningWindow (now, scheduledAt, warnBeforeMs = RESTART_WARN_BEFORE_MS)
24export function isInRestartWarningWindow (now, scheduledAt, warnBeforeMs = RESTART_WARN_BEFORE_MS)