|
|
|
|
@@ -1,6 +1,8 @@
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using Managing.Application.Abstractions.Grains;
|
|
|
|
|
using Managing.Application.Abstractions.Services;
|
|
|
|
|
using Managing.Core;
|
|
|
|
|
using Managing.Domain.Accounts;
|
|
|
|
|
using Managing.Domain.Backtests;
|
|
|
|
|
using Managing.Domain.Bots;
|
|
|
|
|
using Managing.Domain.MoneyManagements;
|
|
|
|
|
@@ -22,7 +24,7 @@ public class BundleBacktestGrain : Grain, IBundleBacktestGrain, IRemindable
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger<BundleBacktestGrain> _logger;
|
|
|
|
|
private readonly IServiceScopeFactory _scopeFactory;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Reminder configuration
|
|
|
|
|
private const string RETRY_REMINDER_NAME = "BundleBacktestRetry";
|
|
|
|
|
private static readonly TimeSpan RETRY_INTERVAL = TimeSpan.FromMinutes(30);
|
|
|
|
|
@@ -70,9 +72,11 @@ public class BundleBacktestGrain : Grain, IBundleBacktestGrain, IRemindable
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Get pending and failed bundle backtest requests for retry capability
|
|
|
|
|
var pendingRequests = await backtester.GetBundleBacktestRequestsByStatusAsync(BundleBacktestRequestStatus.Pending);
|
|
|
|
|
var failedRequests = await backtester.GetBundleBacktestRequestsByStatusAsync(BundleBacktestRequestStatus.Failed);
|
|
|
|
|
|
|
|
|
|
var pendingRequests =
|
|
|
|
|
await backtester.GetBundleBacktestRequestsByStatusAsync(BundleBacktestRequestStatus.Pending);
|
|
|
|
|
var failedRequests =
|
|
|
|
|
await backtester.GetBundleBacktestRequestsByStatusAsync(BundleBacktestRequestStatus.Failed);
|
|
|
|
|
|
|
|
|
|
var allRequests = pendingRequests.Concat(failedRequests);
|
|
|
|
|
return allRequests.FirstOrDefault(r => r.RequestId == bundleRequestId);
|
|
|
|
|
}
|
|
|
|
|
@@ -97,7 +101,8 @@ public class BundleBacktestGrain : Grain, IBundleBacktestGrain, IRemindable
|
|
|
|
|
await backtester.UpdateBundleBacktestRequestAsync(bundleRequest);
|
|
|
|
|
|
|
|
|
|
// Deserialize the backtest requests as strongly-typed objects
|
|
|
|
|
var backtestRequests = JsonSerializer.Deserialize<List<RunBacktestRequest>>(bundleRequest.BacktestRequestsJson);
|
|
|
|
|
var backtestRequests =
|
|
|
|
|
JsonSerializer.Deserialize<List<RunBacktestRequest>>(bundleRequest.BacktestRequestsJson);
|
|
|
|
|
if (backtestRequests == null)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Failed to deserialize backtest requests");
|
|
|
|
|
@@ -131,13 +136,17 @@ public class BundleBacktestGrain : Grain, IBundleBacktestGrain, IRemindable
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Get total count from deserialized requests instead of string splitting
|
|
|
|
|
var backtestRequests = JsonSerializer.Deserialize<List<RunBacktestRequest>>(bundleRequest.BacktestRequestsJson);
|
|
|
|
|
var backtestRequests =
|
|
|
|
|
JsonSerializer.Deserialize<List<RunBacktestRequest>>(bundleRequest.BacktestRequestsJson);
|
|
|
|
|
var totalCount = backtestRequests?.Count ?? 0;
|
|
|
|
|
|
|
|
|
|
// Update current backtest being processed
|
|
|
|
|
bundleRequest.CurrentBacktest = $"Backtest {index + 1} of {totalCount}";
|
|
|
|
|
await backtester.UpdateBundleBacktestRequestAsync(bundleRequest);
|
|
|
|
|
|
|
|
|
|
bundleRequest.User.Accounts = await ServiceScopeHelpers.WithScopedService<IAccountService, List<Account>>(
|
|
|
|
|
_scopeFactory,
|
|
|
|
|
async service => { return (await service.GetAccountsByUserAsync(bundleRequest.User, true)).ToList(); });
|
|
|
|
|
// Run the backtest directly with the strongly-typed request
|
|
|
|
|
var backtestId = await RunSingleBacktest(backtester, runBacktestRequest, bundleRequest, index);
|
|
|
|
|
if (!string.IsNullOrEmpty(backtestId))
|
|
|
|
|
@@ -308,7 +317,7 @@ public class BundleBacktestGrain : Grain, IBundleBacktestGrain, IRemindable
|
|
|
|
|
var message = bundleRequest.FailedBacktests == 0
|
|
|
|
|
? $"✅ Bundle backtest '{bundleRequest.Name}' (ID: {bundleRequest.RequestId}) completed successfully."
|
|
|
|
|
: $"⚠️ Bundle backtest '{bundleRequest.Name}' (ID: {bundleRequest.RequestId}) completed with {bundleRequest.FailedBacktests} failed backtests.";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await messengerService.SendMessage(message, bundleRequest.User.TelegramChannel);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@@ -346,7 +355,8 @@ public class BundleBacktestGrain : Grain, IBundleBacktestGrain, IRemindable
|
|
|
|
|
// Check if bundle is still failed
|
|
|
|
|
if (bundleRequest.Status != BundleBacktestRequestStatus.Failed)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Bundle request {RequestId} is no longer failed (status: {Status}), unregistering reminder",
|
|
|
|
|
_logger.LogInformation(
|
|
|
|
|
"Bundle request {RequestId} is no longer failed (status: {Status}), unregistering reminder",
|
|
|
|
|
bundleRequestId, bundleRequest.Status);
|
|
|
|
|
await UnregisterRetryReminder();
|
|
|
|
|
return;
|
|
|
|
|
@@ -354,7 +364,7 @@ public class BundleBacktestGrain : Grain, IBundleBacktestGrain, IRemindable
|
|
|
|
|
|
|
|
|
|
// Retry the bundle processing
|
|
|
|
|
_logger.LogInformation("Retrying failed bundle request {RequestId}", bundleRequestId);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Reset status to pending for retry
|
|
|
|
|
bundleRequest.Status = BundleBacktestRequestStatus.Pending;
|
|
|
|
|
bundleRequest.ErrorMessage = null;
|
|
|
|
|
@@ -382,7 +392,8 @@ public class BundleBacktestGrain : Grain, IBundleBacktestGrain, IRemindable
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Failed to register retry reminder for bundle request {RequestId}", this.GetPrimaryKey());
|
|
|
|
|
_logger.LogError(ex, "Failed to register retry reminder for bundle request {RequestId}",
|
|
|
|
|
this.GetPrimaryKey());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -397,15 +408,16 @@ public class BundleBacktestGrain : Grain, IBundleBacktestGrain, IRemindable
|
|
|
|
|
if (reminder != null)
|
|
|
|
|
{
|
|
|
|
|
await this.UnregisterReminder(reminder);
|
|
|
|
|
_logger.LogInformation("Unregistered retry reminder for bundle request {RequestId}", this.GetPrimaryKey());
|
|
|
|
|
_logger.LogInformation("Unregistered retry reminder for bundle request {RequestId}",
|
|
|
|
|
this.GetPrimaryKey());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Failed to unregister retry reminder for bundle request {RequestId}", this.GetPrimaryKey());
|
|
|
|
|
_logger.LogError(ex, "Failed to unregister retry reminder for bundle request {RequestId}",
|
|
|
|
|
this.GetPrimaryKey());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|