eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
1 lines • 1.05 kB
JavaScript
const FORCED_EXIT_BACKSTOP_MS=900;function installShutdownSignal(e={}){let t=new AbortController,n=Promise.withResolvers(),r=!1,i=0,a=0,o,stop=i=>{r||(r=!0,e.onStop?.(),t.abort(i),n.resolve(i),e.exitAfterMs!==void 0&&(o=setTimeout(()=>process.exit(i===void 0?0:i===`SIGINT`?130:143),e.exitAfterMs)))},handleSignal=e=>{a>0||(i+=1,i>1&&process.exit(e===`SIGINT`?130:143),stop(e))},onSigint=()=>handleSignal(`SIGINT`),onSigterm=()=>handleSignal(`SIGTERM`);return process.on(`SIGINT`,onSigint),process.on(`SIGTERM`,onSigterm),{signal:t.signal,stopped:n.promise,async withTerminalLease(e){a+=1;try{return await e()}finally{--a}},requestStop:()=>stop(),dispose(){o!==void 0&&clearTimeout(o),process.off(`SIGINT`,onSigint),process.off(`SIGTERM`,onSigterm)}}}async function waitForShutdownSignal(e){let t=installShutdownSignal();try{await Promise.race([t.stopped.then(()=>`signal`),e.wait?.().then(()=>`stopped`)??new Promise(()=>{})])===`signal`&&await e.close()}finally{t.dispose()}}export{FORCED_EXIT_BACKSTOP_MS,installShutdownSignal,waitForShutdownSignal};