tt-help-cli-ycl 1.4.64 → 1.4.66
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/src/watchdog/process-manager.js +74 -14
package/package.json
CHANGED
|
@@ -15,6 +15,48 @@ import { EventEmitter } from "events";
|
|
|
15
15
|
const MAX_RESTART = 3;
|
|
16
16
|
const IS_WIN = process.platform === "win32";
|
|
17
17
|
|
|
18
|
+
// 引号感知的命令拆分:"a b" "c d" → ["a b", "c d"]
|
|
19
|
+
function splitCommand(cmdLine) {
|
|
20
|
+
const parts = [];
|
|
21
|
+
let cur = "",
|
|
22
|
+
inDq = false,
|
|
23
|
+
inSq = false;
|
|
24
|
+
for (let i = 0; i < cmdLine.length; i++) {
|
|
25
|
+
const ch = cmdLine[i];
|
|
26
|
+
if (ch === '"' && !inSq) {
|
|
27
|
+
inDq = !inDq;
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
if (ch === "'" && !inDq) {
|
|
31
|
+
inSq = !inSq;
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
if (ch === " " && !inDq && !inSq) {
|
|
35
|
+
if (cur) {
|
|
36
|
+
parts.push(cur);
|
|
37
|
+
cur = "";
|
|
38
|
+
}
|
|
39
|
+
} else {
|
|
40
|
+
cur += ch;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (cur) parts.push(cur);
|
|
44
|
+
return parts;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Windows: 从 cmd.exe 往下找真正的子进程 PID(跳过 conhost)
|
|
48
|
+
function getChildPid(parentPid) {
|
|
49
|
+
try {
|
|
50
|
+
const out = execSync(
|
|
51
|
+
`powershell -NoProfile -Command "Get-CimInstance Win32_Process -Filter 'ParentProcessId=${parentPid}' | Where-Object { $_.Name -ne 'conhost.exe' } | Select-Object -First 1 -ExpandProperty ProcessId"`,
|
|
52
|
+
{ encoding: "utf-8", timeout: 3000, stdio: ["ignore", "pipe", "ignore"] },
|
|
53
|
+
).trim();
|
|
54
|
+
return parseInt(out, 10) || 0;
|
|
55
|
+
} catch {
|
|
56
|
+
return 0;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
18
60
|
function getProcessStats(pid) {
|
|
19
61
|
try {
|
|
20
62
|
const cmd = IS_WIN
|
|
@@ -77,18 +119,35 @@ export class ProcessManager extends EventEmitter {
|
|
|
77
119
|
let child;
|
|
78
120
|
const startTime = Date.now();
|
|
79
121
|
try {
|
|
80
|
-
//
|
|
81
|
-
if (args.length
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
122
|
+
// 拆分为 program + args;Windows .cmd 需 shell,用完整命令行
|
|
123
|
+
if (args.length === 0) {
|
|
124
|
+
const parts = splitCommand(command);
|
|
125
|
+
command = parts[0] || command;
|
|
126
|
+
args = parts.slice(1);
|
|
127
|
+
}
|
|
128
|
+
const fullCmd = [command, ...args].join(" ");
|
|
129
|
+
child = IS_WIN
|
|
130
|
+
? spawn(fullCmd, {
|
|
131
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
132
|
+
detached: false,
|
|
133
|
+
shell: true,
|
|
134
|
+
})
|
|
135
|
+
: spawn(command, args, {
|
|
136
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
137
|
+
detached: false,
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
// Windows: cmd.exe 不是真实进程,延迟获取子进程 PID
|
|
141
|
+
if (IS_WIN) {
|
|
142
|
+
setTimeout(() => {
|
|
143
|
+
const realPid = getChildPid(child.pid);
|
|
144
|
+
if (realPid > 0) {
|
|
145
|
+
const info = this.processes.get(id);
|
|
146
|
+
if (info && info.child === child) {
|
|
147
|
+
info._realPid = realPid;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}, 2000);
|
|
92
151
|
}
|
|
93
152
|
} catch (e) {
|
|
94
153
|
console.error(`[watchdog] 启动命令 ${id} 失败: ${e.message}`);
|
|
@@ -203,7 +262,8 @@ export class ProcessManager extends EventEmitter {
|
|
|
203
262
|
const result = [];
|
|
204
263
|
for (const [cmdId, info] of this.processes) {
|
|
205
264
|
if (info.stopped) continue;
|
|
206
|
-
|
|
265
|
+
// Windows: 优先用真实子进程 PID(否则拿到的是 cmd.exe 的内存)
|
|
266
|
+
const pid = info._realPid || info.child?.pid || 0;
|
|
207
267
|
const stats = pid ? getProcessStats(pid) : null;
|
|
208
268
|
const uptimeSeconds = Math.round((Date.now() - info.startTime) / 1000);
|
|
209
269
|
|
|
@@ -310,7 +370,7 @@ export class ProcessManager extends EventEmitter {
|
|
|
310
370
|
if (info.stopped || !info.child) continue;
|
|
311
371
|
const thresholds = info.thresholds || {};
|
|
312
372
|
const uptimeSeconds = Math.round((now - info.startTime) / 1000);
|
|
313
|
-
const pid = info.child.pid;
|
|
373
|
+
const pid = info._realPid || info.child.pid;
|
|
314
374
|
|
|
315
375
|
let reason = "";
|
|
316
376
|
// 检查运行时长
|