Fix backtest
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
using MediatR;
|
||||
using static Managing.Common.Enums;
|
||||
using Managing.Application.Abstractions;
|
||||
using Managing.Application.Abstractions;
|
||||
using Managing.Core;
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Application.ManageBot;
|
||||
|
||||
@@ -19,67 +19,71 @@ public class LoadBackupBotCommandHandler : IRequestHandler<LoadBackupBotCommand,
|
||||
}
|
||||
|
||||
public Task<string> Handle(LoadBackupBotCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var backupBots = _botService.GetSavedBots().ToList();
|
||||
_logger.LogInformation("Loading {Count} backup bots.", backupBots.Count);
|
||||
|
||||
var result = new Dictionary<string, BotStatus>();
|
||||
bool anyBackupStarted = false;
|
||||
bool anyBotActive = false;
|
||||
|
||||
foreach (var backupBot in backupBots)
|
||||
{
|
||||
try
|
||||
var backupBots = _botService.GetSavedBots().ToList();
|
||||
_logger.LogInformation("Loading {Count} backup bots.", backupBots.Count);
|
||||
|
||||
var result = new Dictionary<string, BotStatus>();
|
||||
bool anyBackupStarted = false;
|
||||
bool anyBotActive = false;
|
||||
|
||||
foreach (var backupBot in backupBots)
|
||||
{
|
||||
var activeBot = _botService.GetActiveBots().FirstOrDefault(b => b.GetName() == backupBot.Name);
|
||||
|
||||
if (activeBot == null)
|
||||
try
|
||||
{
|
||||
_logger.LogInformation("No active instance found for bot {BotName}. Starting backup...", backupBot.Name);
|
||||
_botService.StartBotFromBackup(backupBot);
|
||||
var activeBot = _botService.GetActiveBots().FirstOrDefault(b => b.Identifier == backupBot.Identifier);
|
||||
|
||||
activeBot = _botService.GetActiveBots().FirstOrDefault(b => b.GetName() == backupBot.Name);
|
||||
if (activeBot != null)
|
||||
if (activeBot == null)
|
||||
{
|
||||
result[activeBot.GetName()] = BotStatus.Backup;
|
||||
anyBackupStarted = true;
|
||||
_logger.LogInformation("Backup bot {BotName} started successfully.", backupBot.Name);
|
||||
_logger.LogInformation("No active instance found for bot {Identifier}. Starting backup...",
|
||||
backupBot.Identifier);
|
||||
_botService.StartBotFromBackup(backupBot);
|
||||
|
||||
activeBot = _botService.GetActiveBots().FirstOrDefault(b => b.Identifier == backupBot.Identifier);
|
||||
if (activeBot != null)
|
||||
{
|
||||
result[activeBot.Identifier] = BotStatus.Backup;
|
||||
anyBackupStarted = true;
|
||||
_logger.LogInformation("Backup bot {Identifier} started successfully.", backupBot.Identifier);
|
||||
}
|
||||
else
|
||||
{
|
||||
result[backupBot.Identifier] = BotStatus.Down;
|
||||
_logger.LogWarning("Backup bot {Identifier} failed to start.", backupBot.Identifier);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result[backupBot.Name] = BotStatus.Down;
|
||||
_logger.LogWarning("Backup bot {BotName} failed to start.", backupBot.Name);
|
||||
var status = MiscExtensions.ParseEnum<BotStatus>(activeBot.GetStatus());
|
||||
result[activeBot.Identifier] = status;
|
||||
anyBotActive = true;
|
||||
_logger.LogInformation("Bot {Identifier} is already active with status {Status}.",
|
||||
activeBot.Identifier,
|
||||
status);
|
||||
}
|
||||
}
|
||||
else
|
||||
catch (Exception ex)
|
||||
{
|
||||
var status = MiscExtensions.ParseEnum<BotStatus>(activeBot.GetStatus());
|
||||
result[activeBot.GetName()] = status;
|
||||
anyBotActive = true;
|
||||
_logger.LogInformation("Bot {BotName} is already active with status {Status}.", activeBot.GetName(), status);
|
||||
_logger.LogError(ex, "Error loading bot {Identifier}. Deleting its backup.", backupBot.Identifier);
|
||||
_botService.DeleteBotBackup(backupBot.Identifier);
|
||||
result[backupBot.Identifier] = BotStatus.Down;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error loading bot {BotName}. Deleting its backup.", backupBot.Name);
|
||||
_botService.DeleteBotBackup(backupBot.Name);
|
||||
result[backupBot.Name] = BotStatus.Down;
|
||||
}
|
||||
|
||||
var summary = string.Join(", ", result.Select(b => $"{b.Key}: {b.Value}"));
|
||||
_logger.LogInformation("Bot loading completed. Summary: {Summary}", summary);
|
||||
|
||||
// Determine final status
|
||||
BotStatus finalStatus = anyBackupStarted
|
||||
? BotStatus.Backup
|
||||
: anyBotActive
|
||||
? BotStatus.Up
|
||||
: BotStatus.Down;
|
||||
|
||||
_logger.LogInformation("Final aggregate bot status: {FinalStatus}", finalStatus);
|
||||
|
||||
return Task.FromResult(finalStatus.ToString());
|
||||
}
|
||||
|
||||
var summary = string.Join(", ", result.Select(b => $"{b.Key}: {b.Value}"));
|
||||
_logger.LogInformation("Bot loading completed. Summary: {Summary}", summary);
|
||||
|
||||
// Determine final status
|
||||
BotStatus finalStatus = anyBackupStarted
|
||||
? BotStatus.Backup
|
||||
: anyBotActive ? BotStatus.Up : BotStatus.Down;
|
||||
|
||||
_logger.LogInformation("Final aggregate bot status: {FinalStatus}", finalStatus);
|
||||
|
||||
return Task.FromResult(finalStatus.ToString());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class LoadBackupBotCommand : IRequest<string>
|
||||
|
||||
Reference in New Issue
Block a user