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