diff --git a/packages/orchestrator/src/cli.ts b/packages/orchestrator/src/cli.ts index 0ad2da39..e6b7da5f 100644 --- a/packages/orchestrator/src/cli.ts +++ b/packages/orchestrator/src/cli.ts @@ -2,6 +2,7 @@ import { readFileSync } from "node:fs"; import { dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; +import { serve } from "./serve.ts"; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); @@ -10,21 +11,32 @@ const packageJson = JSON.parse(readFileSync(join(__dirname, "../package.json"), }; function printHelp(): void { - console.log(`orchestrator v${packageJson.version}\n\nUsage:\n orchestrator --help\n orchestrator --version`); + console.log( + `orchestrator v${packageJson.version}\n\nUsage:\n orchestrator serve\n orchestrator --help\n orchestrator --version`, + ); } -const args = process.argv.slice(2); +async function main(): Promise { + const args = process.argv.slice(2); -if (args.length === 0 || args[0] === "--help" || args[0] === "-h") { + if (args.length === 0 || args[0] === "--help" || args[0] === "-h") { + printHelp(); + process.exit(0); + } + + if (args[0] === "--version" || args[0] === "-v") { + console.log(packageJson.version); + process.exit(0); + } + + if (args[0] === "serve") { + await serve(); + return; + } + + console.error(`Unknown command: ${args[0]}`); printHelp(); - process.exit(0); + process.exit(1); } -if (args[0] === "--version" || args[0] === "-v") { - console.log(packageJson.version); - process.exit(0); -} - -console.error(`Unknown command: ${args[0]}`); -printHelp(); -process.exit(1); +await main(); diff --git a/packages/orchestrator/src/handler.ts b/packages/orchestrator/src/handler.ts index 894308bb..0e894ba0 100644 --- a/packages/orchestrator/src/handler.ts +++ b/packages/orchestrator/src/handler.ts @@ -39,6 +39,7 @@ export async function handleIpcRequest(request: SpawnRequest): Promise; export async function handleIpcRequest(request: StopRequest): Promise; export async function handleIpcRequest(request: StatusRequest): Promise; +export async function handleIpcRequest(request: OrchestratorRequest): Promise; export async function handleIpcRequest(request: OrchestratorRequest): Promise { switch (request.type) { case "spawn": { diff --git a/packages/orchestrator/src/index.ts b/packages/orchestrator/src/index.ts index e16fbc5f..459cd58f 100644 --- a/packages/orchestrator/src/index.ts +++ b/packages/orchestrator/src/index.ts @@ -3,5 +3,6 @@ export * from "./handler.ts"; export * from "./ipc/client.ts"; export * from "./ipc/protocol.ts"; export * from "./ipc/server.ts"; +export * from "./serve.ts"; export * from "./storage.ts"; export * from "./types.ts"; diff --git a/packages/orchestrator/src/serve.ts b/packages/orchestrator/src/serve.ts new file mode 100644 index 00000000..1651086b --- /dev/null +++ b/packages/orchestrator/src/serve.ts @@ -0,0 +1,26 @@ +import { existsSync, unlinkSync } from "node:fs"; +import { getSocketPath } from "./config.ts"; +import { handleIpcRequest } from "./handler.ts"; +import { startIpcServer } from "./ipc/server.ts"; + +export async function serve(): Promise { + const socketPath = getSocketPath(); + const server = await startIpcServer(handleIpcRequest); + + const cleanup = () => { + server.close(); + if (existsSync(socketPath)) { + unlinkSync(socketPath); + } + }; + + process.on("SIGINT", () => { + cleanup(); + process.exit(0); + }); + + process.on("SIGTERM", () => { + cleanup(); + process.exit(0); + }); +}