refactor(startup): unify dev and cli daemon startup through runner

This commit is contained in:
Mohamed Boudra
2026-02-16 03:22:03 +00:00
parent 5bd6cc11de
commit 04a06197de
5 changed files with 87 additions and 171 deletions

View File

@@ -1,6 +1,27 @@
import { fileURLToPath } from "url";
import { existsSync } from "node:fs";
import { runSupervisor } from "./supervisor.js";
import { applySherpaLoaderEnv } from "../src/server/speech/providers/local/sherpa/sherpa-runtime-env.js";
type DaemonRunnerConfig = {
devMode: boolean;
workerArgs: string[];
};
function parseConfig(argv: string[]): DaemonRunnerConfig {
let devMode = false;
const workerArgs: string[] = [];
for (const arg of argv) {
if (arg === "--dev") {
devMode = true;
continue;
}
workerArgs.push(arg);
}
return { devMode, workerArgs };
}
function resolveWorkerEntry(): string {
const candidates = [
@@ -19,18 +40,32 @@ function resolveWorkerEntry(): string {
return candidates[0];
}
function resolveWorkerExecArgv(): string[] {
const workerEntry = resolveWorkerEntry();
function resolveDevWorkerEntry(): string {
const candidate = fileURLToPath(new URL("../src/server/index.ts", import.meta.url));
if (!existsSync(candidate)) {
throw new Error(`Dev worker entry not found: ${candidate}`);
}
return candidate;
}
function resolveWorkerExecArgv(workerEntry: string): string[] {
return workerEntry.endsWith(".ts") ? ["--import", "tsx"] : [];
}
const config = parseConfig(process.argv.slice(2));
const workerEntry = config.devMode ? resolveDevWorkerEntry() : resolveWorkerEntry();
applySherpaLoaderEnv(process.env);
runSupervisor({
name: "DaemonRunner",
startupMessage: "Starting daemon worker (IPC restart enabled)",
resolveWorkerEntry,
workerArgs: process.argv.slice(2),
startupMessage: config.devMode
? "Starting daemon worker (dev mode, crash restarts enabled)"
: "Starting daemon worker (IPC restart enabled)",
resolveWorkerEntry: () => workerEntry,
workerArgs: config.workerArgs,
workerEnv: process.env,
workerExecArgv: resolveWorkerExecArgv(),
restartOnCrash: false,
workerExecArgv: resolveWorkerExecArgv(workerEntry),
restartOnCrash: config.devMode,
shutdownReasons: ["cli_shutdown"],
});