66 lines
2.2 KiB
JavaScript
66 lines
2.2 KiB
JavaScript
import { pathToFileURL } from 'node:url';
|
|
import { createOrchestratorApp } from './app.mjs';
|
|
import { createOrchestratorCheckpoint } from './checkpoint.mjs';
|
|
import { createLangGraphOrchestratorRuntime } from './runtime.mjs';
|
|
|
|
function positivePort(value, fallback = 8093) {
|
|
const parsed = Number(value);
|
|
if (!Number.isInteger(parsed) || parsed < 1 || parsed > 65535) return fallback;
|
|
return parsed;
|
|
}
|
|
|
|
export async function startOrchestratorServer({
|
|
env = process.env,
|
|
logger = console,
|
|
} = {}) {
|
|
const checkpoint = await createOrchestratorCheckpoint({
|
|
mode: env.MEMIND_ORCHESTRATOR_CHECKPOINT_MODE,
|
|
connectionString: env.MEMIND_ORCHESTRATOR_DATABASE_URL,
|
|
schema: env.MEMIND_ORCHESTRATOR_DATABASE_SCHEMA,
|
|
});
|
|
const runtime = createLangGraphOrchestratorRuntime({
|
|
checkpointer: checkpoint.checkpointer,
|
|
checkpointKind: checkpoint.kind,
|
|
durable: checkpoint.durable,
|
|
checkpointProbe: checkpoint.probe,
|
|
});
|
|
const app = createOrchestratorApp({
|
|
runtime,
|
|
serviceToken: env.MEMIND_ORCHESTRATOR_SERVICE_TOKEN,
|
|
});
|
|
const host = String(env.MEMIND_ORCHESTRATOR_HOST ?? '127.0.0.1').trim() || '127.0.0.1';
|
|
const port = positivePort(env.MEMIND_ORCHESTRATOR_PORT, 8093);
|
|
const server = await new Promise((resolve, reject) => {
|
|
const listening = app.listen(port, host, () => resolve(listening));
|
|
listening.once('error', reject);
|
|
});
|
|
logger.log(`[orchestrator] listening on http://${host}:${port} (${checkpoint.kind})`);
|
|
|
|
async function close() {
|
|
await new Promise((resolve, reject) => {
|
|
server.close((error) => (error ? reject(error) : resolve()));
|
|
});
|
|
await checkpoint.close();
|
|
}
|
|
|
|
return { app, server, runtime, checkpoint, close };
|
|
}
|
|
|
|
const isEntrypoint = process.argv[1]
|
|
&& import.meta.url === pathToFileURL(process.argv[1]).href;
|
|
|
|
if (isEntrypoint) {
|
|
const running = await startOrchestratorServer();
|
|
const shutdown = async (signal) => {
|
|
console.log(`[orchestrator] received ${signal}, shutting down`);
|
|
await running.close();
|
|
process.exit(0);
|
|
};
|
|
process.once('SIGINT', () => void shutdown('SIGINT'));
|
|
process.once('SIGTERM', () => void shutdown('SIGTERM'));
|
|
}
|
|
|
|
export const orchestratorServerInternals = {
|
|
positivePort,
|
|
};
|