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