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

@@ -154,7 +154,7 @@ namespace Managing.Application.ManageBot
flippingBotData.Timeframe,
flippingBotData.IsForWatchingOnly,
flippingBotData.BotTradingBalance);
botTask = Task.Run(InitBot((ITradingBot)bot, backupBot));
botTask = Task.Run(() => InitBot((ITradingBot)bot, backupBot));
break;
}
@@ -165,14 +165,12 @@ namespace Managing.Application.ManageBot
}
}
private Action InitBot(ITradingBot bot, BotBackup backupBot)
private void InitBot(ITradingBot bot, BotBackup backupBot)
{
bot.Start();
var user = _userService.GetUser(backupBot.User.Name);
backupBot.User = user;
bot.LoadBackup(backupBot);
return () => { };
bot.Start();
}
public IBot CreateSimpleBot(string botName, Workflow workflow)
@@ -261,7 +259,8 @@ namespace Managing.Application.ManageBot
Timeframe = interval,
IsForWatchingOnly = isForWatchingOnly,
BotTradingBalance = initialTradingBalance,
BotType = BotType.ScalpingBot
BotType = BotType.ScalpingBot,
Name = name
};
return new ScalpingBot(

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