feat(cli): add stop command to shut down running daemon
This commit is contained in:
1 parent
2b8171cc37
commit
48411dccd2
8 files changed
+498
-19
No files matched your search
@@ -658,4 +658,63 @@ describe("pi2 main", () => {
|
||||
checks: [{ name: "node", status: "fail", detail: "v18.0.0" }],
|
||||
});
|
||||
});
|
||||
|
||||
it("prints stop usage for `stop --help` and `daemon stop --help`", async () => {
|
||||
const out1: string[] = [];
|
||||
const code1 = await main(["stop", "--help"], { out: (line) => out1.push(line) });
|
||||
expect(code1).toBe(0);
|
||||
expect(out1.join("\n")).toContain("pi2 stop [options]");
|
||||
expect(out1.join("\n")).toContain("-f, --force");
|
||||
|
||||
const out2: string[] = [];
|
||||
const code2 = await main(["daemon", "stop", "--help"], { out: (line) => out2.push(line) });
|
||||
expect(code2).toBe(0);
|
||||
expect(out2.join("\n")).toContain("pi2 stop [options]");
|
||||
});
|
||||
|
||||
it("routes `stop` and `daemon stop` to stopRunner with parsed flags", async () => {
|
||||
const calls: unknown[] = [];
|
||||
const code1 = await main(["stop", "--force", "-t", "10"], {
|
||||
stopRunner: async (opts) => {
|
||||
calls.push(opts);
|
||||
return 0;
|
||||
},
|
||||
});
|
||||
expect(code1).toBe(0);
|
||||
expect(calls[0]).toMatchObject({ force: true, timeoutSec: 10 });
|
||||
|
||||
calls.length = 0;
|
||||
const code2 = await main(["daemon", "stop", "-f"], {
|
||||
stopRunner: async (opts) => {
|
||||
calls.push(opts);
|
||||
return 0;
|
||||
},
|
||||
});
|
||||
expect(code2).toBe(0);
|
||||
expect(calls[0]).toMatchObject({ force: true });
|
||||
});
|
||||
|
||||
it("rejects invalid arguments for `stop` with exit 2", async () => {
|
||||
const err: string[] = [];
|
||||
const code1 = await main(["stop", "extra"], { err: (line) => err.push(line) });
|
||||
expect(code1).toBe(2);
|
||||
expect(err[0]).toContain('unexpected argument "extra"');
|
||||
|
||||
err.length = 0;
|
||||
const code2 = await main(["stop", "--timeout", "nope"], { err: (line) => err.push(line) });
|
||||
expect(code2).toBe(2);
|
||||
expect(err[0]).toContain("--timeout must be a positive integer");
|
||||
});
|
||||
|
||||
it("reports missing daemon on `stop` with exit 1", async () => {
|
||||
const err: string[] = [];
|
||||
const code = await main(["stop"], {
|
||||
err: (line) => err.push(line),
|
||||
stopRunner: async () => {
|
||||
throw new CliError("no running pi2 daemon");
|
||||
},
|
||||
});
|
||||
expect(code).toBe(1);
|
||||
expect(err[0]).toBe("pi2: no running pi2 daemon");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,192 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { runStop, type StopOptions } from "../../src/cli/stop.js";
|
||||
import { CliError } from "../../src/cli/util.js";
|
||||
import type { DaemonLock } from "../../src/cli/lock.js";
|
||||
|
||||
function mockLock(overrides: Partial<DaemonLock> = {}): DaemonLock {
|
||||
return {
|
||||
pid: 12345,
|
||||
bootId: "boot-1",
|
||||
port: 7676,
|
||||
daemonId: "d-test-1",
|
||||
startedAt: 1700000000000,
|
||||
protocolVersion: 1,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
describe("runStop", () => {
|
||||
it("throws CliError when neither daemon.json nor daemon.lock exists", async () => {
|
||||
await expect(
|
||||
runStop({
|
||||
readDaemonJson: () => undefined,
|
||||
readLockFile: () => undefined,
|
||||
}),
|
||||
).rejects.toThrow("no running pi2 daemon");
|
||||
});
|
||||
|
||||
it("cleans up stale lock and throws CliError when PID is dead", async () => {
|
||||
const unlinked: string[] = [];
|
||||
const lock = mockLock({ pid: 99999 });
|
||||
|
||||
await expect(
|
||||
runStop({
|
||||
readDaemonJson: () => lock,
|
||||
isPidAlive: () => false,
|
||||
bootId: () => "boot-1",
|
||||
unlink: (p) => unlinked.push(p),
|
||||
}),
|
||||
).rejects.toThrow("no running pi2 daemon (cleaned up stale lock for pid 99999)");
|
||||
|
||||
expect(unlinked.length).toBe(2);
|
||||
});
|
||||
|
||||
it("cleans up stale lock and throws CliError when bootId has changed", async () => {
|
||||
const unlinked: string[] = [];
|
||||
const lock = mockLock({ pid: 12345, bootId: "boot-old" });
|
||||
|
||||
await expect(
|
||||
runStop({
|
||||
readDaemonJson: () => lock,
|
||||
isPidAlive: () => true,
|
||||
bootId: () => "boot-new",
|
||||
unlink: (p) => unlinked.push(p),
|
||||
}),
|
||||
).rejects.toThrow("no running pi2 daemon (cleaned up stale lock for pid 12345)");
|
||||
|
||||
expect(unlinked.length).toBe(2);
|
||||
});
|
||||
|
||||
it("sends SIGTERM, waits for daemon to exit, and returns 0", async () => {
|
||||
const signals: { pid: number; signal: NodeJS.Signals }[] = [];
|
||||
const lines: string[] = [];
|
||||
const unlinked: string[] = [];
|
||||
let alive = true;
|
||||
|
||||
const code = await runStop({
|
||||
readDaemonJson: () => mockLock({ pid: 12345 }),
|
||||
isPidAlive: () => alive,
|
||||
bootId: () => "boot-1",
|
||||
kill: (pid, sig) => {
|
||||
signals.push({ pid, signal: sig });
|
||||
alive = false;
|
||||
},
|
||||
print: (line) => lines.push(line),
|
||||
unlink: (p) => unlinked.push(p),
|
||||
sleep: async () => {},
|
||||
});
|
||||
|
||||
expect(code).toBe(0);
|
||||
expect(signals).toEqual([{ pid: 12345, signal: "SIGTERM" }]);
|
||||
expect(lines).toEqual(["pi2 daemon (pid 12345) stopped"]);
|
||||
expect(unlinked.length).toBe(2);
|
||||
});
|
||||
|
||||
it("handles ESRCH on SIGTERM cleanly as stopped", async () => {
|
||||
const lines: string[] = [];
|
||||
const unlinked: string[] = [];
|
||||
|
||||
const esrch = new Error("kill ESRCH") as NodeJS.ErrnoException;
|
||||
esrch.code = "ESRCH";
|
||||
|
||||
const code = await runStop({
|
||||
readDaemonJson: () => mockLock({ pid: 12345 }),
|
||||
isPidAlive: () => true,
|
||||
bootId: () => "boot-1",
|
||||
kill: () => {
|
||||
throw esrch;
|
||||
},
|
||||
print: (line) => lines.push(line),
|
||||
unlink: (p) => unlinked.push(p),
|
||||
});
|
||||
|
||||
expect(code).toBe(0);
|
||||
expect(lines).toEqual(["pi2 daemon (pid 12345) stopped"]);
|
||||
expect(unlinked.length).toBe(2);
|
||||
});
|
||||
|
||||
it("throws CliError when SIGTERM fails with EPERM", async () => {
|
||||
const eperm = new Error("kill EPERM") as NodeJS.ErrnoException;
|
||||
eperm.code = "EPERM";
|
||||
|
||||
await expect(
|
||||
runStop({
|
||||
readDaemonJson: () => mockLock({ pid: 12345 }),
|
||||
isPidAlive: () => true,
|
||||
bootId: () => "boot-1",
|
||||
kill: () => {
|
||||
throw eperm;
|
||||
},
|
||||
}),
|
||||
).rejects.toThrow("failed to stop pi2 daemon (pid 12345): permission denied");
|
||||
});
|
||||
|
||||
it("throws CliError when daemon does not exit within timeout without force", async () => {
|
||||
let fakeTime = 1000;
|
||||
await expect(
|
||||
runStop({
|
||||
readDaemonJson: () => mockLock({ pid: 12345 }),
|
||||
isPidAlive: () => true,
|
||||
bootId: () => "boot-1",
|
||||
kill: () => {},
|
||||
timeoutSec: 1,
|
||||
now: () => fakeTime,
|
||||
sleep: async (ms) => {
|
||||
fakeTime += ms;
|
||||
},
|
||||
}),
|
||||
).rejects.toThrow("pi2 daemon (pid 12345) did not stop within 1s; use --force to kill");
|
||||
});
|
||||
|
||||
it("escalates to SIGKILL with --force when SIGTERM times out", async () => {
|
||||
const signals: { pid: number; signal: NodeJS.Signals }[] = [];
|
||||
const lines: string[] = [];
|
||||
const unlinked: string[] = [];
|
||||
let fakeTime = 1000;
|
||||
let killedWithSigkill = false;
|
||||
|
||||
const code = await runStop({
|
||||
readDaemonJson: () => mockLock({ pid: 12345 }),
|
||||
isPidAlive: () => !killedWithSigkill,
|
||||
bootId: () => "boot-1",
|
||||
kill: (pid, sig) => {
|
||||
signals.push({ pid, signal: sig });
|
||||
if (sig === "SIGKILL") killedWithSigkill = true;
|
||||
},
|
||||
force: true,
|
||||
timeoutSec: 1,
|
||||
now: () => fakeTime,
|
||||
sleep: async (ms) => {
|
||||
fakeTime += ms;
|
||||
},
|
||||
print: (line) => lines.push(line),
|
||||
unlink: (p) => unlinked.push(p),
|
||||
});
|
||||
|
||||
expect(code).toBe(0);
|
||||
expect(signals).toEqual([
|
||||
{ pid: 12345, signal: "SIGTERM" },
|
||||
{ pid: 12345, signal: "SIGKILL" },
|
||||
]);
|
||||
expect(lines).toEqual(["pi2 daemon (pid 12345) killed with SIGKILL"]);
|
||||
expect(unlinked.length).toBe(2);
|
||||
});
|
||||
|
||||
it("throws CliError if daemon still alive after SIGKILL", async () => {
|
||||
let fakeTime = 1000;
|
||||
await expect(
|
||||
runStop({
|
||||
readDaemonJson: () => mockLock({ pid: 12345 }),
|
||||
isPidAlive: () => true,
|
||||
bootId: () => "boot-1",
|
||||
kill: () => {},
|
||||
force: true,
|
||||
timeoutSec: 1,
|
||||
now: () => fakeTime,
|
||||
sleep: async (ms) => {
|
||||
fakeTime += ms;
|
||||
},
|
||||
}),
|
||||
).rejects.toThrow("pi2 daemon (pid 12345) could not be killed with SIGKILL");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user