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

Function start

packages/kernel/runtime-workerd-subprocess/src/index.ts:465–551  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

463 };
464
465 const start = async () => {
466 if (disposed) throw new Error("workerd runner has been disposed");
467 if (processState) return;
468 if (starting) return starting;
469
470 starting = (async () => {
471 const now = Date.now();
472 const delayMs = Math.max(0, restartBackoffMs - (now - lastExitAt));
473 if (delayMs > 0) {
474 await new Promise((resolve) => setTimeout(resolve, delayMs));
475 }
476
477 const tmp = join(
478 tmpdir(),
479 `executor-workerd-${process.pid}-${Date.now()}-${crypto.randomUUID()}`,
480 );
481 await mkdir(tmp, { recursive: true, mode: 0o700 });
482 for (const [name, source] of Object.entries(options.modules)) {
483 await mkdir(dirname(join(tmp, name)), { recursive: true, mode: 0o700 });
484 if (typeof source === "string") {
485 await writeFile(join(tmp, name), source, { mode: 0o600 });
486 } else if (source.kind === "wasm") {
487 await writeFile(join(tmp, name), source.bytes, { mode: 0o600 });
488 } else {
489 await writeFile(join(tmp, name), source.source, { mode: 0o600 });
490 }
491 }
492
493 const hostServer = await startToolServer(options.toolBridge, token);
494 const reservation = await reserveLocalPort();
495 const listenPort = reservation.port;
496 await writeFile(
497 join(tmp, "config.capnp"),
498 buildConfig({
499 listenPort,
500 hostPort: options.toolBridge ? hostServer.port : null,
501 modules: options.modules,
502 mainModule: options.mainModule,
503 compatibilityDate,
504 compatibilityFlags,
505 unsafeEval: options.unsafeEval ?? false,
506 globalOutbound: options.globalOutbound ?? "blocked",
507 }),
508 { mode: 0o600 },
509 );
510 await reservation.close();
511
512 const workerdBin = await findWorkerdBin(options.workerdBin);
513 const proc = spawn(workerdBin, ["serve", join(tmp, "config.capnp"), "--experimental"], {
514 stdio: ["ignore", "pipe", "pipe"],
515 });
516 proc.stderr.setEncoding("utf8");
517 proc.stdout.resume();
518 let stderrTail = "";
519 proc.stderr.on("data", (chunk: string) => {
520 stderrTail = (stderrTail + chunk).slice(-4000);
521 });
522 proc.on("exit", (code, signal) => {

Callers 1

runFunction · 0.70

Calls 9

startToolServerFunction · 0.85
reserveLocalPortFunction · 0.85
findWorkerdBinFunction · 0.85
failPendingFunction · 0.85
waitReadyFunction · 0.85
disposeStateFunction · 0.85
buildConfigFunction · 0.70
normalizeErrorFunction · 0.70
closeMethod · 0.65

Tested by

no test coverage detected