Update Resume bot status
This commit is contained in:
@@ -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<ILiveBotRegistryGrain>(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user