feat(server): run the background service on macOS via launchd (#6286)
Co-authored-by: Claude Fable 5 <[email protected]>
This commit is contained in:
1 parent
7441b36926
commit
2aa5f095fc
11 files changed
+531
-78
No files matched your search
@@ -4,8 +4,10 @@ import {
|
||||
HostProcessArguments,
|
||||
HostProcessExecutablePath,
|
||||
HostProcessPlatform,
|
||||
HostProcessUserId,
|
||||
} from "@t3tools/shared/hostProcess";
|
||||
import * as ConfigProvider from "effect/ConfigProvider";
|
||||
import * as Duration from "effect/Duration";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as FileSystem from "effect/FileSystem";
|
||||
import * as Layer from "effect/Layer";
|
||||
@@ -47,6 +49,51 @@ it("survives the kernel OOM-killing a greedy agent child", () => {
|
||||
expect(unit).toContain("OOMPolicy=continue");
|
||||
});
|
||||
|
||||
const macPlan = {
|
||||
nodePath: "/opt/homebrew/bin/node",
|
||||
launcherPath: "/Users/theo/.t3/runtime/service-launcher.mjs",
|
||||
baseDir: "/Users/theo/.t3",
|
||||
logPath: "/Users/theo/.t3/userdata/logs/boot-service.log",
|
||||
unitPath: "/Users/theo/Library/LaunchAgents/com.t3tools.t3code.service.plist",
|
||||
};
|
||||
|
||||
it("keeps launchd pinned to the stable launcher rather than a versioned server", () => {
|
||||
const plist = BootService.renderBootServicePlist(macPlan, { homeDir: "/Users/theo" });
|
||||
|
||||
expect(plist).toContain("<string>/opt/homebrew/bin/node</string>");
|
||||
expect(plist).toContain("<string>/Users/theo/.t3/runtime/service-launcher.mjs</string>");
|
||||
expect(plist).not.toContain("versions/1.2.3");
|
||||
});
|
||||
|
||||
it("restarts the launch agent on the systemd cadence", () => {
|
||||
const plist = BootService.renderBootServicePlist(macPlan, { homeDir: "/Users/theo" });
|
||||
|
||||
expect(plist).toContain("<key>RunAtLoad</key>\n <true/>");
|
||||
expect(plist).toContain("<key>KeepAlive</key>\n <true/>");
|
||||
expect(plist).toContain("<key>ThrottleInterval</key>\n <integer>5</integer>");
|
||||
expect(plist).toContain("<key>ExitTimeOut</key>\n <integer>90</integer>");
|
||||
});
|
||||
|
||||
it("appends both stdio streams to the boot service log", () => {
|
||||
const plist = BootService.renderBootServicePlist(macPlan, { homeDir: "/Users/theo" });
|
||||
|
||||
expect(plist).toContain(
|
||||
"<key>StandardOutPath</key>\n <string>/Users/theo/.t3/userdata/logs/boot-service.log</string>",
|
||||
);
|
||||
expect(plist).toContain(
|
||||
"<key>StandardErrorPath</key>\n <string>/Users/theo/.t3/userdata/logs/boot-service.log</string>",
|
||||
);
|
||||
});
|
||||
|
||||
it("escapes XML in host paths", () => {
|
||||
const plist = BootService.renderBootServicePlist(
|
||||
{ ...macPlan, baseDir: "/Users/theo/T3 & <Co>" },
|
||||
{ homeDir: "/Users/theo" },
|
||||
);
|
||||
|
||||
expect(plist).toContain("<string>/Users/theo/T3 & <Co></string>");
|
||||
});
|
||||
|
||||
const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
|
||||
platform: NodeJS.Platform = "linux",
|
||||
usePinnedLauncher = false,
|
||||
@@ -68,12 +115,14 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
|
||||
yield* fs.writeFileString(runtime.sentinelPath, "1.2.3\n");
|
||||
|
||||
const commands: string[] = [];
|
||||
const timeouts = new Map<string, unknown>();
|
||||
const control: { failCommand: string | undefined } = { failCommand: undefined };
|
||||
const runner = ProcessRunner.ProcessRunner.of({
|
||||
run: (input) =>
|
||||
Effect.sync(() => {
|
||||
const command = `${input.command} ${input.args.join(" ")}`;
|
||||
commands.push(command);
|
||||
timeouts.set(command, input.timeout);
|
||||
return {
|
||||
stdout: input.args[1] === "--version" ? "t3 v1.2.3\n" : "",
|
||||
stderr: "",
|
||||
@@ -99,19 +148,20 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
|
||||
Effect.provide(
|
||||
Layer.mergeAll(
|
||||
Layer.succeed(HostProcessPlatform, platform),
|
||||
Layer.succeed(HostProcessUserId, 501),
|
||||
Layer.succeed(HostProcessExecutablePath, "/usr/bin/node"),
|
||||
Layer.succeed(HostProcessArguments, ["/usr/bin/node", path.join(home, "bin.mjs")]),
|
||||
ConfigProvider.layer(ConfigProvider.fromEnv({ env: { HOME: home } })),
|
||||
),
|
||||
),
|
||||
);
|
||||
return { service, fs, statePath, commands, control };
|
||||
return { service, fs, statePath, commands, timeouts, control };
|
||||
});
|
||||
|
||||
it.layer(NodeServices.layer)("boot service install", (it) => {
|
||||
it.effect("installs, reports current state, and uninstalls", () =>
|
||||
Effect.gen(function* () {
|
||||
const { service, fs, statePath, commands } = yield* makeHarness();
|
||||
const { service, fs, statePath, commands, timeouts } = yield* makeHarness();
|
||||
const plan = yield* service.install;
|
||||
|
||||
expect(parseServiceState(yield* fs.readFileString(statePath))).toEqual({
|
||||
@@ -137,6 +187,11 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
|
||||
expect(yield* service.uninstall).toBe(true);
|
||||
expect((yield* service.status).installed).toBe(false);
|
||||
expect(commands.some((command) => command.startsWith("npm "))).toBe(false);
|
||||
// The stop can block up to systemd's 90s TimeoutStopSec; the runner's
|
||||
// 60s default would cancel it mid-shutdown.
|
||||
expect(timeouts.get("systemctl --user disable --now t3code.service")).toEqual(
|
||||
Duration.seconds(120),
|
||||
);
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -195,11 +250,95 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("fails closed off Linux", () =>
|
||||
it.effect("fails closed on Windows", () =>
|
||||
Effect.gen(function* () {
|
||||
const { service } = yield* makeHarness("darwin");
|
||||
const { service } = yield* makeHarness("win32");
|
||||
expect((yield* service.status).supported).toBe(false);
|
||||
expect((yield* service.install.pipe(Effect.flip))._tag).toBe("BootServiceUnsupportedError");
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("installs, reports current state, and uninstalls on macOS", () =>
|
||||
Effect.gen(function* () {
|
||||
const { service, fs, statePath, commands, timeouts } = yield* makeHarness("darwin");
|
||||
const plan = yield* service.install;
|
||||
|
||||
expect(plan.unitPath.endsWith("Library/LaunchAgents/com.t3tools.t3code.service.plist")).toBe(
|
||||
true,
|
||||
);
|
||||
expect(parseServiceState(yield* fs.readFileString(statePath))).toEqual({
|
||||
protocol: SERVICE_LAUNCHER_PROTOCOL,
|
||||
activeVersion: "1.2.3",
|
||||
});
|
||||
expect(yield* fs.readFileString(plan.launcherPath)).toBe("export {};\n");
|
||||
expect((yield* service.status).current).toBe(true);
|
||||
expect(yield* service.uninstall).toBe(true);
|
||||
expect((yield* service.status).installed).toBe(false);
|
||||
expect(commands.some((command) => command.startsWith("npm "))).toBe(false);
|
||||
expect(commands.some((command) => command.startsWith("systemctl "))).toBe(false);
|
||||
// A bootout can block up to the plist's 90s ExitTimeOut; the runner's
|
||||
// 60s default would cancel it and let bootstrap race a loaded job.
|
||||
expect(timeouts.get("launchctl bootout --wait gui/501/com.t3tools.t3code.service")).toEqual(
|
||||
Duration.seconds(120),
|
||||
);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("restarts the launch agent when repair fails", () =>
|
||||
Effect.gen(function* () {
|
||||
const { service, commands, control } = yield* makeHarness("darwin");
|
||||
yield* service.install;
|
||||
const plistPath = (yield* service.status).unitPath;
|
||||
commands.length = 0;
|
||||
control.failCommand = `launchctl bootstrap gui/501 ${plistPath}`;
|
||||
|
||||
const error = yield* service.install.pipe(Effect.flip);
|
||||
expect(error._tag).toBe("BootServiceCommandError");
|
||||
expect(commands.filter((command) => command.startsWith("launchctl "))).toEqual([
|
||||
"launchctl bootout --wait gui/501/com.t3tools.t3code.service",
|
||||
"launchctl enable gui/501/com.t3tools.t3code.service",
|
||||
`launchctl bootstrap gui/501 ${plistPath}`,
|
||||
`launchctl bootstrap gui/501 ${plistPath}`,
|
||||
]);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("ignores a bootout for an agent that is not loaded", () =>
|
||||
Effect.gen(function* () {
|
||||
const { service, control } = yield* makeHarness("darwin");
|
||||
yield* service.install;
|
||||
control.failCommand = "launchctl bootout --wait gui/501/com.t3tools.t3code.service";
|
||||
|
||||
yield* service.install;
|
||||
expect((yield* service.status).current).toBe(true);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("restarts without overwriting a pending remote update on macOS", () =>
|
||||
Effect.gen(function* () {
|
||||
const { service, fs, statePath, commands } = yield* makeHarness("darwin");
|
||||
yield* service.install;
|
||||
const plistPath = (yield* service.status).unitPath;
|
||||
// @effect-diagnostics-next-line preferSchemaOverJson:off - fixed launcher-owned test document.
|
||||
const pendingState = JSON.stringify({
|
||||
protocol: SERVICE_LAUNCHER_PROTOCOL - 1,
|
||||
activeVersion: "1.2.3",
|
||||
update: {
|
||||
id: "remote-update",
|
||||
fromVersion: "1.2.3",
|
||||
targetVersion: "1.2.4",
|
||||
status: "pending",
|
||||
},
|
||||
});
|
||||
yield* fs.writeFileString(statePath, pendingState);
|
||||
commands.length = 0;
|
||||
|
||||
expect((yield* service.install.pipe(Effect.flip))._tag).toBe("BootServiceUpdatePendingError");
|
||||
expect(serviceStateHasPendingUpdate(yield* fs.readFileString(statePath))).toBe(true);
|
||||
expect(commands.filter((command) => command.startsWith("launchctl "))).toEqual([
|
||||
"launchctl bootout --wait gui/501/com.t3tools.t3code.service",
|
||||
`launchctl bootstrap gui/501 ${plistPath}`,
|
||||
]);
|
||||
}),
|
||||
);
|
||||
});
|
||||
Reference in new issue
Block a user