diff --git a/src/Managing.Api/Controllers/AccountController.cs b/src/Managing.Api/Controllers/AccountController.cs index 21c35130..9338d700 100644 --- a/src/Managing.Api/Controllers/AccountController.cs +++ b/src/Managing.Api/Controllers/AccountController.cs @@ -49,7 +49,7 @@ namespace Managing.Api.Controllers public async Task>> GetAccounts() { var user = await GetUser(); - return Ok(_AccountService.GetAccountsByUser(user, true)); + return Ok(await _AccountService.GetAccountsByUserAsync(user, true)); } /// diff --git a/src/Managing.Application.Abstractions/Services/IAccountService.cs b/src/Managing.Application.Abstractions/Services/IAccountService.cs index 32092a7b..c8171b04 100644 --- a/src/Managing.Application.Abstractions/Services/IAccountService.cs +++ b/src/Managing.Application.Abstractions/Services/IAccountService.cs @@ -8,7 +8,6 @@ public interface IAccountService { Task CreateAccount(User user, Account account); bool DeleteAccount(User user, string name); - IEnumerable GetAccountsByUser(User user, bool hideSecrets = true); Task> GetAccountsByUserAsync(User user, bool hideSecrets = true, bool getBalance = false); Task> GetAccounts(bool hideSecrets, bool getBalance); Task> GetAccountsAsync(bool hideSecrets, bool getBalance); diff --git a/src/Managing.Application/Accounts/AccountService.cs b/src/Managing.Application/Accounts/AccountService.cs index 018c9fe0..f199f4f4 100644 --- a/src/Managing.Application/Accounts/AccountService.cs +++ b/src/Managing.Application/Accounts/AccountService.cs @@ -168,11 +168,6 @@ public class AccountService : IAccountService return accounts; } - public IEnumerable GetAccountsByUser(User user, bool hideSecrets = true) - { - return GetAccountsByUserAsync(user, hideSecrets).Result; - } - public async Task> GetAccountsByUserAsync(User user, bool hideSecrets = true, bool getBalance = false) { diff --git a/src/Managing.Application/Grains/BundleBacktestGrain.cs b/src/Managing.Application/Grains/BundleBacktestGrain.cs index 7f130a84..be94999e 100644 --- a/src/Managing.Application/Grains/BundleBacktestGrain.cs +++ b/src/Managing.Application/Grains/BundleBacktestGrain.cs @@ -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 _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>(bundleRequest.BacktestRequestsJson); + var backtestRequests = + JsonSerializer.Deserialize>(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>(bundleRequest.BacktestRequestsJson); + var backtestRequests = + JsonSerializer.Deserialize>(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>( + _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 - -} +} \ No newline at end of file diff --git a/src/Managing.Application/Users/UserService.cs b/src/Managing.Application/Users/UserService.cs index bc5abd27..21271250 100644 --- a/src/Managing.Application/Users/UserService.cs +++ b/src/Managing.Application/Users/UserService.cs @@ -73,7 +73,7 @@ public class UserService : IUserService throw new Exception("Name not corresponding to user " + name); // User and account found - user.Accounts = _accountService.GetAccountsByUser(user).ToList(); + user.Accounts = (await _accountService.GetAccountsByUserAsync(user)).ToList(); // Check if recoverred address owns the account var account = user.Accounts.FirstOrDefault(a => a.Key.Equals(recoveredAddress)); @@ -184,7 +184,8 @@ public class UserService : IUserService { var agentGrain = _grainFactory.GetGrain(user.Id); 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) {