|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const pollHealthCheck = async ( |
|
|
healthUrl: string, |
|
|
maxAttempts: number = 60, |
|
|
intervalMs: number = 1000, |
|
|
onProgress?: (attempt: number, maxAttempts: number) => void |
|
|
): Promise<void> => { |
|
|
console.log(`Starting health check for: ${healthUrl}`); |
|
|
|
|
|
for (let attempt = 1; attempt <= maxAttempts; attempt++) { |
|
|
try { |
|
|
if (onProgress) { |
|
|
onProgress(attempt, maxAttempts); |
|
|
} |
|
|
|
|
|
const response = await fetch(healthUrl, { |
|
|
method: 'GET', |
|
|
headers: { |
|
|
'Cache-Control': 'no-cache', |
|
|
}, |
|
|
|
|
|
signal: AbortSignal.timeout(5000), |
|
|
}); |
|
|
|
|
|
if (response.ok) { |
|
|
console.log(`Health check passed on attempt ${attempt}`); |
|
|
return; |
|
|
} |
|
|
|
|
|
console.log(`Health check attempt ${attempt}/${maxAttempts} failed with status ${response.status}`); |
|
|
} catch (error) { |
|
|
console.log(`Health check attempt ${attempt}/${maxAttempts} failed:`, error instanceof Error ? error.message : error); |
|
|
} |
|
|
|
|
|
|
|
|
if (attempt < maxAttempts) { |
|
|
await new Promise(resolve => setTimeout(resolve, intervalMs)); |
|
|
} |
|
|
} |
|
|
|
|
|
throw new Error(`Health check failed after ${maxAttempts} attempts. Server may not be ready.`); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const waitForServerReady = async ( |
|
|
healthUrl: string, |
|
|
maxAttempts: number = 60, |
|
|
intervalMs: number = 1000, |
|
|
onProgress?: (attempt: number, maxAttempts: number) => void |
|
|
): Promise<void> => { |
|
|
return pollHealthCheck(healthUrl, maxAttempts, intervalMs, onProgress); |
|
|
}; |
|
|
|