MCPcopy
hub / github.com/inkeep/open-knowledge / resolveUiLockCollision

Function resolveUiLockCollision

packages/cli/src/commands/ui.ts:432–482  ·  view source on GitHub ↗
(
  opts: ResolveUiLockCollisionOptions,
)

Source from the content-addressed store, hash-verified

430}
431
432export async function resolveUiLockCollision(
433 opts: ResolveUiLockCollisionOptions,
434): Promise<UiCollisionResult> {
435 const readLock =
436 opts.readLock ??
437 (async () => {
438 const { readUiLock } = await import('@inkeep/open-knowledge-server');
439 return readUiLock(opts.lockDir);
440 });
441
442 const initial = await readLock();
443 if (!initial) {
444 throw new Error(
445 'UI lock collision reported but the lock disappeared before handling — retry acquiring.',
446 );
447 }
448
449 if (initial.port === opts.requestedPort && initial.port > 0) {
450 return { mode: 'already-running', port: initial.port };
451 }
452
453 let upstreamPort = initial.port;
454 if (upstreamPort === 0) {
455 const deadline = Date.now() + (opts.pollDeadlineMs ?? 2000);
456 const intervalMs = opts.pollIntervalMs ?? 100;
457 while (Date.now() < deadline) {
458 await new Promise<void>((done) => {
459 setTimeout(done, intervalMs);
460 });
461 const lock = await readLock();
462 if (lock && lock.port > 0) {
463 upstreamPort = lock.port;
464 break;
465 }
466 }
467 if (upstreamPort === 0) {
468 throw new Error('UI did not bind within 2s; run `ok clean`');
469 }
470 if (upstreamPort === opts.requestedPort) {
471 return { mode: 'already-running', port: upstreamPort };
472 }
473 }
474
475 const handle = await startProxyServer({
476 listenPort: opts.requestedPort,
477 host: opts.host,
478 upstreamHost: 'localhost',
479 upstreamPort,
480 });
481 return { mode: 'proxy', handle, upstreamPort };
482}
483
484export function uiCommand(getConfig: () => Config): Command {
485 return new Command('ui')

Callers 2

ui.test.tsFile · 0.90
uiCommandFunction · 0.85

Calls 3

startProxyServerFunction · 0.90
readUiLockFunction · 0.85
readLockFunction · 0.70

Tested by

no test coverage detected