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

@@ -25,7 +25,6 @@ namespace Managing.Application.Bots
public override void Start()
{
Task.Run(() => InitWorker(Run));
base.Start();
}

View File

@@ -52,7 +52,7 @@ public class TradingBot : Bot, ITradingBot
IBotService botService,
TradingBotConfig config
)
: base(config.AccountName)
: base(config.Name)
{
ExchangeService = exchangeService;
AccountService = accountService;
@@ -85,17 +85,17 @@ public class TradingBot : Bot, ITradingBot
}
}
public override async void Start()
public override void Start()
{
base.Start();
// Load account synchronously
await LoadAccount();
LoadAccount().GetAwaiter().GetResult();
if (!Config.IsForBacktest)
{
LoadScenario(Config.ScenarioName);
await PreloadCandles();
await CancelAllOrders();
PreloadCandles().GetAwaiter().GetResult();
CancelAllOrders().GetAwaiter().GetResult();
try
{
@@ -109,7 +109,7 @@ public class TradingBot : Bot, ITradingBot
Logger.LogError(ex, ex.Message);
}
await InitWorker(Run);
InitWorker(Run).GetAwaiter().GetResult();
}
// Fee = TradingService.GetFee(Account, IsForBacktest);
@@ -1010,6 +1010,7 @@ public class TradingBot : Bot, ITradingBot
BotTradingBalance = data.BotTradingBalance,
BotType = data.BotType,
CooldownPeriod = data.CooldownPeriod,
Name = data.Name
};
Signals = data.Signals;

View File

@@ -17,4 +17,5 @@ public class TradingBotConfig
public decimal BotTradingBalance { get; set; }
public decimal CooldownPeriod { get; set; } = 1;
public int MaxLossStreak { get; set; } = 0; // 0 means no limit
public string Name { get; set; }
}

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