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

Function spawnDaemonAsLockHolder

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

Source from the content-addressed store, hash-verified

537 error.message.includes("Another daemon startup is already in progress");
538
539const spawnDaemonAsLockHolder = (input: {
540 host: string;
541 scopeId: string;
542 preferredPort: number;
543 allowedHosts: ReadonlyArray<string>;
544}): Effect.Effect<string, Error, FileSystem.FileSystem | PlatformPath.Path> =>
545 Effect.gen(function* () {
546 const existing = yield* readDaemonPointer({ hostname: input.host, scopeId: input.scopeId });
547 if (existing && isPidAlive(existing.pid)) {
548 const existingUrl = daemonBaseUrl(existing.hostname, existing.port);
549 if (yield* isServerReachable(existingUrl)) {
550 return existingUrl;
551 }
552 }
553
554 const selectedPort = yield* chooseDaemonPort({
555 preferredPort: input.preferredPort,
556 hostname: input.host,
557 });
558
559 if (selectedPort !== input.preferredPort) {
560 console.error(
561 `Port ${input.preferredPort} is in use. Starting daemon on available port ${selectedPort} instead.`,
562 );
563 }
564
565 const spec = yield* Effect.try({
566 try: () =>
567 buildDaemonSpawnSpec({
568 port: selectedPort,
569 hostname: input.host,
570 isDevMode,
571 scriptPath: script,
572 executablePath: process.execPath,
573 allowedHosts: input.allowedHosts,
574 }),
575 catch: (cause) =>
576 cause instanceof Error
577 ? cause
578 : new Error(`Failed to build daemon command: ${String(cause)}`),
579 });
580
581 const startBaseUrl = daemonBaseUrl(input.host, selectedPort);
582 console.error(`Starting daemon on ${input.host}:${selectedPort}...`);
583 const child = yield* spawnDetached({
584 command: spec.command,
585 args: spec.args,
586 env: process.env,
587 });
588
589 const readyBaseUrl = yield* waitForDaemonStartupTarget({ requestedBaseUrl: startBaseUrl });
590
591 if (!readyBaseUrl) {
592 yield* terminateSpawnedDetachedProcess(child).pipe(Effect.ignore);
593 return yield* Effect.fail(
594 new Error(
595 [
596 `Daemon did not become reachable at ${startBaseUrl} and no reachable local server manifest appeared within ${DAEMON_BOOT_TIMEOUT_MS}ms.`,

Callers 1

spawnAndWaitForDaemonFunction · 0.85

Calls 10

readDaemonPointerFunction · 0.90
isPidAliveFunction · 0.90
chooseDaemonPortFunction · 0.90
buildDaemonSpawnSpecFunction · 0.90
spawnDetachedFunction · 0.90
daemonBaseUrlFunction · 0.85
isServerReachableFunction · 0.85
errorMethod · 0.80

Tested by

no test coverage detected