chore(gateway): use ws bind as lock
This commit is contained in:
@@ -1,48 +1,10 @@
|
||||
#!/usr/bin/env tsx
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { forceFreePort, type PortProcess } from "../src/cli/ports.js";
|
||||
|
||||
const DEFAULT_PORT = 18789;
|
||||
const DEFAULT_LOCK = path.join(os.tmpdir(), "clawdis-gateway.lock");
|
||||
|
||||
function killPid(pid: number, reason: string) {
|
||||
try {
|
||||
process.kill(pid, "SIGTERM");
|
||||
console.log(`sent SIGTERM to ${pid} (${reason})`);
|
||||
} catch (err) {
|
||||
if ((err as NodeJS.ErrnoException).code === "ESRCH") {
|
||||
console.log(`pid ${pid} (${reason}) not running`);
|
||||
} else {
|
||||
console.error(`failed to kill ${pid} (${reason}): ${String(err)}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function killLockHolder(lockPath: string) {
|
||||
if (!fs.existsSync(lockPath)) return;
|
||||
try {
|
||||
const contents = fs.readFileSync(lockPath, "utf8").trim();
|
||||
const pid = Number.parseInt(contents.split("\n")[0] ?? "", 10);
|
||||
if (Number.isFinite(pid)) {
|
||||
killPid(pid, "gateway lock holder");
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(`failed to read lock ${lockPath}: ${String(err)}`);
|
||||
}
|
||||
}
|
||||
|
||||
function cleanupLock(lockPath: string) {
|
||||
if (!fs.existsSync(lockPath)) return;
|
||||
try {
|
||||
fs.rmSync(lockPath, { force: true });
|
||||
console.log(`removed gateway lock: ${lockPath}`);
|
||||
} catch (err) {
|
||||
console.error(`failed to remove lock ${lockPath}: ${String(err)}`);
|
||||
}
|
||||
}
|
||||
|
||||
function killGatewayListeners(port: number): PortProcess[] {
|
||||
try {
|
||||
@@ -86,16 +48,13 @@ function main() {
|
||||
process.env.CLAWDIS_GATEWAY_PORT ?? `${DEFAULT_PORT}`,
|
||||
10,
|
||||
);
|
||||
const lockPath = process.env.CLAWDIS_GATEWAY_LOCK ?? DEFAULT_LOCK;
|
||||
|
||||
console.log(`🧹 test:force - clearing gateway on port ${port}`);
|
||||
killLockHolder(lockPath);
|
||||
const killed = killGatewayListeners(port);
|
||||
if (killed.length === 0) {
|
||||
console.log("no listeners to kill");
|
||||
}
|
||||
|
||||
cleanupLock(lockPath);
|
||||
console.log("running pnpm test…");
|
||||
runTests();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user