fix: harden workflow console runtime

This commit is contained in:
lda
2026-07-02 17:35:16 +07:00 Verified
parent bcf31583af
commit ea1276badd
22 changed files with 684 additions and 128 deletions
+31 -3
View File
@@ -1,5 +1,7 @@
import { Effect, Layer } from "effect";
import { Effect } from "effect";
import { serve } from "@hono/node-server";
import * as fs from "node:fs";
import * as path from "node:path";
import { fileURLToPath } from "node:url";
import {
WorkflowRpc,
@@ -19,6 +21,13 @@ const hostname = process.env.WEB_HOST ?? "127.0.0.1";
const liveLayer = makeWorkflowRpcLayer();
const consoleRoot = fileURLToPath(new URL("../../console/dist", import.meta.url));
const consoleIndex = path.join(consoleRoot, "index.html");
const staticConsoleRoot = fs.existsSync(consoleIndex) ? consoleRoot : undefined;
if (!staticConsoleRoot) {
// In dev, Vite serves the console and proxies /api to this server. A missing
// dist directory should not prevent the API proxy from starting.
console.warn(`console dist not found; serving API only: ${consoleRoot}`);
}
const runOperation: RunOperation = async (
operation: OperationName,
@@ -30,12 +39,31 @@ const runOperation: RunOperation = async (
return yield* execute(operation, target, params);
}).pipe(Effect.provide(liveLayer), Effect.runPromise);
const app = createApp({ runOperation, consoleRoot });
let app: ReturnType<typeof createApp>;
try {
app = createApp({
runOperation,
...(staticConsoleRoot ? { consoleRoot: staticConsoleRoot } : {}),
});
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
console.error(`Failed to start workflow console server: ${message}`);
process.exit(1);
}
serve({
const server = serve({
fetch: app.fetch,
hostname,
port,
});
console.log(`workflow console server listening on http://${hostname}:${port}`);
const shutdown = (signal: NodeJS.Signals) => {
console.log(`received ${signal}, stopping workflow console server`);
server.close(() => process.exit(0));
setTimeout(() => process.exit(1), 5_000).unref();
};
process.once("SIGINT", shutdown);
process.once("SIGTERM", shutdown);