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

Function ensureDaemon

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

Source from the content-addressed store, hash-verified

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