feat(research): run durable backtests in a bounded worker pool
This commit is contained in:
parent
bbf0dd4453
commit
26b97265c6
15 changed files with 1730 additions and 129 deletions
|
|
@ -1,8 +1,8 @@
|
|||
import { asValue, type AwilixContainer } from 'awilix';
|
||||
import type { ServiceCradle as ServiceDefinitions } from '@stock-bot/di';
|
||||
import { BacktestRunner } from './backtest/BacktestRunner';
|
||||
import type { BacktestExecutionEngine } from './backtest/execution-engine';
|
||||
import { PostgresBacktestRunRepository } from './backtest/PostgresBacktestRunRepository';
|
||||
import { assertResearchConcurrency, ResearchBacktestPool } from './backtest/ResearchBacktestPool';
|
||||
import { UnavailableBacktestRunRepository } from './backtest/run-repository';
|
||||
import { RustBacktestAdapter } from './backtest/RustBacktestAdapter';
|
||||
import type { PaperConfigurationInput, PaperRuntime } from './contracts/paper-runtime';
|
||||
|
|
@ -41,12 +41,15 @@ export async function registerOrchestratorServices(
|
|||
allowSyntheticData?: boolean;
|
||||
syntheticSeed?: number;
|
||||
nativeExecutionTimeoutMs?: number;
|
||||
maxConcurrent?: number;
|
||||
canonicalPaper?: PaperConfigurationInput;
|
||||
} = {}
|
||||
): Promise<void> {
|
||||
const role = options.role === undefined ? 'research-only' : options.role;
|
||||
const useRustEngine = options.useRustEngine === undefined ? true : options.useRustEngine;
|
||||
assertSupportedComposition(role, useRustEngine);
|
||||
const maxConcurrent = options.maxConcurrent === undefined ? 1 : options.maxConcurrent;
|
||||
assertResearchConcurrency(maxConcurrent);
|
||||
if (options.canonicalPaper !== undefined && role !== 'paper') {
|
||||
throw new Error('Canonical paper configuration requires a paper composition');
|
||||
}
|
||||
|
|
@ -100,16 +103,22 @@ export async function registerOrchestratorServices(
|
|||
programs: canonicalPaper.strategies.map(createBuiltinStrategy),
|
||||
})
|
||||
: undefined;
|
||||
const engine = new RustBacktestAdapter(
|
||||
{ logger: services.logger, storage: storageService },
|
||||
{ timeoutMs: options.nativeExecutionTimeoutMs }
|
||||
);
|
||||
const runner = new BacktestRunner({
|
||||
engine,
|
||||
engineName: 'native',
|
||||
repository: services.postgres
|
||||
? new PostgresBacktestRunRepository(services.postgres)
|
||||
: new UnavailableBacktestRunRepository(),
|
||||
const runner = new ResearchBacktestPool({
|
||||
runners: Array.from(
|
||||
{ length: maxConcurrent },
|
||||
() =>
|
||||
new BacktestRunner({
|
||||
engine: new RustBacktestAdapter(
|
||||
{ logger: services.logger, storage: storageService },
|
||||
{ timeoutMs: options.nativeExecutionTimeoutMs }
|
||||
),
|
||||
engineName: 'native',
|
||||
repository: services.postgres
|
||||
? new PostgresBacktestRunRepository(services.postgres)
|
||||
: new UnavailableBacktestRunRepository(),
|
||||
logger: services.logger,
|
||||
})
|
||||
),
|
||||
logger: services.logger,
|
||||
});
|
||||
const analytics = new AnalyticsService(services);
|
||||
|
|
@ -118,7 +127,6 @@ export async function registerOrchestratorServices(
|
|||
Object.assign(custom, {
|
||||
StorageService: storageService,
|
||||
AnalyticsService: analytics,
|
||||
BacktestEngine: engine,
|
||||
BacktestRunner: runner,
|
||||
...(paper ? { CanonicalPaperRuntime: paper } : {}),
|
||||
});
|
||||
|
|
@ -127,15 +135,13 @@ export async function registerOrchestratorServices(
|
|||
StorageService: StorageService;
|
||||
AnalyticsService: AnalyticsService;
|
||||
CanonicalPaperRuntime: PaperRuntime;
|
||||
BacktestEngine: BacktestExecutionEngine;
|
||||
BacktestRunner: BacktestRunner;
|
||||
BacktestRunner: ResearchBacktestPool;
|
||||
orchestratorServices: IServiceContainer;
|
||||
}
|
||||
>;
|
||||
canonicalContainer.register({
|
||||
StorageService: asValue(storageService),
|
||||
AnalyticsService: asValue(analytics),
|
||||
BacktestEngine: asValue(engine),
|
||||
BacktestRunner: asValue(runner),
|
||||
orchestratorServices: asValue(services),
|
||||
...(paper ? { CanonicalPaperRuntime: asValue(paper) } : {}),
|
||||
|
|
@ -147,8 +153,9 @@ export async function registerOrchestratorServices(
|
|||
if (paper && canonicalPaper && options.initializeMode !== false) {
|
||||
await paper.initialize(canonicalPaper);
|
||||
}
|
||||
services.queueManager?.getQueue('orchestrator', { workers: 1, concurrency: 1 });
|
||||
services.queueManager?.getQueue('orchestrator', { workers: 1, concurrency: maxConcurrent });
|
||||
} catch (error) {
|
||||
await runner.shutdown().catch(() => {});
|
||||
await paper?.shutdown().catch(() => {});
|
||||
throw error;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue