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

Function spawnDaemonAsLockHolder

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

Source from the content-addressed store, hash-verified

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