4.3 KiB
4.3 KiB
Implementation Plan
Phase 1: Database & Domain Setup
- Create
BacktestJobentity inManaging.Domain/Backtests/ - Create
BacktestJobStatusenum (Pending, Running, Completed, Failed) - Create database migration for
BacktestJobstable - Add indexes:
idx_status_priority,idx_bundle_request,idx_assigned_worker - Create
IBacktestJobRepositoryinterface - Implement
BacktestJobRepositorywith advisory lock support
Phase 2: Compute Worker Project
- Refactor
Managing.Workers.Apiproject (or rename toManaging.Compute) - Remove Orleans dependencies completely
- Add project references to shared projects (Application, Domain, Infrastructure)
- Configure DI container with all required services (NO Orleans)
- Create
BacktestComputeWorkerbackground service - Implement job polling logic (every 5 seconds)
- Implement job claiming with PostgreSQL advisory locks
- Implement semaphore-based concurrency control
- Implement progress callback mechanism
- Implement heartbeat mechanism (every 30 seconds)
- Add configuration:
MaxConcurrentBacktests,JobPollIntervalSeconds,WorkerId
Phase 3: API Server Updates
- Update
BacktestControllerto create jobs instead of calling grains directly - Implement
CreateBundleBacktestendpoint (returns immediately) - Implement
GetJobStatusendpoint (polls database for single job) - Implement
GetBundleStatusendpoint (polls database, aggregates job statuses) - Update
Backtester.csto generateBacktestJobentities from bundle variants - Remove all Orleans grain calls for backtests (direct replacement, no feature flags)
- Remove
IGrainFactorydependency fromBacktester.cs
Phase 4: Shared Logic Extraction
- Create
BacktestExecutor.csservice (new file) - Extract backtest execution logic from
BacktestTradingBotGraintoBacktestExecutor - Make backtest logic Orleans-agnostic (no grain dependencies)
- Add progress callback support to execution method
- Ensure candle loading works in compute worker context
- Handle credit debiting/refunding in executor
- Handle user context resolution in executor
Phase 5: Monitoring & Health Checks
- Add health check endpoint to compute worker (
/healthor/healthz) - Add metrics: pending jobs, running jobs, completed/failed counts
- Add stale job detection (reclaim jobs from dead workers, LastHeartbeat > 5 min)
- Add comprehensive logging for job lifecycle events
- Include structured logging: JobId, BundleRequestId, UserId, WorkerId, Duration
Phase 6: SignalR & Notifications
- Inject
IHubContext<BacktestHub>into compute worker or executor - Send SignalR progress updates during job execution
- Update
BacktestJob.ProgressPercentagein database - Update
BundleBacktestRequestprogress when jobs complete - Send completion notifications via SignalR and Telegram
Phase 7: Deployment
- Create Dockerfile for
Managing.Compute(or update existing) - Update
docker-compose.ymlto add compute worker service - Configure environment variables:
MaxConcurrentBacktests,JobPollIntervalSeconds,WorkerId - Set up health check configuration in Docker
- Configure auto-scaling rules for compute workers (min: 1, max: 10)
Phase 9: Testing & Validation
- Unit tests: BacktestJobRepository (advisory locks, job claiming, stale detection)
- Unit tests: BacktestExecutor (core logic, progress callbacks)
- Integration tests: Single backtest job processing
- Integration tests: Bundle backtest with multiple jobs
- Integration tests: Concurrent job processing (multiple workers)
- Integration tests: Job recovery after worker failure
- Integration tests: Priority queue ordering
- Load tests: 100+ concurrent users, 1000+ pending jobs, multiple workers
Phase 8: Cleanup & Removal
- Remove or deprecate
BacktestTradingBotGrain.cs(no longer used) - Remove or deprecate
BundleBacktestGrain.cs(replaced by compute workers) - Remove Orleans grain interfaces for backtests (if not used elsewhere)
- Update
ApiBootstrap.csto remove Orleans backtest grain registrations - Remove Orleans dependencies from
Backtester.cs(keep for other operations) - Update documentation to reflect new architecture