Poll `/health` until it succeeds or `readyTimeoutMs` elapses.
()
| 298 | |
| 299 | /** Poll `/health` until it succeeds or `readyTimeoutMs` elapses. */ |
| 300 | private async waitForReady(): Promise<void> { |
| 301 | const deadline = Date.now() + this.readyTimeoutMs; |
| 302 | let attempt = 0; |
| 303 | while (Date.now() < deadline) { |
| 304 | attempt++; |
| 305 | try { |
| 306 | const res = await fetch(`${this.baseUrl}/health`, { |
| 307 | signal: AbortSignal.timeout(this.readyPollIntervalMs), |
| 308 | }); |
| 309 | if (res.ok) { |
| 310 | this.logger.debug(`[hindsight] health check passed (attempt ${attempt})`); |
| 311 | return; |
| 312 | } |
| 313 | } catch { |
| 314 | // expected while the daemon is still booting |
| 315 | } |
| 316 | await new Promise((resolve) => setTimeout(resolve, this.readyPollIntervalMs)); |
| 317 | } |
| 318 | throw new Error( |
| 319 | `Hindsight daemon did not become ready within ${this.readyTimeoutMs}ms at ${this.baseUrl}` |
| 320 | ); |
| 321 | } |
| 322 | } |