Update Resume bot status

This commit is contained in:
2025-09-18 13:23:26 +07:00
parent 3e5d166a70
commit 530dd83daa

View File

@@ -89,26 +89,66 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
private async Task ResumeBotIfRequiredAsync() private async Task ResumeBotIfRequiredAsync()
{ {
// Make the network call to the registry to get the source of truth
var botRegistry = GrainFactory.GetGrain<ILiveBotRegistryGrain>(0); var botRegistry = GrainFactory.GetGrain<ILiveBotRegistryGrain>(0);
var botId = this.GetPrimaryKey(); var botId = this.GetPrimaryKey();
var botStatus = await botRegistry.GetBotStatus(botId); var registryStatus = await botRegistry.GetBotStatus(botId);
_logger.LogInformation("LiveTradingBotGrain {GrainId} activated. Registry status: {Status}", _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. // Bot is running in registry, proceed with resuming (no DB check needed)
await ResumeBotInternalAsync(botStatus); await ResumeBotInternalAsync(registryStatus);
} }
else if (botStatus == BotStatus.Stopped) else if (registryStatus == BotStatus.Stopped)
{ {
// If the status is stopped, ensure the reminder is unregistered // 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(); await UnregisterReminder();
_logger.LogInformation("LiveTradingBotGrain {GrainId} status is stopped, reminder unregistered", botId); _logger.LogInformation("LiveTradingBotGrain {GrainId} status is stopped, reminder unregistered", botId);
} }
} }
}
/// <summary>
/// Gets the bot status from the database (source of truth)
/// </summary>
private async Task<BotStatus> GetDatabaseBotStatus(Guid botId)
{
try
{
var bot = await ServiceScopeHelpers.WithScopedService<IBotService, Bot>(
_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;
}
}
private async Task ResumeBotInternalAsync(BotStatus previousStatus) private async Task ResumeBotInternalAsync(BotStatus previousStatus)
{ {