Skip to content

Commit 8415aee

Browse files
committed
refactor(server): share launchd shutdown command validation
1 parent 8d0e75b commit 8415aee

2 files changed

Lines changed: 25 additions & 49 deletions

File tree

‎apps/server/src/cloud/bootService.test.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,15 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
9494
linger: string;
9595
enabled: boolean;
9696
active: boolean;
97+
missingExitCode: number;
9798
drainPolls: number;
9899
onDrainPoll: Effect.Effect<void>;
99100
} = {
100101
failCommand: undefined,
101102
linger: "yes",
102103
enabled: true,
103104
active: true,
105+
missingExitCode: 113,
104106
drainPolls: 0,
105107
onDrainPoll: Effect.void,
106108
};
@@ -152,7 +154,7 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
152154
launchdPrint
153155
? control.active || draining
154156
? 0
155-
: 113
157+
: control.missingExitCode
156158
: failed ||
157159
(input.args[1] === "is-active" && !control.active) ||
158160
(input.args[1] === "is-enabled" && !control.enabled)
@@ -849,11 +851,12 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
849851
}),
850852
);
851853

852-
it.effect("ignores a bootout for an agent that is not loaded", () =>
854+
it.effect.each([112, 113])("ignores a bootout for an agent missing with exit %s", (code) =>
853855
Effect.gen(function* () {
854856
const { service, control } = yield* makeHarness("darwin");
855857
yield* service.install();
856858
control.active = false;
859+
control.missingExitCode = code;
857860
control.failCommand = "launchctl bootout gui/501/com.t3tools.t3code.service";
858861

859862
yield* service.install();

‎apps/server/src/cloud/bootService.ts‎

Lines changed: 20 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,7 @@ export interface BootServiceStep {
186186
/** Non-zero exit is logged and ignored. */
187187
readonly optional?: boolean;
188188
readonly timeout?: Duration.Input;
189-
/**
190-
* Re-run once a second while the command exits 0, for up to
191-
* STOP_STEP_TIMEOUT, until it exits with one of these codes. Any other exit
192-
* fails.
193-
*/
189+
/** Poll successful probes until one of these exits, bounded by STOP_STEP_TIMEOUT. */
194190
readonly untilExit?: ReadonlyArray<number>;
195191
}
196192

@@ -300,10 +296,8 @@ function launchdManager(input: {
300296
);
301297
const domainTarget = `gui/${input.uid}`;
302298
const serviceTarget = `${domainTarget}/${BOOT_SERVICE_LAUNCHD_LABEL}`;
303-
// bootout returns while the job drains for up to ExitTimeOut, and a
304-
// bootstrap during the drain fails EIO. `print` finds the job until launchd
305-
// has removed it: 113 is no such service, 112 no such domain. The wait is
306-
// strict, so a bootout that failed with the job still loaded fails here.
299+
// bootout returns before draining finishes; bootstrap races fail EIO until print reports
300+
// no such service (113) or domain (112). Other failures must not permit bootstrap.
307301
const waitForStop: BootServiceStep = {
308302
step: "waiting for the launch agent to stop",
309303
command: "launchctl",
@@ -631,39 +625,26 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
631625
step: string,
632626
command: string,
633627
args: ReadonlyArray<string>,
634-
options?: { readonly timeout?: Duration.Input },
628+
options?: Pick<BootServiceStep, "timeout" | "untilExit">,
635629
) {
636-
return yield* runner.run({ command, args, timeout: options?.timeout }).pipe(
630+
const untilExit = options?.untilExit;
631+
const commandRun = runner.run({ command, args, timeout: options?.timeout });
632+
const run =
633+
untilExit === undefined
634+
? commandRun
635+
: commandRun.pipe(
636+
Effect.repeat({
637+
schedule: Schedule.spaced(Duration.seconds(1)),
638+
while: (result) => result.code === 0,
639+
}),
640+
Effect.timeout(STOP_STEP_TIMEOUT),
641+
Effect.withSpan("cloud.boot_service.wait_for_step"),
642+
);
643+
return yield* run.pipe(
637644
Effect.mapError((cause) => new BootServiceCommandError({ step, cause })),
638645
Effect.filterOrFail(
639-
(result) => result.code === 0,
640646
(result) =>
641-
new BootServiceCommandError({
642-
step,
643-
exitCode: Number(result.code),
644-
stdoutLength: result.stdout.length,
645-
stderrLength: result.stderr.length,
646-
}),
647-
),
648-
Effect.tapError(logFailure),
649-
);
650-
});
651-
652-
const waitForStep = Effect.fn("cloud.boot_service.wait_for_step")(function* (
653-
step: string,
654-
command: string,
655-
args: ReadonlyArray<string>,
656-
untilExit: ReadonlyArray<number>,
657-
) {
658-
return yield* runner.run({ command, args }).pipe(
659-
Effect.repeat({
660-
schedule: Schedule.spaced(Duration.seconds(1)),
661-
while: (result) => result.code === 0,
662-
}),
663-
Effect.timeout(STOP_STEP_TIMEOUT),
664-
Effect.mapError((cause) => new BootServiceCommandError({ step, cause })),
665-
Effect.filterOrFail(
666-
(result) => untilExit.includes(Number(result.code)),
647+
untilExit === undefined ? result.code === 0 : untilExit.includes(Number(result.code)),
667648
(result) =>
668649
new BootServiceCommandError({
669650
step,
@@ -680,15 +661,7 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
680661
Effect.forEach(
681662
steps,
682663
(entry) => {
683-
const run =
684-
entry.untilExit === undefined
685-
? runStep(
686-
entry.step,
687-
entry.command,
688-
entry.args,
689-
entry.timeout === undefined ? undefined : { timeout: entry.timeout },
690-
)
691-
: waitForStep(entry.step, entry.command, entry.args, entry.untilExit);
664+
const run = runStep(entry.step, entry.command, entry.args, entry);
692665
return entry.optional === true ? run.pipe(Effect.ignore) : run.pipe(Effect.asVoid);
693666
},
694667
{ discard: true },

0 commit comments

Comments
 (0)