Skip to content

Commit af2456f

Browse files
committed
refactor(onboard): pass heartbeat activity as data
1 parent 34d4703 commit af2456f

2 files changed

Lines changed: 18 additions & 21 deletions

File tree

src/lib/sandbox-base-image/local-build-heartbeat.test.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,8 @@ describe("local sandbox base-image build heartbeat", () => {
3838
Record<string, unknown>,
3939
];
4040
expect(executable).toBe("/node");
41-
expect(args.slice(0, 2)).toEqual([
42-
"-e",
43-
expect.stringContaining("Still working on sandbox base image build"),
44-
]);
45-
expect(args.slice(2)).toEqual(["25000", "42"]);
41+
expect(args.slice(0, 2)).toEqual(["-e", expect.any(String)]);
42+
expect(args.slice(2)).toEqual(["25000", "42", "build"]);
4643
expect(options).toEqual({ env: {}, stdio: ["ignore", "inherit", "inherit"] });
4744
expect(fixture.child.unref).toHaveBeenCalledOnce();
4845
expect(fixture.child.kill).toHaveBeenCalledWith("SIGTERM");

src/lib/sandbox-base-image/local-build-heartbeat.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,20 @@ const DEFAULT_HEARTBEAT_INTERVAL_MS = 30_000;
77

88
type HeartbeatActivity = "build" | "pull";
99

10-
function heartbeatChildScript(activity: HeartbeatActivity): string {
11-
return [
12-
"const intervalMs = Number(process.argv[1]);",
13-
"const parentPid = Number(process.argv[2]);",
14-
"const startedAt = Date.now();",
15-
"const stop = () => process.exit(0);",
16-
'process.on("SIGINT", stop);',
17-
'process.on("SIGTERM", stop);',
18-
"setInterval(() => {",
19-
" try { process.kill(parentPid, 0); } catch { stop(); return; }",
20-
" const elapsedSeconds = Math.max(0, Math.round((Date.now() - startedAt) / 1000));",
21-
` process.stdout.write(\` ⏳ Still working on sandbox base image ${activity}… (\${elapsedSeconds}s elapsed)\\n\`);`,
22-
"}, intervalMs);",
23-
].join("\n");
24-
}
10+
const HEARTBEAT_CHILD_SCRIPT = [
11+
"const intervalMs = Number(process.argv[1]);",
12+
"const parentPid = Number(process.argv[2]);",
13+
"const activity = process.argv[3];",
14+
"const startedAt = Date.now();",
15+
"const stop = () => process.exit(0);",
16+
'process.on("SIGINT", stop);',
17+
'process.on("SIGTERM", stop);',
18+
"setInterval(() => {",
19+
" try { process.kill(parentPid, 0); } catch { stop(); return; }",
20+
" const elapsedSeconds = Math.max(0, Math.round((Date.now() - startedAt) / 1000));",
21+
" process.stdout.write(` ⏳ Still working on sandbox base image ${activity}… (${elapsedSeconds}s elapsed)\\n`);",
22+
"}, intervalMs);",
23+
].join("\n");
2524

2625
type SpawnHeartbeat = typeof spawn;
2726

@@ -48,9 +47,10 @@ export function withLocalBuildHeartbeat<T>(
4847
options.nodeExecutable ?? process.execPath,
4948
[
5049
"-e",
51-
heartbeatChildScript(options.activity ?? "build"),
50+
HEARTBEAT_CHILD_SCRIPT,
5251
String(intervalMs),
5352
String(options.parentPid ?? process.pid),
53+
options.activity ?? "build",
5454
],
5555
{ env: {}, stdio: ["ignore", "inherit", "inherit"] },
5656
);

0 commit comments

Comments
 (0)