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};}};}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
Kubernetes Resource CalculatorCalculate per-pod and cluster CPU and memory requests, limits, QoS class, overcommit ratios, headroom, and node fit estimates for Kubernetes workloads.HTTP Retry & Backoff CalculatorSimulate exponential backoff retry schedules with Full, Equal, Decorrelated, or No Jitter to inspect per-attempt wait times and cumulative latency.
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.
Report a problem with this exampleOpens a pre-filled issue on GitHub.