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() public override void Start()
{ {
Task.Run(() => InitWorker(Run)); Task.Run(() => InitWorker(Run));
base.Start(); base.Start();
} }

View File

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

View File

@@ -17,4 +17,5 @@ public class TradingBotConfig
public decimal BotTradingBalance { get; set; } public decimal BotTradingBalance { get; set; }
public decimal CooldownPeriod { get; set; } = 1; public decimal CooldownPeriod { get; set; } = 1;
public int MaxLossStreak { get; set; } = 0; // 0 means no limit 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.Timeframe,
flippingBotData.IsForWatchingOnly, flippingBotData.IsForWatchingOnly,
flippingBotData.BotTradingBalance); flippingBotData.BotTradingBalance);
botTask = Task.Run(InitBot((ITradingBot)bot, backupBot)); botTask = Task.Run(() => InitBot((ITradingBot)bot, backupBot));
break; 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); var user = _userService.GetUser(backupBot.User.Name);
backupBot.User = user; backupBot.User = user;
bot.LoadBackup(backupBot); bot.LoadBackup(backupBot);
return () => { }; bot.Start();
} }
public IBot CreateSimpleBot(string botName, Workflow workflow) public IBot CreateSimpleBot(string botName, Workflow workflow)
@@ -261,7 +259,8 @@ namespace Managing.Application.ManageBot
Timeframe = interval, Timeframe = interval,
IsForWatchingOnly = isForWatchingOnly, IsForWatchingOnly = isForWatchingOnly,
BotTradingBalance = initialTradingBalance, BotTradingBalance = initialTradingBalance,
BotType = BotType.ScalpingBot BotType = BotType.ScalpingBot,
Name = name
}; };
return new ScalpingBot( 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...", _logger.LogInformation("No active instance found for bot {Identifier}. Starting backup...",
backupBot.Identifier); backupBot.Identifier);
// Start the bot from backup
_botService.StartBotFromBackup(backupBot); _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) if (activeBot != null)
{ {
result[activeBot.Identifier] = BotStatus.Backup; result[activeBot.Identifier] = BotStatus.Up;
anyBackupStarted = true; anyBackupStarted = true;
_logger.LogInformation("Backup bot {Identifier} started successfully.", backupBot.Identifier); _logger.LogInformation("Backup bot {Identifier} started successfully.", backupBot.Identifier);
break;
} }
else
attempts++;
if (attempts < maxAttempts)
{
Thread.Sleep(1000); // Wait another second before next attempt
}
}
if (activeBot == null)
{ {
result[backupBot.Identifier] = BotStatus.Down; 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 else