Fix stop/restart

This commit is contained in:
2025-07-10 12:53:59 +07:00
parent 0c1184a22d
commit 14b3a3c39a
9 changed files with 164 additions and 271 deletions

View File

@@ -20,7 +20,7 @@ namespace Managing.Application.Abstractions
List<Position> Positions { get; set; }
Dictionary<DateTime, decimal> WalletBalances { get; set; }
Dictionary<IndicatorType, IndicatorsResultBase> IndicatorsValues { get; set; }
DateTime StartupTime { get; set; }
DateTime StartupTime { get; }
DateTime CreateDate { get; }
DateTime PreloadSince { get; set; }
int PreloadedCandlesCount { get; set; }

View File

@@ -116,14 +116,15 @@ public class TradingBot : Bot, ITradingBot
// Send startup message only for fresh starts (not reboots)
// Consider it a reboot if the bot was created more than 5 minutes ago
var timeSinceCreation = DateTime.UtcNow - CreateDate;
var isReboot = timeSinceCreation.TotalMinutes > 5;
var isReboot = timeSinceCreation.TotalMinutes > 3;
StartupTime = DateTime.UtcNow;
if (!isReboot)
{
try
{
StartupTime = DateTime.UtcNow;
var indicatorNames = Indicators.Select(i => i.Type.ToString()).ToList();
var startupMessage = $"🚀 **Bot Started Successfully!**\n\n" +
$"📊 **Trading Setup:**\n" +
@@ -157,6 +158,7 @@ public class TradingBot : Bot, ITradingBot
}
}
InitWorker(Run).GetAwaiter().GetResult();
}
@@ -1458,6 +1460,7 @@ public class TradingBot : Bot, ITradingBot
Identifier = backup.Identifier;
User = backup.User;
Status = backup.LastStatus;
StartupTime = data.StartupTime;
}
/// <summary>

View File

@@ -174,7 +174,18 @@ namespace Managing.Application.ManageBot
bot.User = user;
// Config is already set correctly from backup data, so we only need to restore signals, positions, etc.
bot.LoadBackup(backupBot);
bot.Start();
// Only start the bot if the backup status is Up
if (backupBot.LastStatus == BotStatus.Up)
{
// Start the bot asynchronously without waiting for completion
_ = Task.Run(() => bot.Start());
}
else
{
// Keep the bot in Down status if it was originally Down
bot.Stop();
}
}
public IBot CreateSimpleBot(string botName, Workflow workflow)
@@ -246,12 +257,15 @@ namespace Managing.Application.ManageBot
{
// Stop the bot first to ensure clean state
bot.Stop();
// Small delay to ensure stop is complete
await Task.Delay(100);
// Restart the bot (this will update StartupTime)
bot.Restart();
// Start the bot asynchronously without waiting for completion
_ = Task.Run(() => bot.Start());
var restartMessage = $"🔄 **Bot Restarted**\n\n" +
$"🎯 **Agent:** {bot.User.AgentName}\n" +

View File

@@ -1,13 +1,16 @@
using MediatR;
using static Managing.Common.Enums;
namespace Managing.Application.ManageBot.Commands
{
public class ToggleIsForWatchingCommand : IRequest<string>
public class RestartBotCommand : IRequest<string>
{
public string Name { get; }
public BotType BotType { get; }
public ToggleIsForWatchingCommand(string name)
public RestartBotCommand(BotType botType, string name)
{
BotType = botType;
Name = name;
}
}

View File

@@ -1,16 +1,13 @@
using MediatR;
using static Managing.Common.Enums;
namespace Managing.Application.ManageBot.Commands
{
public class RestartBotCommand : IRequest<string>
public class ToggleIsForWatchingCommand : IRequest<string>
{
public string Name { get; }
public BotType BotType { get; }
public RestartBotCommand(BotType botType, string name)
public ToggleIsForWatchingCommand(string name)
{
BotType = botType;
Name = name;
}
}

View File

@@ -31,13 +31,6 @@ public class LoadBackupBotCommandHandler : IRequestHandler<LoadBackupBotCommand,
{
try
{
if (backupBot.LastStatus == BotStatus.Down)
{
_logger.LogInformation("Skipping backup bot {Identifier} as it is marked as Down.",
backupBot.Identifier);
continue;
}
var activeBot = _botService.GetActiveBots().FirstOrDefault(b => b.Identifier == backupBot.Identifier);
if (activeBot == null)
@@ -61,10 +54,20 @@ public class LoadBackupBotCommandHandler : IRequestHandler<LoadBackupBotCommand,
.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);
// Check if the bot was originally Down
if (backupBot.LastStatus == BotStatus.Down)
{
result[activeBot.Identifier] = BotStatus.Down;
_logger.LogInformation("Backup bot {Identifier} loaded but kept in Down status as it was originally Down.",
backupBot.Identifier);
}
else
{
result[activeBot.Identifier] = BotStatus.Up;
anyBackupStarted = true;
_logger.LogInformation("Backup bot {Identifier} started successfully.",
backupBot.Identifier);
}
break;
}