Fix worker and signal

This commit is contained in:
2025-07-21 23:30:54 +07:00
parent 83ed78a1fa
commit 27c1e9d1ba
13 changed files with 132 additions and 60 deletions

View File

@@ -37,7 +37,7 @@ public class BundleBacktestWorker : BaseWorker<BundleBacktestWorker>
try
{
// Get pending bundle backtest requests
var pendingRequests = _backtester.GetPendingBundleBacktestRequests();
var pendingRequests = _backtester.GetBundleBacktestRequestsByStatus(BundleBacktestRequestStatus.Pending);
foreach (var bundleRequest in pendingRequests)
{
@@ -46,6 +46,8 @@ public class BundleBacktestWorker : BaseWorker<BundleBacktestWorker>
await ProcessBundleRequest(bundleRequest, cancellationToken);
}
await RetryUnfinishedBacktestsInFailedBundles(cancellationToken);
}
catch (Exception ex)
{
@@ -87,7 +89,11 @@ public class BundleBacktestWorker : BaseWorker<BundleBacktestWorker>
_backtester.UpdateBundleBacktestRequest(bundleRequest);
// Run the backtest directly with the strongly-typed request
await RunSingleBacktest(runBacktestRequest, bundleRequest, i, cancellationToken);
var backtestId = await RunSingleBacktest(runBacktestRequest, bundleRequest, i, cancellationToken);
if (!string.IsNullOrEmpty(backtestId))
{
bundleRequest.Results.Add(backtestId);
}
// Update progress
bundleRequest.CompletedBacktests++;
@@ -140,13 +146,14 @@ public class BundleBacktestWorker : BaseWorker<BundleBacktestWorker>
}
// Change RunSingleBacktest to accept RunBacktestRequest directly
private async Task RunSingleBacktest(RunBacktestRequest runBacktestRequest, BundleBacktestRequest bundleRequest,
private async Task<string> RunSingleBacktest(RunBacktestRequest runBacktestRequest,
BundleBacktestRequest bundleRequest,
int index, CancellationToken cancellationToken)
{
if (runBacktestRequest == null || runBacktestRequest.Config == null)
{
_logger.LogError("Invalid RunBacktestRequest in bundle (null config)");
return;
return string.Empty;
}
// Map MoneyManagement
@@ -232,11 +239,48 @@ public class BundleBacktestWorker : BaseWorker<BundleBacktestWorker>
runBacktestRequest.StartDate,
runBacktestRequest.EndDate,
bundleRequest.User, // No user context in worker
runBacktestRequest.Save,
true,
runBacktestRequest.WithCandles,
bundleRequest.RequestId // Use bundleRequestId as requestId for traceability
);
_logger.LogInformation("Processed backtest for bundle request {RequestId}", bundleRequest.RequestId);
// Assume the backtest is created and you have its ID (e.g., backtest.Id)
// Return the backtest ID
return result.Id;
}
private async Task RetryUnfinishedBacktestsInFailedBundles(CancellationToken cancellationToken)
{
var failedBundles = _backtester.GetBundleBacktestRequestsByStatus(BundleBacktestRequestStatus.Failed);
foreach (var failedBundle in failedBundles)
{
if (cancellationToken.IsCancellationRequested)
break;
// Use Results property to determine which backtests need to be retried
var succeededIds = new HashSet<string>(failedBundle.Results ?? new List<string>());
// Deserialize the original requests
var originalRequests =
JsonSerializer
.Deserialize<List<RunBacktestRequest>>(failedBundle.BacktestRequestsJson);
if (originalRequests == null) continue;
for (int i = 0; i < originalRequests.Count; i++)
{
var expectedId = /* logic to compute expected backtest id for this request */ string.Empty;
// If this backtest was not run or did not succeed, re-run it
if (!succeededIds.Contains(expectedId))
{
var backtestId = await RunSingleBacktest(originalRequests[i], failedBundle, i, cancellationToken);
if (!string.IsNullOrEmpty(backtestId))
{
failedBundle.Results?.Add(backtestId);
_backtester.UpdateBundleBacktestRequest(failedBundle);
}
}
}
}
}
}