openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 910 B
JavaScript
import{writeSync}from"node:fs";import{parentPort,receiveMessageOnPort,workerData}from"node:worker_threads";
//!src/cli/gateway-cli/shutdown-hard-exit.worker.mjs
const deadlineNs=workerData?.deadlineNs;if(!parentPort||typeof deadlineNs!==`bigint`)throw new Error(`Shutdown watchdog requires its monotonic deadline`);const port=parentPort;let pending=workerData.pending;let cancelled=false;const receive=e=>{if(e===`cancel`){cancelled=true;clearTimeout(timer);port.close()}else if(typeof e===`string`)pending=e};const delayMs=Math.max(0,Number(deadlineNs-process.hrtime.bigint())/1e6);const timer=setTimeout(()=>{for(let e=receiveMessageOnPort(port);e;e=receiveMessageOnPort(port))receive(e.message);if(!cancelled)try{writeSync(2,`hard-exit watchdog deadline reached; pending close steps: ${pending}; forcing SIGKILL\n`)}finally{process.kill(process.pid,`SIGKILL`)}},delayMs);port.on(`message`,receive);export{};