(deps: KillCommandDeps)
| 59 | } |
| 60 | |
| 61 | export async function handleKillCommand(deps: KillCommandDeps): Promise<void> { |
| 62 | const lock = deps.getLiveLock(); |
| 63 | if (!lock) { |
| 64 | deps.stdout.write('No running Kimi server.\n'); |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | const { pid } = lock; |
| 69 | const origin = serverOrigin(lockConnectHost(lock), lock.port); |
| 70 | |
| 71 | // 1. API path — best-effort graceful shutdown. Ignore every outcome: the |
| 72 | // server may be an older build without the route, already wedged, or may |
| 73 | // drop the connection as it exits. The bearer token (M5.1) is best-effort |
| 74 | // too: if it can't be read the API call 401s and the PID path below still |
| 75 | // guarantees the kill. |
| 76 | const token = deps.resolveToken(); |
| 77 | await deps.requestShutdown(origin, token).catch(() => {}); |
| 78 | |
| 79 | // 2. PID path — SIGTERM, wait, then SIGKILL. |
| 80 | deps.signalPid(pid, 'SIGTERM'); |
| 81 | |
| 82 | if (await waitForExit(pid, TERM_GRACE_MS, deps)) { |
| 83 | deps.stdout.write(`Kimi server (pid ${String(pid)}) stopped.\n`); |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | deps.signalPid(pid, 'SIGKILL'); |
| 88 | |
| 89 | if (await waitForExit(pid, KILL_GRACE_MS, deps)) { |
| 90 | deps.stdout.write(`Kimi server (pid ${String(pid)}) killed.\n`); |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | throw new Error( |
| 95 | `Failed to stop Kimi server (pid ${String(pid)}); insufficient permissions?`, |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | async function waitForExit( |
| 100 | pid: number, |
no test coverage detected