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

Function claimAndBoot

e2e/src/ports.ts:258–299  ·  view source on GitHub ↗
(
  claims: ReadonlyArray<PortClaim>,
  boot: (ports: Record<string, number>) => Promise<{ teardown: () => Promise<void>; value: T }>,
  options: {
    readonly maxAttempts?: number;
    readonly label?: string;
    /** Additional acquisition failures that are safe to retry from scratch. */
    readonly retryWhen?: (error: unknown) => boolean;
  } = {},
)

Source from the content-addressed store, hash-verified

256 * returned `teardown` chains the caller's teardown then releases the block.
257 */
258export const claimAndBoot = async <T>(
259 claims: ReadonlyArray<PortClaim>,
260 boot: (ports: Record<string, number>) => Promise<{ teardown: () => Promise<void>; value: T }>,
261 options: {
262 readonly maxAttempts?: number;
263 readonly label?: string;
264 /** Additional acquisition failures that are safe to retry from scratch. */
265 readonly retryWhen?: (error: unknown) => boolean;
266 } = {},
267): Promise<{ ports: Record<string, number>; teardown: () => Promise<void>; value: T }> => {
268 const maxAttempts = options.maxAttempts ?? 3;
269 const label = options.label ?? "boot";
270 let lastError: unknown;
271 for (let attempt = 1; attempt <= maxAttempts; attempt++) {
272 const { ports, release } = await claimPorts(claims);
273 try {
274 const booted = await boot(ports);
275 return {
276 ports,
277 value: booted.value,
278 teardown: async () => {
279 await booted.teardown();
280 await release();
281 },
282 };
283 } catch (error) {
284 await release();
285 lastError = error;
286 const retryable = isAddrInUse(error) || options.retryWhen?.(error) === true;
287 if (!retryable || attempt === maxAttempts) throw error;
288 const collided = claims
289 .map((claim) => ports[claim.envVar])
290 .filter((port): port is number => port !== undefined)
291 .join(", ");
292 const reason = isAddrInUse(error) ? `hit EADDRINUSE on port(s) ${collided}` : String(error);
293 console.warn(
294 `[e2e] ${label} acquisition failed (${reason}, attempt ${attempt}/${maxAttempts}); re-claiming a fresh block and retrying`,
295 );
296 }
297 }
298 throw lastError;
299};

Callers 5

setupFunction · 0.90
setupFunction · 0.90
setupFunction · 0.90
setupFunction · 0.90

Calls 5

claimPortsFunction · 0.85
bootFunction · 0.85
isAddrInUseFunction · 0.85
warnMethod · 0.80
releaseFunction · 0.50

Tested by

no test coverage detected