Fix bot management for delete and stop

This commit is contained in:
2025-05-10 13:52:09 +07:00
parent ba2fbc976a
commit b7d5a0b6a7
10 changed files with 41 additions and 28 deletions

View File

@@ -25,6 +25,5 @@ public interface IBotService
Task<string> StopBot(string botName);
Task<bool> DeleteBot(string botName);
Task<string> RestartBot(string botName);
void DeleteBotBackup(string backupBotName);
void ToggleIsForWatchingOnly(string botName);
}

View File

@@ -126,8 +126,6 @@ public class TradingBot : Bot, ITradingBot
else
{
Account = account;
// Set the User property from the account
User = account.User;
}
}

View File

@@ -232,11 +232,6 @@ namespace Managing.Application.ManageBot
return Task.FromResult(BotStatus.Down.ToString());
}
public void DeleteBotBackup(string identifier)
{
_botRepository.DeleteBotBackup(identifier);
}
public void ToggleIsForWatchingOnly(string identifier)
{
if (_botTasks.TryGetValue(identifier, out var botTaskWrapper) &&

View File

@@ -44,39 +44,41 @@ 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);
// 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)
{
activeBot = _botService.GetActiveBots().FirstOrDefault(b => b.Identifier == 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);
_logger.LogInformation("Backup bot {Identifier} started successfully.",
backupBot.Identifier);
break;
}
attempts++;
if (attempts < maxAttempts)
{
Thread.Sleep(1000); // Wait another second before next attempt
}
}
if (activeBot == null)
{
result[backupBot.Identifier] = BotStatus.Down;
_logger.LogWarning("Backup bot {Identifier} failed to start after {MaxAttempts} attempts.",
_logger.LogWarning("Backup bot {Identifier} failed to start after {MaxAttempts} attempts.",
backupBot.Identifier, maxAttempts);
}
}
@@ -93,7 +95,6 @@ public class LoadBackupBotCommandHandler : IRequestHandler<LoadBackupBotCommand,
catch (Exception ex)
{
_logger.LogError(ex, "Error loading bot {Identifier}. Deleting its backup.", backupBot.Identifier);
_botService.DeleteBotBackup(backupBot.Identifier);
result[backupBot.Identifier] = BotStatus.Down;
}
}