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

Function spawnAndWaitForDaemon

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

Source from the content-addressed store, hash-verified

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