BlueGit

timfa questward

Files

questward / server / src / deploy / scheduler.js

scheduler.js

scheduler.js 4.6 KiB Download Raw
1import {
2 RESTART_WARN_BEFORE_MS,
3 DEFAULT_DEPLOY_CHECK_MS,
4 nextLocalMidnightMs,
5 isInRestartWarningWindow
6} from "../../../shared/deploy/schedule.js";
7import { checkGitRemoteAhead } from "./gitRemote.js";
8
9/**
10 * 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.
12 */
13export class DeployScheduler
14{
15 /**
16 * @param {{
17 * repoRoot: string,
18 * branch?: string,
19 * checkIntervalMs?: number,
20 * warnBeforeMs?: number,
21 * enabled?: boolean,
22 * now?: () => number,
23 * checkRemote?: (repoRoot: string, branch: string) => Promise<object>,
24 * onStatusChange?: (status: object) => void,
25 * onRestartDue?: (status: object) => void
26 * }} options
27 */
28 constructor (options)
29 {
30 this.repoRoot = options.repoRoot;
31 this.branch = options.branch ?? process.env.TILE_MMO_GIT_BRANCH ?? "master";
32 this.checkIntervalMs = Math.max(
33 30_000,
34 Number(options.checkIntervalMs ?? process.env.TILE_MMO_DEPLOY_CHECK_MS) || DEFAULT_DEPLOY_CHECK_MS
35 );
36 this.warnBeforeMs = options.warnBeforeMs ?? RESTART_WARN_BEFORE_MS;
37 this.enabled = options.enabled ?? process.env.TILE_MMO_DEPLOY_SCHEDULER !== "0";
38 this.now = options.now ?? (() => Date.now());
39 this.checkRemote = options.checkRemote ?? checkGitRemoteAhead;
40 this.onStatusChange = options.onStatusChange ?? (() => {});
41 this.onRestartDue = options.onRestartDue ?? (() => {});
42
43 this.scheduledAt = null;
44 this.reasons = [];
45 this.localCommit = null;
46 this.remoteCommit = null;
47 this.warningActive = false;
48 this.restartFired = false;
49 this.#checkTimer = null;
50 this.#tickTimer = null;
51 this.#checking = false;
52 }
53
54 #checkTimer;
55 #tickTimer;
56 #checking;
57
58 start ()
59 {
60 if (!this.enabled)
61 { return; }
62 void this.poll();
63 this.#checkTimer = setInterval(() => { void this.poll(); }, Math.max(30_000, this.checkIntervalMs));
64 this.#tickTimer = setInterval(() => this.tick(), 1000);
65 }
66
67 stop ()
68 {
69 if (this.#checkTimer)
70 { clearInterval(this.#checkTimer); }
71 if (this.#tickTimer)
72 { clearInterval(this.#tickTimer); }
73 this.#checkTimer = null;
74 this.#tickTimer = null;
75 }
76
77 status ()
78 {
79 const now = this.now();
80 const scheduled = this.scheduledAt != null;
81 const warning = scheduled && (
82 this.warningActive || isInRestartWarningWindow(now, this.scheduledAt, this.warnBeforeMs)
83 );
84 return {
85 scheduled,
86 scheduledAt: this.scheduledAt,
87 warning: !!warning,
88 reasons: [...this.reasons],
89 localCommit: this.localCommit,
90 remoteCommit: this.remoteCommit,
91 branch: this.branch,
92 remainingMs: scheduled ? Math.max(0, this.scheduledAt - now) : null
93 };
94 }
95
96 async poll ()
97 {
98 if (!this.enabled || this.#checking || this.restartFired)
99 { return; }
100 this.#checking = true;
101 try
102 {
103 const result = await this.checkRemote(this.repoRoot, this.branch);
104 if (!result?.ok)
105 { return; }
106 this.localCommit = result.local ?? this.localCommit;
107 this.remoteCommit = result.remote ?? this.remoteCommit;
108 if (result.ahead)
109 { this.#scheduleCodeUpdate(); }
110 else if (this.scheduledAt != null && this.reasons.includes("code-update"))
111 { this.#clearSchedule(); }
112 }
113 finally
114 {
115 this.#checking = false;
116 }
117 }
118
119 tick ()
120 {
121 if (!this.enabled || this.restartFired || this.scheduledAt == null)
122 { return; }
123 const now = this.now();
124 const warning = isInRestartWarningWindow(now, this.scheduledAt, this.warnBeforeMs)
125 || now >= this.scheduledAt;
126 if (warning !== this.warningActive)
127 {
128 this.warningActive = warning;
129 this.onStatusChange(this.status());
130 }
131 if (now >= this.scheduledAt)
132 {
133 this.restartFired = true;
134 this.onRestartDue(this.status());
135 }
136 }
137
138 /** Test helper: force a pending midnight restart. */
139 forceScheduleForTests (scheduledAt, reasons = ["code-update"])
140 {
141 this.scheduledAt = scheduledAt;
142 this.reasons = [...reasons];
143 this.warningActive = isInRestartWarningWindow(this.now(), scheduledAt, this.warnBeforeMs);
144 this.onStatusChange(this.status());
145 }
146
147 #scheduleCodeUpdate ()
148 {
149 if (this.scheduledAt != null)
150 { return; }
151 this.scheduledAt = nextLocalMidnightMs(this.now());
152 this.reasons = ["code-update"];
153 this.warningActive = isInRestartWarningWindow(this.now(), this.scheduledAt, this.warnBeforeMs);
154 console.log(
155 `[deploy] origin/${this.branch} is ahead; restart scheduled for ${new Date(this.scheduledAt).toString()}`
156 );
157 this.onStatusChange(this.status());
158 }
159
160 #clearSchedule ()
161 {
162 this.scheduledAt = null;
163 this.reasons = [];
164 this.warningActive = false;
165 console.log("[deploy] Remote no longer ahead; cancelled scheduled restart.");
166 this.onStatusChange(this.status());
167 }
168}