mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +00:00
* fix(cli): an update never replaces the CLI's files while a hyperframes command is running * fix(cli): a killed command's leftover pid file cannot hold updates back * fix(cli): judge a stale pid file by the installer's own clock, so sleep cannot drop a live command * fix(cli): after a sleep the installer watches every command's file afresh * docs(cli): shorten the installer script comment * docs(cli): keep the new files within their comment share * refactor(cli): split the upgrade eligibility check out of the installer scheduler * docs(cli): keep the scheduler's doc on the scheduler
153 lines
4.8 KiB
TypeScript
153 lines
4.8 KiB
TypeScript
import { spawn, type ChildProcess } from "node:child_process";
|
|
import {
|
|
existsSync,
|
|
mkdirSync,
|
|
mkdtempSync,
|
|
readFileSync,
|
|
rmSync,
|
|
utimesSync,
|
|
writeFileSync,
|
|
} from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import { afterEach, expect, it } from "vitest";
|
|
import { installerScript } from "./autoUpdate.js";
|
|
|
|
// Runs the real detached-installer script; the "install" only writes a marker file.
|
|
const dirs: string[] = [];
|
|
const children: ChildProcess[] = [];
|
|
|
|
afterEach(() => {
|
|
for (const child of children.splice(0)) child.kill();
|
|
for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true });
|
|
});
|
|
|
|
function setup(staleMs = 60_000) {
|
|
const dir = mkdtempSync(join(tmpdir(), "hf-installer-"));
|
|
dirs.push(dir);
|
|
const runningDir = join(dir, "running");
|
|
mkdirSync(runningDir);
|
|
const marker = join(dir, "installed");
|
|
const script = installerScript({
|
|
configFile: join(dir, "config.json"),
|
|
version: "9.9.9",
|
|
bin: process.execPath,
|
|
args: ["-e", `require("node:fs").writeFileSync(${JSON.stringify(marker)}, "")`],
|
|
runningDir,
|
|
pollMs: 50,
|
|
maxWaitMs: 60_000,
|
|
staleMs,
|
|
});
|
|
return { dir, runningDir, marker, script };
|
|
}
|
|
|
|
function start(args: string[]): ChildProcess {
|
|
const child = spawn(process.execPath, args, { stdio: "ignore" });
|
|
children.push(child);
|
|
return child;
|
|
}
|
|
|
|
const exited = (child: ChildProcess) =>
|
|
child.exitCode !== null || child.signalCode !== null
|
|
? Promise.resolve()
|
|
: new Promise<void>((resolve) => child.once("exit", () => resolve()));
|
|
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
|
|
async function until(check: () => boolean): Promise<void> {
|
|
while (!check()) await sleep(25);
|
|
}
|
|
|
|
function backdate(file: string): void {
|
|
const tenMinutesAgo = new Date(Date.now() - 10 * 60_000);
|
|
utimesSync(file, tenMinutesAgo, tenMinutesAgo);
|
|
}
|
|
|
|
async function expectInstalledAndRemoved(script: string, marker: string, file: string) {
|
|
await exited(start(["-e", script]));
|
|
expect(existsSync(marker)).toBe(true);
|
|
expect(existsSync(file)).toBe(false);
|
|
}
|
|
|
|
function registerCli(runningDir: string): ChildProcess {
|
|
const cli = start(["-e", "setInterval(() => {}, 1000)"]);
|
|
writeFileSync(join(runningDir, String(cli.pid)), "");
|
|
return cli;
|
|
}
|
|
|
|
it("never replaces package files while a CLI process is running, then installs once it exits", async () => {
|
|
const { dir, runningDir, marker, script } = setup();
|
|
const cli = registerCli(runningDir);
|
|
const installer = start(["-e", script]);
|
|
|
|
await sleep(1000);
|
|
expect(existsSync(marker)).toBe(false);
|
|
|
|
cli.kill();
|
|
await exited(installer);
|
|
expect(existsSync(marker)).toBe(true);
|
|
expect(JSON.parse(readFileSync(join(dir, "config.json"), "utf-8")).completedUpdate).toMatchObject(
|
|
{ version: "9.9.9", ok: true },
|
|
);
|
|
});
|
|
|
|
it("does not wait on a CLI process that died without cleaning up", async () => {
|
|
const { runningDir, marker, script } = setup();
|
|
const dead = start(["-e", ""]);
|
|
await exited(dead);
|
|
const staleFile = join(runningDir, String(dead.pid));
|
|
writeFileSync(staleFile, "");
|
|
|
|
await expectInstalledAndRemoved(script, marker, staleFile);
|
|
});
|
|
|
|
it("keeps waiting on a live command whose file looks old after the machine slept", async () => {
|
|
const { runningDir, marker, script } = setup();
|
|
const cli = registerCli(runningDir);
|
|
const file = join(runningDir, String(cli.pid));
|
|
backdate(file);
|
|
const installer = start(["-e", script]);
|
|
|
|
await sleep(1000);
|
|
expect(existsSync(marker)).toBe(false);
|
|
expect(existsSync(file)).toBe(true);
|
|
|
|
cli.kill();
|
|
await exited(installer);
|
|
expect(existsSync(marker)).toBe(true);
|
|
});
|
|
|
|
it("drops a pid file whose pid belongs to another process once it stops changing", async () => {
|
|
const { runningDir, marker, script } = setup(300);
|
|
const other = start(["-e", "setInterval(() => {}, 1000)"]);
|
|
const staleFile = join(runningDir, String(other.pid));
|
|
writeFileSync(staleFile, "");
|
|
backdate(staleFile);
|
|
|
|
await expectInstalledAndRemoved(script, marker, staleFile);
|
|
});
|
|
|
|
it("lets only one installer wait: a later launch exits and installs nothing", async () => {
|
|
const { dir, runningDir, marker, script } = setup();
|
|
const cli = registerCli(runningDir);
|
|
const first = start(["-e", script]);
|
|
await until(() => existsSync(join(dir, "config.json.install-lock")));
|
|
|
|
await exited(start(["-e", script]));
|
|
expect(existsSync(marker)).toBe(false);
|
|
|
|
cli.kill();
|
|
await exited(first);
|
|
expect(existsSync(marker)).toBe(true);
|
|
});
|
|
|
|
it("takes over an install lock whose pid belongs to another process once it stops changing", async () => {
|
|
const { dir, marker, script } = setup(300);
|
|
const other = start(["-e", "setInterval(() => {}, 1000)"]);
|
|
const lock = join(dir, "config.json.install-lock");
|
|
writeFileSync(lock, String(other.pid));
|
|
backdate(lock);
|
|
|
|
await exited(start(["-e", script]));
|
|
expect(existsSync(marker)).toBe(true);
|
|
});
|