Health Check Liveness And Readiness
Implement liveness and readiness probes with dependency checks and timeout for Kubernetes orchestration integration.
Code
export function healthChecks({checks={},timeout=5000}={}){async function runChecks(){const results={};for(const[name,check]of Object.entries(checks)){try{const r=await Promise.race([check(),new Promise((_,rej)=>setTimeout(()=>rej(new Error('timeout')),timeout))]);results[name]={status:'up',detail:r};}catch(e){results[name]={status:'down',error:e.message};}}return results;}return{async liveness(){return{status:'alive',ts:Date.now()};},async readiness(){const r=await runChecks();const allUp=Object.values(r).every(x=>x.status==='up');return{status:allUp?'ready':'not ready',checks:r};}};}Report a problem with this exampleOpens a pre-filled issue on GitHub.