@fefeding/aicmd 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/server.js +15 -2
package/package.json
CHANGED
package/server.js
CHANGED
|
@@ -306,10 +306,19 @@ for (let i = 0; i < process.argv.length; i++) {
|
|
|
306
306
|
}
|
|
307
307
|
}
|
|
308
308
|
|
|
309
|
+
// PID 文件路径
|
|
310
|
+
const pidDir = process.env.AICMD_DATA_DIR || path.join(require('os').homedir(), '.aicmd');
|
|
311
|
+
try { fs.mkdirSync(pidDir, { recursive: true }); } catch {}
|
|
312
|
+
const pidFilePath = path.join(pidDir, 'aicmd.server.pid');
|
|
313
|
+
|
|
314
|
+
// 进程退出时清理 PID 文件
|
|
315
|
+
function cleanupPid() {
|
|
316
|
+
try { if (fs.existsSync(pidFilePath)) fs.unlinkSync(pidFilePath); } catch {}
|
|
317
|
+
}
|
|
318
|
+
|
|
309
319
|
// 启动服务器
|
|
310
320
|
const PORT = portFromArgs || process.env.PORT || 9802;
|
|
311
321
|
server.listen(PORT, () => {
|
|
312
|
-
const pidFilePath = path.join(__dirname, 'aicmd.server.pid');
|
|
313
322
|
fs.writeFileSync(pidFilePath, process.pid.toString());
|
|
314
323
|
console.log(`PID ${process.pid} written to ${pidFilePath}`);
|
|
315
324
|
console.log(`aicmd Server is running on port ${PORT}`);
|
|
@@ -317,14 +326,18 @@ server.listen(PORT, () => {
|
|
|
317
326
|
});
|
|
318
327
|
|
|
319
328
|
// 进程退出处理
|
|
320
|
-
process.on('exit', () => { logStream.end(); });
|
|
329
|
+
process.on('exit', () => { cleanupPid(); logStream.end(); });
|
|
330
|
+
process.on('SIGTERM', () => { cleanupPid(); process.exit(0); });
|
|
331
|
+
process.on('SIGINT', () => { cleanupPid(); process.exit(0); });
|
|
321
332
|
process.on('uncaughtException', (err) => {
|
|
322
333
|
console.error('Uncaught exception:', err);
|
|
334
|
+
cleanupPid();
|
|
323
335
|
logStream.end();
|
|
324
336
|
process.exit(1);
|
|
325
337
|
});
|
|
326
338
|
process.on('unhandledRejection', (err) => {
|
|
327
339
|
console.error('Unhandled rejection:', err);
|
|
340
|
+
cleanupPid();
|
|
328
341
|
logStream.end();
|
|
329
342
|
process.exit(1);
|
|
330
343
|
});
|