Fix bundle backtest grain
This commit is contained in:
@@ -49,7 +49,7 @@ namespace Managing.Api.Controllers
|
|||||||
public async Task<ActionResult<IEnumerable<Account>>> GetAccounts()
|
public async Task<ActionResult<IEnumerable<Account>>> GetAccounts()
|
||||||
{
|
{
|
||||||
var user = await GetUser();
|
var user = await GetUser();
|
||||||
return Ok(_AccountService.GetAccountsByUser(user, true));
|
return Ok(await _AccountService.GetAccountsByUserAsync(user, true));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ public interface IAccountService
|
|||||||
{
|
{
|
||||||
Task<Account> CreateAccount(User user, Account account);
|
Task<Account> CreateAccount(User user, Account account);
|
||||||
bool DeleteAccount(User user, string name);
|
bool DeleteAccount(User user, string name);
|
||||||
IEnumerable<Account> GetAccountsByUser(User user, bool hideSecrets = true);
|
|
||||||
Task<IEnumerable<Account>> GetAccountsByUserAsync(User user, bool hideSecrets = true, bool getBalance = false);
|
Task<IEnumerable<Account>> GetAccountsByUserAsync(User user, bool hideSecrets = true, bool getBalance = false);
|
||||||
Task<IEnumerable<Account>> GetAccounts(bool hideSecrets, bool getBalance);
|
Task<IEnumerable<Account>> GetAccounts(bool hideSecrets, bool getBalance);
|
||||||
Task<IEnumerable<Account>> GetAccountsAsync(bool hideSecrets, bool getBalance);
|
Task<IEnumerable<Account>> GetAccountsAsync(bool hideSecrets, bool getBalance);
|
||||||
|
|||||||
@@ -168,11 +168,6 @@ public class AccountService : IAccountService
|
|||||||
return accounts;
|
return accounts;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Account> GetAccountsByUser(User user, bool hideSecrets = true)
|
|
||||||
{
|
|
||||||
return GetAccountsByUserAsync(user, hideSecrets).Result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<IEnumerable<Account>> GetAccountsByUserAsync(User user, bool hideSecrets = true,
|
public async Task<IEnumerable<Account>> GetAccountsByUserAsync(User user, bool hideSecrets = true,
|
||||||
bool getBalance = false)
|
bool getBalance = false)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using Managing.Application.Abstractions.Grains;
|
using Managing.Application.Abstractions.Grains;
|
||||||
using Managing.Application.Abstractions.Services;
|
using Managing.Application.Abstractions.Services;
|
||||||
|
using Managing.Core;
|
||||||
|
using Managing.Domain.Accounts;
|
||||||
using Managing.Domain.Backtests;
|
using Managing.Domain.Backtests;
|
||||||
using Managing.Domain.Bots;
|
using Managing.Domain.Bots;
|
||||||
using Managing.Domain.MoneyManagements;
|
using Managing.Domain.MoneyManagements;
|
||||||
@@ -22,7 +24,7 @@ public class BundleBacktestGrain : Grain, IBundleBacktestGrain, IRemindable
|
|||||||
{
|
{
|
||||||
private readonly ILogger<BundleBacktestGrain> _logger;
|
private readonly ILogger<BundleBacktestGrain> _logger;
|
||||||
private readonly IServiceScopeFactory _scopeFactory;
|
private readonly IServiceScopeFactory _scopeFactory;
|
||||||
|
|
||||||
// Reminder configuration
|
// Reminder configuration
|
||||||
private const string RETRY_REMINDER_NAME = "BundleBacktestRetry";
|
private const string RETRY_REMINDER_NAME = "BundleBacktestRetry";
|
||||||
private static readonly TimeSpan RETRY_INTERVAL = TimeSpan.FromMinutes(30);
|
private static readonly TimeSpan RETRY_INTERVAL = TimeSpan.FromMinutes(30);
|
||||||
@@ -70,9 +72,11 @@ public class BundleBacktestGrain : Grain, IBundleBacktestGrain, IRemindable
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Get pending and failed bundle backtest requests for retry capability
|
// Get pending and failed bundle backtest requests for retry capability
|
||||||
var pendingRequests = await backtester.GetBundleBacktestRequestsByStatusAsync(BundleBacktestRequestStatus.Pending);
|
var pendingRequests =
|
||||||
var failedRequests = await backtester.GetBundleBacktestRequestsByStatusAsync(BundleBacktestRequestStatus.Failed);
|
await backtester.GetBundleBacktestRequestsByStatusAsync(BundleBacktestRequestStatus.Pending);
|
||||||
|
var failedRequests =
|
||||||
|
await backtester.GetBundleBacktestRequestsByStatusAsync(BundleBacktestRequestStatus.Failed);
|
||||||
|
|
||||||
var allRequests = pendingRequests.Concat(failedRequests);
|
var allRequests = pendingRequests.Concat(failedRequests);
|
||||||
return allRequests.FirstOrDefault(r => r.RequestId == bundleRequestId);
|
return allRequests.FirstOrDefault(r => r.RequestId == bundleRequestId);
|
||||||
}
|
}
|
||||||
@@ -97,7 +101,8 @@ public class BundleBacktestGrain : Grain, IBundleBacktestGrain, IRemindable
|
|||||||
await backtester.UpdateBundleBacktestRequestAsync(bundleRequest);
|
await backtester.UpdateBundleBacktestRequestAsync(bundleRequest);
|
||||||
|
|
||||||
// Deserialize the backtest requests as strongly-typed objects
|
// 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)
|
if (backtestRequests == null)
|
||||||
{
|
{
|
||||||
throw new InvalidOperationException("Failed to deserialize backtest requests");
|
throw new InvalidOperationException("Failed to deserialize backtest requests");
|
||||||
@@ -131,13 +136,17 @@ public class BundleBacktestGrain : Grain, IBundleBacktestGrain, IRemindable
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Get total count from deserialized requests instead of string splitting
|
// 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;
|
var totalCount = backtestRequests?.Count ?? 0;
|
||||||
|
|
||||||
// Update current backtest being processed
|
// Update current backtest being processed
|
||||||
bundleRequest.CurrentBacktest = $"Backtest {index + 1} of {totalCount}";
|
bundleRequest.CurrentBacktest = $"Backtest {index + 1} of {totalCount}";
|
||||||
await backtester.UpdateBundleBacktestRequestAsync(bundleRequest);
|
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
|
// Run the backtest directly with the strongly-typed request
|
||||||
var backtestId = await RunSingleBacktest(backtester, runBacktestRequest, bundleRequest, index);
|
var backtestId = await RunSingleBacktest(backtester, runBacktestRequest, bundleRequest, index);
|
||||||
if (!string.IsNullOrEmpty(backtestId))
|
if (!string.IsNullOrEmpty(backtestId))
|
||||||
@@ -308,7 +317,7 @@ public class BundleBacktestGrain : Grain, IBundleBacktestGrain, IRemindable
|
|||||||
var message = bundleRequest.FailedBacktests == 0
|
var message = bundleRequest.FailedBacktests == 0
|
||||||
? $"✅ Bundle backtest '{bundleRequest.Name}' (ID: {bundleRequest.RequestId}) completed successfully."
|
? $"✅ Bundle backtest '{bundleRequest.Name}' (ID: {bundleRequest.RequestId}) completed successfully."
|
||||||
: $"⚠️ Bundle backtest '{bundleRequest.Name}' (ID: {bundleRequest.RequestId}) completed with {bundleRequest.FailedBacktests} failed backtests.";
|
: $"⚠️ Bundle backtest '{bundleRequest.Name}' (ID: {bundleRequest.RequestId}) completed with {bundleRequest.FailedBacktests} failed backtests.";
|
||||||
|
|
||||||
await messengerService.SendMessage(message, bundleRequest.User.TelegramChannel);
|
await messengerService.SendMessage(message, bundleRequest.User.TelegramChannel);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -346,7 +355,8 @@ public class BundleBacktestGrain : Grain, IBundleBacktestGrain, IRemindable
|
|||||||
// Check if bundle is still failed
|
// Check if bundle is still failed
|
||||||
if (bundleRequest.Status != BundleBacktestRequestStatus.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);
|
bundleRequestId, bundleRequest.Status);
|
||||||
await UnregisterRetryReminder();
|
await UnregisterRetryReminder();
|
||||||
return;
|
return;
|
||||||
@@ -354,7 +364,7 @@ public class BundleBacktestGrain : Grain, IBundleBacktestGrain, IRemindable
|
|||||||
|
|
||||||
// Retry the bundle processing
|
// Retry the bundle processing
|
||||||
_logger.LogInformation("Retrying failed bundle request {RequestId}", bundleRequestId);
|
_logger.LogInformation("Retrying failed bundle request {RequestId}", bundleRequestId);
|
||||||
|
|
||||||
// Reset status to pending for retry
|
// Reset status to pending for retry
|
||||||
bundleRequest.Status = BundleBacktestRequestStatus.Pending;
|
bundleRequest.Status = BundleBacktestRequestStatus.Pending;
|
||||||
bundleRequest.ErrorMessage = null;
|
bundleRequest.ErrorMessage = null;
|
||||||
@@ -382,7 +392,8 @@ public class BundleBacktestGrain : Grain, IBundleBacktestGrain, IRemindable
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
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)
|
if (reminder != null)
|
||||||
{
|
{
|
||||||
await this.UnregisterReminder(reminder);
|
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)
|
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
|
#endregion
|
||||||
|
}
|
||||||
}
|
|
||||||
@@ -73,7 +73,7 @@ public class UserService : IUserService
|
|||||||
throw new Exception("Name not corresponding to user " + name);
|
throw new Exception("Name not corresponding to user " + name);
|
||||||
|
|
||||||
// User and account found
|
// User and account found
|
||||||
user.Accounts = _accountService.GetAccountsByUser(user).ToList();
|
user.Accounts = (await _accountService.GetAccountsByUserAsync(user)).ToList();
|
||||||
|
|
||||||
// Check if recoverred address owns the account
|
// Check if recoverred address owns the account
|
||||||
var account = user.Accounts.FirstOrDefault(a => a.Key.Equals(recoveredAddress));
|
var account = user.Accounts.FirstOrDefault(a => a.Key.Equals(recoveredAddress));
|
||||||
@@ -184,7 +184,8 @@ public class UserService : IUserService
|
|||||||
{
|
{
|
||||||
var agentGrain = _grainFactory.GetGrain<IAgentGrain>(user.Id);
|
var agentGrain = _grainFactory.GetGrain<IAgentGrain>(user.Id);
|
||||||
await agentGrain.UpdateAgentNameAsync(agentName);
|
await agentGrain.UpdateAgentNameAsync(agentName);
|
||||||
_logger.LogInformation("AgentGrain updated for user {UserId} with agent name {AgentName}", user.Id, agentName);
|
_logger.LogInformation("AgentGrain updated for user {UserId} with agent name {AgentName}", user.Id,
|
||||||
|
agentName);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user