Add cancellation token support to backtest execution and update progress handling

This commit is contained in:
2025-11-13 18:05:55 +07:00
parent 17d904c445
commit 1f7d914625
4 changed files with 16 additions and 15 deletions

View File

@@ -267,18 +267,9 @@ public class BacktestComputeWorker : BackgroundService
_jobProgressTrackers.TryAdd(job.Id, progressTracker);
// Progress callback that only updates in-memory progress (non-blocking)
// Timeout is now enforced via CancellationToken, not by throwing in callback
Func<int, Task> progressCallback = (percentage) =>
{
// Check if job has been running too long
var elapsed = DateTime.UtcNow - jobStartTime;
if (elapsed.TotalMinutes > _options.JobTimeoutMinutes)
{
_logger.LogWarning(
"Job {JobId} has been running for {ElapsedMinutes} minutes, exceeding timeout of {TimeoutMinutes} minutes",
job.Id, elapsed.TotalMinutes, _options.JobTimeoutMinutes);
throw new TimeoutException($"Job exceeded timeout of {_options.JobTimeoutMinutes} minutes");
}
// Update progress in memory only - persistence happens in background
progressTracker.UpdateProgress(percentage);
@@ -301,7 +292,8 @@ public class BacktestComputeWorker : BackgroundService
requestId: job.RequestId,
bundleRequestId: job.BundleRequestId,
metadata: null,
progressCallback: progressCallback);
progressCallback: progressCallback,
cancellationToken: linkedCts.Token);
}
catch (OperationCanceledException) when (timeoutCts.Token.IsCancellationRequested && !cancellationToken.IsCancellationRequested)
{