* Check master endpoint health, retrying up to 3 times.
(masterEndpoint: string)
| 61 | * Check master endpoint health, retrying up to 3 times. |
| 62 | */ |
| 63 | async function checkMasterHealth(masterEndpoint: string) { |
| 64 | const MAX_RETRIES = 3; |
| 65 | let retryCount = 0; |
| 66 | while (retryCount < MAX_RETRIES) { |
| 67 | retryCount++; |
| 68 | try { |
| 69 | const response = await fetch(`${masterEndpoint}/health`, { |
| 70 | method: 'GET', |
| 71 | signal: AbortSignal.timeout(10000) // 10 second timeout |
| 72 | }); |
| 73 | |
| 74 | if (!response.ok) { |
| 75 | throw new Error(`Health check returned status ${response.status}`); |
| 76 | } |
| 77 | if (retryCount > 1) { |
| 78 | console.debug(`[K8s] Successfully reached master after retry (attempt ${retryCount}/${MAX_RETRIES}): ${masterEndpoint}`); |
| 79 | } |
| 80 | return true; |
| 81 | } catch (error: any) { |
| 82 | console.error(error); |
| 83 | console.debug(`[K8s] Health check error (attempt ${retryCount}/${MAX_RETRIES}): ${error instanceof Error ? error.message : error} - ${masterEndpoint}`); |
| 84 | if (retryCount < MAX_RETRIES) { |
| 85 | // Wait briefly before retrying |
| 86 | await sleep(3); |
| 87 | continue; |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | console.error(`[K8s] Failed to reach master after ${MAX_RETRIES} attempts: ${masterEndpoint}`); |
| 93 | if (!ApiConfig.isMasterNode()) { |
| 94 | // If not master node, exit the process |
| 95 | process.exit(1); |
| 96 | } |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | |
| 101 | function addCorsHeaders(reply: any) { |
no test coverage detected