everything-dev
Version:
A consolidated product package for building Module Federation apps with oRPC APIs.
289 lines (287 loc) • 11.8 kB
JavaScript
import { getProcessEnv } from "./env/process-env.mjs";
import { DevGeneratedEnvLive, DevRuntimeConfig, DevRuntimeConfigLive, ServiceDescriptorMap, ServiceDescriptorMapLive, isAuthMirrorPluginEntry } from "./service-descriptor.mjs";
import { createLogPipeline, isDebug, resolveLogLevel } from "./dev-log-pipeline.mjs";
import { isPidAlive, readRegistry, registerStandalone, unregisterPid, updateChildPids, writeRegistry } from "./process-registry.mjs";
import { probePortBindable } from "./app.mjs";
import { ShellEnvLive } from "./env/project-env.mjs";
import { ownerOfPort } from "./infra/port-ownership.mjs";
import { getProcessStates, makeDevProcess } from "./orchestrator.mjs";
import { createDevLogger, formatLogLine } from "./dev-logs.mjs";
import { reapGroup } from "./process-kill.mjs";
import { createDevRenderer } from "./components/dev-render.mjs";
import { Clock, DateTime, Deferred, Effect, Exit, Layer } from "effect";
//#region src/dev-session.ts
const adoptOrphanedChildren = (configDir) => {
try {
const entries = readRegistry().filter((entry) => entry.pid > 1 && entry.configDir === configDir && !isPidAlive(entry.pid));
if (entries.length === 0) return;
const reaped = [];
for (const entry of entries) for (const childPid of entry.childPids ?? []) {
if (childPid === process.pid) continue;
if (isPidAlive(childPid)) {
reapGroup(childPid);
reaped.push(childPid);
}
}
if (reaped.length > 0) console.error(`[Dev] Reaped ${reaped.length} orphaned child process(es) from dead session(s): ${reaped.join(", ")}`);
const deadPids = new Set(entries.map((entry) => entry.pid));
writeRegistry(readRegistry().filter((entry) => !deadPids.has(entry.pid)));
} catch {}
};
const isInteractiveSupported = () => {
return process.stdin.isTTY === true && process.stdout.isTTY === true;
};
const STARTUP_ORDER = [
"ui",
"auth",
"api",
"plugin",
"host-build",
"host"
];
const sortByOrder = (packages) => {
return [...packages].sort((a, b) => {
const aIdx = a.startsWith("plugin:") ? STARTUP_ORDER.indexOf("plugin") : STARTUP_ORDER.indexOf(a);
const bIdx = b.startsWith("plugin:") ? STARTUP_ORDER.indexOf("plugin") : STARTUP_ORDER.indexOf(b);
if (aIdx === -1 && bIdx === -1) return 0;
if (aIdx === -1) return 1;
if (bIdx === -1) return -1;
return aIdx - bIdx;
});
};
const runDevSession = (configDir, orchestrator, onShutdownReady) => Effect.gen(function* () {
adoptOrphanedChildren(configDir);
const services = yield* ServiceDescriptorMap;
const runtimeConfig = yield* DevRuntimeConfig;
const orderedPackages = sortByOrder(orchestrator.packages);
const initialProcesses = getProcessStates(orderedPackages, services, orchestrator.port);
if (isDebug(getProcessEnv("DEBUG"))) {
yield* Effect.logError(`[DEBUG session] orchestrator.packages: ${orchestrator.packages.join(", ")}`);
yield* Effect.logError(`[DEBUG session] orderedPackages: ${orderedPackages.join(", ")}`);
yield* Effect.logError(`[DEBUG session] services keys: ${[...services.keys()].join(", ")}`);
yield* Effect.logError(`[DEBUG session] initialProcesses: ${initialProcesses.map((p) => `${p.name}=${p.source}`).join(", ")}`);
}
const logger = yield* Effect.promise(() => createDevLogger(configDir, orchestrator.description));
const shutdown = yield* Deferred.make();
const effectContext = yield* Effect.context();
const controls = {
requestShutdown: () => {
Effect.runPromiseWith(effectContext)(Deferred.succeed(shutdown, void 0));
},
emergencyKill: () => {},
requestShutdownEscalating: () => {},
forceExit: () => {},
restoreView: () => {},
rearmForceExitTimer: () => {}
};
onShutdownReady?.(controls);
const isWorkspaceChild = getProcessEnv("BOS_WORKSPACE_CHILD") === "1";
let ownedPorts = [];
if (!isWorkspaceChild) {
const regPorts = {};
const addPort = (key, value) => {
if (value != null) regPorts[key] = value;
};
addPort("host", runtimeConfig.host.port);
addPort("api", runtimeConfig.api.port);
addPort("ui", runtimeConfig.ui.port);
addPort("auth", runtimeConfig.auth?.port);
if (runtimeConfig.plugins) for (const [id, plugin] of Object.entries(runtimeConfig.plugins)) {
if (!isAuthMirrorPluginEntry(runtimeConfig.auth, id, plugin)) addPort(`plugin:${id}`, plugin.port);
addPort(`plugin-ui:${id}`, plugin.ui?.port);
}
registerStandalone({
pid: process.pid,
configDir,
ports: regPorts,
startedAt: yield* Clock.currentTimeMillis,
description: orchestrator.description
});
ownedPorts = Object.values(regPorts);
}
let view = null;
let shouldExportLogs = false;
const logLevel = resolveLogLevel();
const allLogs = [];
const pipeline = createLogPipeline({
level: logLevel,
sinks: {
display: (event) => {
view?.addLog(event.source, event.line, event.level === "error");
},
file: (event) => {
if (orchestrator.noLogs) return;
logger.write(event);
},
export: (event) => {
allLogs.push(event);
}
}
});
const useInteractive = isInteractiveSupported() && orchestrator.interactive !== false;
view = createDevRenderer(initialProcesses, orchestrator.description, orchestrator.env, () => controls.requestShutdownEscalating(), () => {
shouldExportLogs = true;
controls.requestShutdownEscalating();
}, {
interactive: useInteractive,
onForceExit: () => controls.forceExit()
});
controls.restoreView = () => {
if (useInteractive) view?.unmount();
};
const callbacks = {
onStatus: (name, status, message) => {
view?.updateProcess(name, status, message);
},
onLog: (name, line, isError) => {
pipeline.ingest({
source: name,
line,
isError
});
}
};
const spawned = [];
controls.emergencyKill = () => {
for (const handle of spawned) {
if (handle.pid === void 0) continue;
reapGroup(handle.pid);
}
};
yield* Effect.addFinalizer(() => Effect.gen(function* () {
controls.rearmForceExitTimer();
yield* Effect.forEach(spawned, (h) => h.kill.pipe(Effect.ignore), { concurrency: "unbounded" });
yield* Effect.sleep("200 millis");
for (const port of ownedPorts) if (!(yield* probePortBindable(port))) {
const owner = yield* ownerOfPort(port);
yield* Effect.logError(`[Dev] Port ${port} still bound after teardown${owner ? ` — pid ${owner.pid} (${owner.command})` : " — owner unknown (lsof unavailable)"}`);
}
if (!isWorkspaceChild) yield* Effect.sync(() => {
try {
unregisterPid(process.pid);
} catch {}
});
pipeline.flush();
view?.unmount();
if (shouldExportLogs) {
const now = yield* Clock.currentTimeMillis;
const startedMs = allLogs[0]?.timestamp || now;
yield* Effect.log("");
yield* Effect.log("═".repeat(70));
yield* Effect.log(` SESSION LOGS: ${orchestrator.description}`);
yield* Effect.log(` Started: ${DateTime.formatIso(DateTime.makeUnsafe(startedMs))}`);
yield* Effect.log(` Filtered entries: ${allLogs.length} (level: ${logLevel})`);
yield* Effect.log("═".repeat(70));
yield* Effect.log("");
for (const event of allLogs) yield* Effect.log(formatLogLine(event));
yield* Effect.log("");
yield* Effect.log("═".repeat(70));
yield* Effect.log(` Full logs saved to: ${logger.logFile}`);
yield* Effect.log("═".repeat(70));
yield* Effect.log("");
}
}));
const startProcess = (pkg) => {
const portOverride = pkg === "host" ? orchestrator.port : void 0;
return makeDevProcess(pkg, callbacks, portOverride).pipe(Effect.tap((handle) => Effect.sync(() => spawned.push(handle))), Effect.flatMap((handle) => Effect.acquireRelease(Effect.succeed(handle), () => handle.kill.pipe(Effect.ignore))), Effect.tapError((err) => Effect.sync(() => {
callbacks.onLog(pkg, `Failed to start: ${String(err)}`, true);
callbacks.onStatus(pkg, "error");
})), Effect.orElseSucceed(() => ({
name: pkg,
pid: void 0,
kill: Effect.void,
waitForReady: Effect.void,
waitForExit: Effect.never
})));
};
const startGroup = (packages) => Effect.forEach(packages, startProcess, { concurrency: "unbounded" });
const awaitReady = (pkg, handle) => handle.waitForReady.pipe(Effect.timeout("120 seconds"), Effect.catch((err) => Effect.sync(() => {
callbacks.onLog(pkg, `Timed out or failed: ${err instanceof Error ? err.message : String(err)}`, true);
})));
const nonHostPackages = orderedPackages.filter((pkg) => pkg !== "host");
const hostPackages = orderedPackages.filter((pkg) => pkg === "host");
const startupPhase = Effect.gen(function* () {
const nonHostHandles = yield* startGroup(nonHostPackages);
yield* Effect.forEach(nonHostHandles.map((handle, index) => ({
handle,
pkg: nonHostPackages[index] ?? handle.name
})), ({ handle, pkg }) => awaitReady(pkg, handle), { concurrency: "unbounded" });
const hostHandles = yield* startGroup(hostPackages);
yield* Effect.forEach(hostHandles.map((handle, index) => ({
handle,
pkg: hostPackages[index] ?? handle.name
})), ({ handle, pkg }) => awaitReady(pkg, handle), { concurrency: "unbounded" });
return {
nonHostHandles,
hostHandles
};
});
const startup = yield* Effect.raceFirst(startupPhase, Deferred.await(shutdown).pipe(Effect.as(null)));
if (startup === null) return;
const childPids = [...startup.nonHostHandles, ...startup.hostHandles].map((handle) => Number(handle.pid)).filter((pid) => Number.isFinite(pid) && pid > 1 && pid !== process.pid);
if (!isWorkspaceChild && childPids.length > 0) yield* Effect.sync(() => {
try {
updateChildPids(process.pid, childPids);
} catch {}
});
yield* Deferred.await(shutdown);
});
const runApp = (configDir, orchestrator, services, runtimeConfig, envGenerated, shellEnv) => {
let controls = null;
let signalCount = 0;
let forceExitTimer = null;
const forceExit = () => {
controls?.restoreView();
console.log("\n[Dev] Force exit");
controls?.emergencyKill();
process.exit(0);
};
const initialPpid = process.ppid;
const orphanWatch = setInterval(() => {
if (process.ppid !== initialPpid) {
console.error("\n[Dev] Parent process died — force exiting (orphaned service tree)");
forceExit();
}
}, 200);
orphanWatch.unref?.();
const requestShutdownEscalating = () => {
signalCount++;
if (signalCount > 1) {
forceExit();
return;
}
controls?.restoreView();
console.log("\n[Dev] Shutting down...");
forceExitTimer = setTimeout(forceExit, 5e3);
forceExitTimer.unref?.();
controls?.requestShutdown();
};
const handleSignal = requestShutdownEscalating;
const program = Effect.scoped(runDevSession(configDir, orchestrator, (sessionControls) => {
controls = sessionControls;
sessionControls.requestShutdownEscalating = requestShutdownEscalating;
sessionControls.rearmForceExitTimer = () => {
if (forceExitTimer) clearTimeout(forceExitTimer);
forceExitTimer = setTimeout(forceExit, 5e3);
forceExitTimer.unref?.();
};
sessionControls.forceExit = forceExit;
})).pipe(Effect.provide(Layer.mergeAll(ServiceDescriptorMapLive(services), DevRuntimeConfigLive(runtimeConfig), DevGeneratedEnvLive(envGenerated), ShellEnvLive(shellEnv))), Effect.catchDefect((defect) => Effect.logError("[Dev] Unhandled defect in orchestrator:", defect).pipe(Effect.andThen(Effect.die(defect)))));
process.on("SIGINT", handleSignal);
process.on("SIGTERM", handleSignal);
Effect.runPromiseExit(program).then((exit) => {
clearInterval(orphanWatch);
if (forceExitTimer) clearTimeout(forceExitTimer);
process.exit(Exit.isSuccess(exit) ? 0 : 1);
}).catch((error) => {
clearInterval(orphanWatch);
if (forceExitTimer) clearTimeout(forceExitTimer);
console.error("[Dev] Failed to observe application exit:", error);
process.exit(1);
});
};
const devApp = runApp;
const startApp = runApp;
//#endregion
export { devApp, runDevSession, startApp };
//# sourceMappingURL=dev-session.mjs.map