From ba2fbc976aa33434afcfca2e6c38f55ff57dda50 Mon Sep 17 00:00:00 2001 From: cryptooda Date: Sat, 10 May 2025 00:50:29 +0700 Subject: [PATCH] Fix loading backup --- src/Managing.Application/Bots/SimpleBot.cs | 1 - src/Managing.Application/Bots/TradingBot.cs | 13 ++++--- .../Bots/TradingBotConfig.cs | 1 + .../ManageBot/BotService.cs | 11 +++--- .../ManageBot/LoadBackupBotCommandHandler.cs | 37 +++++++++++++++---- 5 files changed, 42 insertions(+), 21 deletions(-) diff --git a/src/Managing.Application/Bots/SimpleBot.cs b/src/Managing.Application/Bots/SimpleBot.cs index e2e03a6..425a73b 100644 --- a/src/Managing.Application/Bots/SimpleBot.cs +++ b/src/Managing.Application/Bots/SimpleBot.cs @@ -25,7 +25,6 @@ namespace Managing.Application.Bots public override void Start() { Task.Run(() => InitWorker(Run)); - base.Start(); } diff --git a/src/Managing.Application/Bots/TradingBot.cs b/src/Managing.Application/Bots/TradingBot.cs index 8af7289..53fbbb2 100644 --- a/src/Managing.Application/Bots/TradingBot.cs +++ b/src/Managing.Application/Bots/TradingBot.cs @@ -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; diff --git a/src/Managing.Application/Bots/TradingBotConfig.cs b/src/Managing.Application/Bots/TradingBotConfig.cs index 8c6bb95..151ef82 100644 --- a/src/Managing.Application/Bots/TradingBotConfig.cs +++ b/src/Managing.Application/Bots/TradingBotConfig.cs @@ -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; } } \ No newline at end of file diff --git a/src/Managing.Application/ManageBot/BotService.cs b/src/Managing.Application/ManageBot/BotService.cs index e28c6bb..4ccc9d9 100644 --- a/src/Managing.Application/ManageBot/BotService.cs +++ b/src/Managing.Application/ManageBot/BotService.cs @@ -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( diff --git a/src/Managing.Application/ManageBot/LoadBackupBotCommandHandler.cs b/src/Managing.Application/ManageBot/LoadBackupBotCommandHandler.cs index fd88313..133dca0 100644 --- a/src/Managing.Application/ManageBot/LoadBackupBotCommandHandler.cs +++ b/src/Managing.Application/ManageBot/LoadBackupBotCommandHandler.cs @@ -44,19 +44,40 @@ public class LoadBackupBotCommandHandler : IRequestHandler 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