import{setTimeout as c}from"node:timers/promises";import{logger as s}from"./notifiers/logger.js";const u=5e3;class h{#t=[];#r=null;constructor(){process.on("SIGTERM",()=>this.exitWithCode(0))}registerShutdownCallback(r){this.#t.push(r)}async exitWithCode(r,t){if(this.#r)return this.#r;this.#r=new Promise(()=>{});const e=await Promise.race([Promise.allSettled(this.#t.map(i=>a(i,t).catch(n=>s.error("Shutdown handler callback error: ",n.message)))),c(u,"Graceful shutdown timeout")]);typeof e=="string"&&e==="Graceful shutdown timeout"&&s.error(e),process.exit(r)}}function a(o,r){try{return Promise.resolve(o(r))}catch(t){return Promise.reject(t)}}const d=new h;export{d as shutdowner};
