MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / spawnAndWaitForDaemon

Function spawnAndWaitForDaemon

apps/cli/src/main.ts:604–649  ·  view source on GitHub ↗
(input: {
  host: string;
  scopeId: string;
  preferredPort: number;
  allowedHosts: ReadonlyArray<string>;
})

Source from the content-addressed store, hash-verified

602 });
603
604const spawnAndWaitForDaemon = (input: {
605 host: string;
606 scopeId: string;
607 preferredPort: number;
608 allowedHosts: ReadonlyArray<string>;
609}): Effect.Effect<string, Error, FileSystem.FileSystem | PlatformPath.Path> =>
610 Effect.gen(function* () {
611 const requestedBaseUrl = daemonBaseUrl(input.host, input.preferredPort);
612
613 for (let attempt = 1; attempt <= MAX_DAEMON_ELECTION_ATTEMPTS; attempt++) {
614 const acquired = yield* acquireDaemonStartLock({
615 hostname: input.host,
616 scopeId: input.scopeId,
617 }).pipe(
618 Effect.map((lock) => ({ held: true as const, lock })),
619 Effect.catch((error) =>
620 isStartLockContention(error)
621 ? Effect.succeed({ held: false as const, lock: null })
622 : Effect.fail(error),
623 ),
624 );
625
626 if (acquired.held) {
627 const lock = acquired.lock;
628 return yield* spawnDaemonAsLockHolder(input).pipe(
629 Effect.ensuring(releaseDaemonStartLock(lock).pipe(Effect.ignore)),
630 );
631 }
632
633 // Lost the lock: wait for the current holder to advertise a manifest.
634 const ready = yield* waitForDaemonStartupTarget({ requestedBaseUrl });
635 if (ready) return ready;
636 // Timed out with no manifest. The holder may have died mid-startup; loop to
637 // re-acquire, which reclaims its now-stale lock.
638 }
639
640 return yield* Effect.fail(
641 new Error(
642 [
643 `Could not elect or attach to a local Executor daemon after ${MAX_DAEMON_ELECTION_ATTEMPTS} attempts.`,
644 "A daemon startup may be stuck. Stop any partial daemon and retry, or run it in the foreground:",
645 `${cliPrefix} daemon run --foreground --port ${input.preferredPort} --hostname ${input.host}`,
646 ].join("\n"),
647 ),
648 );
649 });
650
651// Auto-start a local daemon on demand so commands like `executor call` work without the
652// user having to run `daemon run` first. Refuses non-local hosts because spawning a

Callers 2

ensureDaemonFunction · 0.85
runBackgroundDaemonStartFunction · 0.85

Calls 6

acquireDaemonStartLockFunction · 0.90
releaseDaemonStartLockFunction · 0.90
daemonBaseUrlFunction · 0.85
isStartLockContentionFunction · 0.85
spawnDaemonAsLockHolderFunction · 0.85

Tested by

no test coverage detected