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

Function ensureDaemon

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

Source from the content-addressed store, hash-verified

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