| 299 | watcher.on('error', (err) => console.error('fs.watch error:', err.message)); |
| 300 | |
| 301 | function shutdown(reason) { |
| 302 | console.log(JSON.stringify({ type: 'server-stopped', reason })); |
| 303 | const infoFile = path.join(STATE_DIR, 'server-info'); |
| 304 | if (fs.existsSync(infoFile)) fs.unlinkSync(infoFile); |
| 305 | fs.writeFileSync( |
| 306 | path.join(STATE_DIR, 'server-stopped'), |
| 307 | JSON.stringify({ reason, timestamp: Date.now() }) + '\n' |
| 308 | ); |
| 309 | watcher.close(); |
| 310 | clearInterval(lifecycleCheck); |
| 311 | server.close(() => process.exit(0)); |
| 312 | } |
| 313 | |
| 314 | function ownerAlive() { |
| 315 | if (!ownerPid) return true; |