Fix loading backup

This commit is contained in:
2025-05-10 00:50:29 +07:00
parent 7c38c27b4a
commit ba2fbc976a
5 changed files with 42 additions and 21 deletions

View File

@@ -44,19 +44,40 @@ public class LoadBackupBotCommandHandler : IRequestHandler<LoadBackupBotCommand,
{
_logger.LogInformation("No active instance found for bot {Identifier}. Starting backup...",
backupBot.Identifier);
// Start the bot from backup
_botService.StartBotFromBackup(backupBot);
activeBot = _botService.GetActiveBots().FirstOrDefault(b => b.Identifier == backupBot.Identifier);
if (activeBot != null)
// Wait a short time to allow the bot to initialize
Thread.Sleep(1000);
// Try to get the active bot multiple times to ensure it's properly started
int attempts = 0;
const int maxAttempts = 5;
while (attempts < maxAttempts)
{
result[activeBot.Identifier] = BotStatus.Backup;
anyBackupStarted = true;
_logger.LogInformation("Backup bot {Identifier} started successfully.", backupBot.Identifier);
activeBot = _botService.GetActiveBots().FirstOrDefault(b => b.Identifier == backupBot.Identifier);
if (activeBot != null)
{
result[activeBot.Identifier] = BotStatus.Up;
anyBackupStarted = true;
_logger.LogInformation("Backup bot {Identifier} started successfully.", backupBot.Identifier);
break;
}
attempts++;
if (attempts < maxAttempts)
{
Thread.Sleep(1000); // Wait another second before next attempt
}
}
else
if (activeBot == null)
{
result[backupBot.Identifier] = BotStatus.Down;
_logger.LogWarning("Backup bot {Identifier} failed to start.", backupBot.Identifier);
_logger.LogWarning("Backup bot {Identifier} failed to start after {MaxAttempts} attempts.",
backupBot.Identifier, maxAttempts);
}
}
else