fix(orchestrator): own startup and shutdown resources (CL16)
This commit is contained in:
parent
21666242f4
commit
8546fce4a9
8 changed files with 384 additions and 117 deletions
|
|
@ -8,12 +8,31 @@ import {
|
|||
resolvePaperComposition,
|
||||
type OrchestratorRole,
|
||||
} from './container';
|
||||
import { ownOrchestratorRuntime } from './runtime';
|
||||
import type { OrchestratorServices } from './types';
|
||||
|
||||
type OwnedServices = Awaited<ReturnType<ServiceContainerBuilder['buildOwned']>>;
|
||||
|
||||
function buildSharedServices(config: StockAppConfig): Promise<OwnedServices> {
|
||||
return new ServiceContainerBuilder()
|
||||
.withConfig(config)
|
||||
.withOptions({
|
||||
enableMongoDB: config.database?.mongodb?.enabled ?? false,
|
||||
enablePostgres: config.database?.postgres?.enabled ?? false,
|
||||
enableCache: config.database?.dragonfly?.enabled ?? false,
|
||||
enableQueue: config.queue?.enabled ?? false,
|
||||
enableQuestDB: config.database?.questdb?.enabled ?? false,
|
||||
enableBrowser: false,
|
||||
enableProxy: false,
|
||||
})
|
||||
.buildOwned();
|
||||
}
|
||||
|
||||
/** Use the shared builder for connection setup and adapter ownership. */
|
||||
export async function createContainer(
|
||||
config: StockAppConfig,
|
||||
options: { role?: OrchestratorRole } = {}
|
||||
options: { role?: OrchestratorRole } = {},
|
||||
build: (config: StockAppConfig) => Promise<OwnedServices> = buildSharedServices
|
||||
) {
|
||||
const settings = config.services?.orchestrator;
|
||||
if (settings?.defaultMode === 'live') {
|
||||
|
|
@ -43,27 +62,28 @@ export async function createContainer(
|
|||
throw new Error('Canonical paper requires durable PostgreSQL custody');
|
||||
}
|
||||
|
||||
const container = await new ServiceContainerBuilder()
|
||||
.withConfig(config)
|
||||
.withOptions({
|
||||
enableMongoDB: config.database?.mongodb?.enabled ?? false,
|
||||
enablePostgres: config.database?.postgres?.enabled ?? false,
|
||||
enableCache: config.database?.dragonfly?.enabled ?? false,
|
||||
enableQueue: config.queue?.enabled ?? false,
|
||||
enableQuestDB: config.database?.questdb?.enabled ?? false,
|
||||
enableBrowser: false,
|
||||
enableProxy: false,
|
||||
})
|
||||
.build();
|
||||
await registerOrchestratorServices(container, {
|
||||
role,
|
||||
useRustEngine,
|
||||
maxConcurrent,
|
||||
allowSyntheticData: config.services?.orchestrator?.backtesting?.allowSyntheticData ?? false,
|
||||
syntheticSeed: config.services?.orchestrator?.backtesting?.syntheticSeed ?? 42,
|
||||
nativeExecutionTimeoutMs: config.services?.orchestrator?.backtesting?.executionTimeoutMs,
|
||||
canonicalPaper,
|
||||
paperDeploymentHost,
|
||||
});
|
||||
return container.resolve('serviceContainer') as OrchestratorServices;
|
||||
const owned = await build(config);
|
||||
let runtime: ReturnType<typeof ownOrchestratorRuntime> | undefined;
|
||||
try {
|
||||
const services = owned.container.resolve('serviceContainer') as OrchestratorServices;
|
||||
runtime = ownOrchestratorRuntime(services, owned.shutdown);
|
||||
await registerOrchestratorServices(owned.container, {
|
||||
role,
|
||||
useRustEngine,
|
||||
maxConcurrent,
|
||||
allowSyntheticData: config.services?.orchestrator?.backtesting?.allowSyntheticData ?? false,
|
||||
syntheticSeed: config.services?.orchestrator?.backtesting?.syntheticSeed ?? 42,
|
||||
nativeExecutionTimeoutMs: config.services?.orchestrator?.backtesting?.executionTimeoutMs,
|
||||
canonicalPaper,
|
||||
paperDeploymentHost,
|
||||
});
|
||||
return runtime;
|
||||
} catch (error) {
|
||||
try {
|
||||
await (runtime ? runtime.shutdown() : owned.shutdown());
|
||||
} catch (cleanupError) {
|
||||
runtime?.services.logger.error('Orchestrator composition cleanup failed', { cleanupError });
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue