From 530dd83daa80dee2188f24de943f995693dc2812 Mon Sep 17 00:00:00 2001 From: cryptooda Date: Thu, 18 Sep 2025 13:23:26 +0700 Subject: [PATCH] Update Resume bot status --- .../Bots/Grains/LiveTradingBotGrain.cs | 60 +++++++++++++++---- 1 file changed, 50 insertions(+), 10 deletions(-) diff --git a/src/Managing.Application/Bots/Grains/LiveTradingBotGrain.cs b/src/Managing.Application/Bots/Grains/LiveTradingBotGrain.cs index 0d924e3f..4a13b9c5 100644 --- a/src/Managing.Application/Bots/Grains/LiveTradingBotGrain.cs +++ b/src/Managing.Application/Bots/Grains/LiveTradingBotGrain.cs @@ -89,24 +89,64 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable private async Task ResumeBotIfRequiredAsync() { - // Make the network call to the registry to get the source of truth var botRegistry = GrainFactory.GetGrain(0); var botId = this.GetPrimaryKey(); - var botStatus = await botRegistry.GetBotStatus(botId); + var registryStatus = await botRegistry.GetBotStatus(botId); _logger.LogInformation("LiveTradingBotGrain {GrainId} activated. Registry status: {Status}", - botId, botStatus); + botId, registryStatus); - if (botStatus == BotStatus.Running && _tradingBot == null) + if (registryStatus == BotStatus.Running && _tradingBot == null) { - // Now, we can proceed with resuming the bot. - await ResumeBotInternalAsync(botStatus); + // Bot is running in registry, proceed with resuming (no DB check needed) + await ResumeBotInternalAsync(registryStatus); } - else if (botStatus == BotStatus.Stopped) + else if (registryStatus == BotStatus.Stopped) { - // If the status is stopped, ensure the reminder is unregistered - await UnregisterReminder(); - _logger.LogInformation("LiveTradingBotGrain {GrainId} status is stopped, reminder unregistered", botId); + // Registry says stopped, but check database to see if it should be running + var databaseStatus = await GetDatabaseBotStatus(botId); + + _logger.LogInformation("LiveTradingBotGrain {GrainId} registry: {RegistryStatus}, database: {DatabaseStatus}", + botId, registryStatus, databaseStatus); + + if (databaseStatus == BotStatus.Running) + { + // Database says running but registry says stopped - trust database + _logger.LogWarning("Status mismatch detected for bot {BotId}. Registry: {RegistryStatus}, Database: {DatabaseStatus}. Trusting database and updating registry.", + botId, registryStatus, databaseStatus); + + // Update registry to match database (source of truth) + await botRegistry.UpdateBotStatus(botId, databaseStatus); + + // Now proceed with resuming the bot + await ResumeBotInternalAsync(databaseStatus); + } + else + { + // Both registry and database agree it should be stopped + await UnregisterReminder(); + _logger.LogInformation("LiveTradingBotGrain {GrainId} status is stopped, reminder unregistered", botId); + } + } + } + + /// + /// Gets the bot status from the database (source of truth) + /// + private async Task GetDatabaseBotStatus(Guid botId) + { + try + { + var bot = await ServiceScopeHelpers.WithScopedService( + _scopeFactory, + async botService => await botService.GetBotByIdentifier(botId)); + + return bot?.Status ?? BotStatus.Saved; + } + catch (Exception ex) + { + _logger.LogError(ex, "Failed to get database status for bot {BotId}, defaulting to Saved", botId); + return BotStatus.Saved; } }