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

Function ensureDaemon

apps/cli/src/main.ts:638–691  ·  view source on GitHub ↗
(
  baseUrl: string,
)

Source from the content-addressed store, hash-verified

636// daemon process on the user's behalf only makes sense when "the user's machine" is
637// also where the request will land.
638const ensureDaemon = (
639 baseUrl: string,
640): Effect.Effect<string, Error, FileSystem.FileSystem | PlatformPath.Path> =>
641 Effect.gen(function* () {
642 const resolvedTarget = yield* resolveDaemonTarget(baseUrl);
643 if (resolvedTarget.fromPointer && (yield* isServerReachable(resolvedTarget.baseUrl))) {
644 return resolvedTarget.baseUrl;
645 }
646
647 const active = yield* readActiveLocalServerManifest();
648 const activeOrigin = active
649 ? normalizeExecutorServerConnection({ origin: active.connection.origin }).origin
650 : null;
651 const targetOrigin = normalizeExecutorServerConnection({
652 origin: resolvedTarget.baseUrl,
653 }).origin;
654 if (activeOrigin === targetOrigin) {
655 return resolvedTarget.baseUrl;
656 }
657
658 if (active && activeOrigin !== targetOrigin) {
659 return yield* Effect.fail(
660 new Error(
661 [
662 `A local Executor ${active.kind} is already running at ${active.connection.origin} (pid ${active.pid}).`,
663 `It owns the current data directory: ${active.dataDir}`,
664 "Refusing to start another local daemon against the same database.",
665 ].join("\n"),
666 ),
667 );
668 }
669
670 const parsed = yield* parseDaemonUrl(baseUrl);
671 const host = canonicalDaemonHost(parsed.hostname);
672
673 if (!canAutoStartLocalDaemonForHost(host)) {
674 return yield* Effect.fail(
675 new Error(
676 [
677 `Executor daemon is not reachable at ${baseUrl}.`,
678 "Auto-start is only supported for local hosts.",
679 `Start it manually: ${cliPrefix} daemon run --port ${parsed.port} --hostname ${host}`,
680 ].join("\n"),
681 ),
682 );
683 }
684
685 return yield* spawnAndWaitForDaemon({
686 host,
687 scopeId: resolvedTarget.scopeId,
688 preferredPort: parsed.port,
689 allowedHosts: [],
690 });
691 }).pipe(Effect.mapError(toError));
692
693const resolveRequestedExecutorServerConnection = (
694 target: ServerTarget,

Callers 3

runStdioMcpSessionFunction · 0.85
main.tsFile · 0.85

Calls 7

canonicalDaemonHostFunction · 0.90
resolveDaemonTargetFunction · 0.85
isServerReachableFunction · 0.85
parseDaemonUrlFunction · 0.85
spawnAndWaitForDaemonFunction · 0.85

Tested by

no test coverage detected