Migration: drop the /proc scan for servers from before the lock, and the fixture generator
The migration only matters for data from before libraries, and the README says to stop the server first; the lock check stays. make.ts rebuilt the committed fixtures from two old commits and will not be run again (the test's header names the commits). isDir was used by nothing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014yB6q4BHMwptTX3YFtkRab
This commit is contained in:
1 parent
d8654fdd19
commit
9c506cd660
5 files changed
+18
-326
No files matched your search
+2
-49
@@ -1,16 +1,9 @@
|
||||
/**
|
||||
* Whether a server has a data directory open. A server writes <data>/server.lock (its pid) while
|
||||
* it runs and removes it on the way out; a lock whose process is gone is stale and ignored.
|
||||
*
|
||||
* Servers from before the lock wrote none. For them (the migration must not run under one), on a
|
||||
* system with /proc, every process running `server/index.ts` is looked at: its checkout is found
|
||||
* from its command line and working directory, and it counts if that checkout's default data
|
||||
* directory (`<checkout>/.data`) is this one. Only command lines and working directories are read,
|
||||
* never a process's environment, so a server started with IE_DATA pointing elsewhere is still
|
||||
* reported if it runs from the checkout whose `.data` this is.
|
||||
*/
|
||||
import { existsSync, readFileSync, readdirSync, readlinkSync, realpathSync, rmSync } from "node:fs";
|
||||
import { dirname, join, resolve } from "node:path";
|
||||
import { existsSync, readFileSync, rmSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { writeAtomic } from "./util";
|
||||
|
||||
export const LOCK = "server.lock";
|
||||
@@ -30,14 +23,6 @@ const alive = (pid: number) => {
|
||||
}
|
||||
};
|
||||
|
||||
const real = (p: string) => {
|
||||
try {
|
||||
return realpathSync(p);
|
||||
} catch {
|
||||
return resolve(p);
|
||||
}
|
||||
};
|
||||
|
||||
/** The process holding the lock on this data directory, if it is still running (and is not this one). */
|
||||
export function lockHolder(data: string): Lock | null {
|
||||
const file = join(data, LOCK);
|
||||
@@ -62,38 +47,6 @@ function cmdline(pid: number): string[] | null {
|
||||
}
|
||||
}
|
||||
|
||||
/** Servers without a lock: processes running server/index.ts from the checkout whose `.data` this is. */
|
||||
export function unlockedServers(data: string): number[] {
|
||||
if (!existsSync("/proc")) return [];
|
||||
const want = real(data);
|
||||
const out: number[] = [];
|
||||
for (const name of readdirSync("/proc")) {
|
||||
if (!/^\d+$/.test(name)) continue;
|
||||
const pid = Number(name);
|
||||
if (pid === process.pid) continue;
|
||||
const args = cmdline(pid);
|
||||
const script = args?.find((a) => a.endsWith("server/index.ts"));
|
||||
if (!script) continue;
|
||||
try {
|
||||
const cwd = readlinkSync(`/proc/${pid}/cwd`);
|
||||
const checkout = dirname(dirname(resolve(cwd, script)));
|
||||
if (real(join(checkout, ".data")) === want) out.push(pid);
|
||||
} catch {
|
||||
/* gone, or not ours to look at */
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Why the data directory is not free, or null if no server has it open. */
|
||||
export function busy(data: string): string | null {
|
||||
const lock = lockHolder(data);
|
||||
if (lock) return `a server (pid ${lock.pid}${lock.port ? `, port ${lock.port}` : ""}) has ${data} open`;
|
||||
const pids = unlockedServers(data);
|
||||
if (pids.length) return `a server (pid ${pids.join(", ")}) is running from the checkout whose data directory is ${data}`;
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Take the lock for this process; it is let go on exit. */
|
||||
export function takeLock(data: string, port: number) {
|
||||
const file = join(data, LOCK);
|
||||
|
||||
Reference in new issue
Block a user