Health Check Liveness And Readiness

Implement liveness and readiness probes with dependency checks and timeout for Kubernetes orchestration integration.

JavascriptResilience

Code

javascriptsnippet
1 line
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};}};}

Inside the snippet

Health Check Liveness And Readiness keeps everything in the functions healthChecks and runChecks.

Asynchronous boundaries use await, so failures surface as rejected promises at the call site instead of being swallowed.

Using it

The main signature reads export function healthChecks({checks={},timeout=5000}={})

Import healthChecks from the file and call it — the rest of the code is implementation detail.

Interactive Tools for This Topic

FAQ

Anything to download besides the language toolchain?
Nothing beyond the toolchain you already have for this language. The file defines `healthChecks`, `runChecks` at load time.