- Remove ~44k LOC of dead code, legacy v1 TS simulator/strategy stack, event-bus, analytics FastAPI prototype, legacy calculation archives, and unused deps (data-ingestion provider API handlers untouched) - Extract @stock-bot/di/http-security and isolate @stock-bot/browser to data-ingestion via ServiceContainerBuilder.withRegistrations() - Harden @stock-bot/mongodb read-only proxies, flatten @stock-bot/postgres with unified master+research migrations, and fix @stock-bot/config EnvLoader, ServiceApplication worker lifecycle, NamespacedCache.clear(), and BatchProcessor error propagation - Flatten workspace root (remove apps/stock/package.json) and collapse orchestrator re-export shims - Pin all Docker image versions, add shm_size/nofile/RAM caps and docker-compose.quant.yml, fix 05-add-visible-column.sql view recreation, and decouple dev/QA Compose project names for side-by-side execution - Update PLATFORM-PLAN, DATA-CONTRACTS (bars_1m PARTITION BY MONTH, Rust bulk ETL, DuckDB limits), MILESTONES, and servers docs
89 lines
3.6 KiB
TypeScript
89 lines
3.6 KiB
TypeScript
import { ServiceContainerBuilder } from '@stock-bot/di';
|
|
import type { StockAppConfig } from '@stock-bot/stock-config';
|
|
import { assertResearchConcurrency } from './backtest/ResearchBacktestPool';
|
|
import { paperConfigurationSchema } from '@stock-bot/stock-config/paper-runtime';
|
|
import {
|
|
assertSupportedComposition,
|
|
registerOrchestratorServices,
|
|
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 } = {},
|
|
build: (config: StockAppConfig) => Promise<OwnedServices> = buildSharedServices
|
|
) {
|
|
const settings = config.services?.orchestrator;
|
|
if (settings?.defaultMode === 'live') {
|
|
throw new Error('Live trading is disabled: no real broker adapter exists');
|
|
}
|
|
const role =
|
|
options.role === undefined
|
|
? settings?.defaultMode === 'paper'
|
|
? 'paper'
|
|
: 'research-only'
|
|
: options.role;
|
|
const configuredEngine = config.services?.orchestrator?.backtesting?.useRustEngine;
|
|
const useRustEngine = configuredEngine === undefined ? true : configuredEngine;
|
|
// Fail before opening connections as well as before registering runtime services.
|
|
assertSupportedComposition(role, useRustEngine);
|
|
const maxConcurrent =
|
|
settings?.backtesting?.maxConcurrent === undefined ? 1 : settings.backtesting.maxConcurrent;
|
|
assertResearchConcurrency(maxConcurrent);
|
|
const { canonicalPaper, paperDeploymentHost } = resolvePaperComposition(
|
|
role,
|
|
settings?.canonicalPaper === undefined
|
|
? undefined
|
|
: paperConfigurationSchema.parse(settings.canonicalPaper),
|
|
settings?.paperDeploymentHost
|
|
);
|
|
if ((canonicalPaper || paperDeploymentHost) && !config.database?.postgres?.enabled) {
|
|
throw new Error('Canonical paper requires durable PostgreSQL custody');
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|