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

Function spawnAndWaitForDaemon

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

Source from the content-addressed store, hash-verified

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