mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 16:08:56 +00:00
* fix(cli): every settings file follows HYPERFRAMES_CONFIG_DIR * fix(cli): the media-use skill copy reads its config from HYPERFRAMES_CONFIG_DIR too * test(cli): expect the config folder as a normalized path on every platform * chore(skills): refresh the media-use manifest hash * fix(cli): a moved settings folder starts from ~/.hyperframes, keeping an opt-out * fix(cli): drop the updater's unused path import * style(cli): keep the settings-seed comment under 120 characters
80 lines
2.4 KiB
TypeScript
80 lines
2.4 KiB
TypeScript
import { mkdirSync, readdirSync, statSync, unlinkSync, writeFileSync } from "node:fs";
|
|
import { join } from "node:path";
|
|
import { hyperframesConfigDir } from "./configDir.js";
|
|
|
|
/** One empty file per live CLI process, named by pid; the background installer waits until none is alive. */
|
|
export const runningDir = () => hyperframesConfigDir("running");
|
|
const RUNNING_HEARTBEAT_MS = 30_000;
|
|
export const RUNNING_STALE_MS = 10 * 60_000;
|
|
|
|
function isAlive(pid: number): boolean {
|
|
try {
|
|
process.kill(pid, 0);
|
|
return true;
|
|
} catch (error) {
|
|
// EPERM: the pid exists but belongs to another user.
|
|
return error instanceof Error && "code" in error && error.code === "EPERM";
|
|
}
|
|
}
|
|
|
|
function pruneExitedClis(dir: string, own: string): void {
|
|
let names: string[] = [];
|
|
try {
|
|
names = readdirSync(dir);
|
|
} catch {
|
|
return;
|
|
}
|
|
for (const name of names) {
|
|
if (name === own) continue;
|
|
const file = join(dir, name);
|
|
try {
|
|
const pid = Number(name);
|
|
const beating = Date.now() - statSync(file).mtimeMs <= RUNNING_STALE_MS;
|
|
// A live pid whose file stopped beating is a reused pid, not a CLI.
|
|
if (Number.isInteger(pid) && pid > 0 && isAlive(pid) && beating) continue;
|
|
unlinkSync(file);
|
|
} catch {
|
|
/* another CLI removed it first */
|
|
}
|
|
}
|
|
}
|
|
|
|
let registered = false;
|
|
|
|
/** Whether this process is on record as running, so an installer would wait for it to exit. */
|
|
export const registeredAsRunning = (): boolean => registered;
|
|
|
|
/** Mark this process as running until it exits. Never throws: false when the record cannot be written. */
|
|
export function registerRunningCli(
|
|
dir: string = runningDir(),
|
|
heartbeatMs: number = RUNNING_HEARTBEAT_MS,
|
|
): boolean {
|
|
try {
|
|
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
const file = join(dir, String(process.pid));
|
|
writeFileSync(file, "", { mode: 0o600 });
|
|
pruneExitedClis(dir, String(process.pid));
|
|
const heartbeat = setInterval(() => {
|
|
try {
|
|
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
writeFileSync(file, String(Date.now()), { mode: 0o600 });
|
|
} catch {
|
|
/* best-effort */
|
|
}
|
|
}, heartbeatMs);
|
|
heartbeat.unref();
|
|
process.on("exit", () => {
|
|
clearInterval(heartbeat);
|
|
try {
|
|
unlinkSync(file);
|
|
} catch {
|
|
/* already gone */
|
|
}
|
|
});
|
|
registered = true;
|
|
} catch {
|
|
registered = false;
|
|
}
|
|
return registered;
|
|
}
|