(name, url, timeoutMs = 30000, proc = null)
| 793 | } |
| 794 | |
| 795 | async function waitForService(name, url, timeoutMs = 30000, proc = null) { |
| 796 | const start = Date.now(); |
| 797 | let lastError = null; |
| 798 | |
| 799 | while (Date.now() - start < timeoutMs) { |
| 800 | // A service that already exited will never answer; let the caller report |
| 801 | // the exit instead of polling until the timeout. |
| 802 | if (proc && !isProcessRunning(proc)) return false; |
| 803 | try { |
| 804 | const res = await fetch(url, { signal: AbortSignal.timeout(5000) }); |
| 805 | if (res.ok) { |
| 806 | logService(name, `Ready at ${url}`, c.green); |
| 807 | return true; |
| 808 | } |
| 809 | } catch (err) { |
| 810 | lastError = err; |
| 811 | // Keep trying |
| 812 | } |
| 813 | await delay(500); |
| 814 | } |
| 815 | |
| 816 | const elapsed = Math.round((Date.now() - start) / 1000); |
| 817 | logService(name, `Timeout waiting for ${url} after ${elapsed}s`, c.red); |
| 818 | if (lastError) { |
| 819 | logService(name, `Last error: ${lastError.message}`, c.dim); |
| 820 | } |
| 821 | return false; |
| 822 | } |
| 823 | |
| 824 | // ═══════════════════════════════════════════════════════════════════════════ |
| 825 | // Service Starters |
no test coverage detected