fix(dev): clean up web backend lifecycle

This commit is contained in:
digua committed 2026-06-05 11:12:42 +08:00
1 parent 296573ed5c
commit 953471a514
6 files changed
+124 -106

No files matched your search

+30
View File
@@ -1,3 +1,5 @@
import { spawnSync } from 'node:child_process'
export function createChatlabStartCommand({ serverDir, backendPort, nodeExecutable = process.execPath }) {
return {
command: nodeExecutable,
@@ -16,6 +18,34 @@ export function createChatlabStartCommand({ serverDir, backendPort, nodeExecutab
cwd: serverDir,
stdio: ['ignore', 'pipe', 'pipe'],
env: { ...process.env },
detached: true,
},
}
}
export function terminateChatlabStartProcess(
childProcess,
{ platform = process.platform, killProcess = process.kill, spawnSyncFn = spawnSync } = {}
) {
if (!childProcess) return
if (childProcess.exitCode !== null || childProcess.signalCode !== null) return
const pid = childProcess.pid
if (!pid) {
childProcess.kill('SIGTERM')
return
}
if (platform === 'win32') {
const result = spawnSyncFn('taskkill', ['/PID', String(pid), '/T', '/F'], { stdio: 'ignore' })
if (result.status === 0) return
childProcess.kill('SIGTERM')
return
}
try {
killProcess(-pid, 'SIGTERM')
} catch {
childProcess.kill('SIGTERM')
}
}