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

Function spawnDaemonAsLockHolder

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

Source from the content-addressed store, hash-verified

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