Add reason when stopping bot

This commit is contained in:
2025-10-10 23:31:32 +07:00
parent d9ffadfe2b
commit d71d47f644
5 changed files with 13 additions and 10 deletions

View File

@@ -28,7 +28,7 @@ public interface ILiveTradingBotGrain : IGrainWithGuidKey
Task CreateAsync(TradingBotConfig config, User user); Task CreateAsync(TradingBotConfig config, User user);
Task StartAsync(); Task StartAsync();
Task StopAsync(); Task StopAsync(string reason);
Task<bool> UpdateConfiguration(TradingBotConfig newConfig); Task<bool> UpdateConfiguration(TradingBotConfig newConfig);
Task<Account> GetAccount(); Task<Account> GetAccount();

View File

@@ -20,6 +20,7 @@ namespace Managing.Application.Abstractions
Candle LastCandle { get; set; } Candle LastCandle { get; set; }
Task Run(); Task Run();
Task StopBot(string reason = null);
int GetWinRate(); int GetWinRate();
decimal GetProfitAndLoss(); decimal GetProfitAndLoss();
decimal GetTotalFees(); decimal GetTotalFees();

View File

@@ -265,12 +265,12 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
}); });
} }
public async Task StopAsync() public async Task StopAsync(string reason)
{ {
await StopAsyncInternal(false); await StopAsyncInternal(false, reason);
} }
private async Task StopAsyncInternal(bool isRestarting) private async Task StopAsyncInternal(bool isRestarting, string? reason = null)
{ {
// Only check for open positions if this is not part of a restart operation // Only check for open positions if this is not part of a restart operation
if (!isRestarting) if (!isRestarting)
@@ -318,6 +318,7 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
SyncStateFromBase(); SyncStateFromBase();
await _state.WriteStateAsync(); await _state.WriteStateAsync();
await SaveBotAsync(BotStatus.Stopped); await SaveBotAsync(BotStatus.Stopped);
await _tradingBot?.StopBot(reason)!;
_tradingBot = null; _tradingBot = null;
await UpdateBotRegistryStatus(BotStatus.Stopped); await UpdateBotRegistryStatus(BotStatus.Stopped);
@@ -409,7 +410,7 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
// Check if the bot should stop due to this failure // Check if the bot should stop due to this failure
if (balanceCheckResult.ShouldStopBot) if (balanceCheckResult.ShouldStopBot)
{ {
await StopAsync(); await StopAsync(balanceCheckResult.Message);
return; return;
} }
} }
@@ -647,7 +648,7 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
try try
{ {
// Stop the bot first if it's running // Stop the bot first if it's running
await StopAsync(); await StopAsync("Deleting bot");
// Unregister from the bot registry // Unregister from the bot registry
var botRegistry = GrainFactory.GetGrain<ILiveBotRegistryGrain>(0); var botRegistry = GrainFactory.GetGrain<ILiveBotRegistryGrain>(0);

View File

@@ -1889,9 +1889,10 @@ public class TradingBotBase : ITradingBot
/// <summary> /// <summary>
/// Handles bot stopping and notifies platform summary /// Handles bot stopping and notifies platform summary
/// </summary> /// </summary>
public async Task StopBot() public async Task StopBot(string reason = null)
{ {
await LogInformation($"🛑 Bot Stopped\nBot: `{Config.Name}`\nTicker: `{Config.Ticker}`"); await LogInformation(
$"🛑 Bot Stopped\nBot: `{Config.Name}`\nTicker: `{Config.Ticker}`\nReason: `{reason ?? "No reason provided"}`");
} }
public async Task LogInformation(string message) public async Task LogInformation(string message)

View File

@@ -54,7 +54,7 @@ namespace Managing.Application.ManageBot
try try
{ {
var grain = _grainFactory.GetGrain<ILiveTradingBotGrain>(identifier); var grain = _grainFactory.GetGrain<ILiveTradingBotGrain>(identifier);
await grain.StopAsync(); await grain.StopAsync("User requested stop");
return BotStatus.Stopped; return BotStatus.Stopped;
} }
catch (Exception e) catch (Exception e)
@@ -72,7 +72,7 @@ namespace Managing.Application.ManageBot
{ {
var config = await grain.GetConfiguration(); var config = await grain.GetConfiguration();
var account = await grain.GetAccount(); var account = await grain.GetAccount();
await grain.StopAsync(); await grain.StopAsync("Deleting bot");
await _botRepository.DeleteBot(identifier); await _botRepository.DeleteBot(identifier);
await grain.DeleteAsync(); await grain.DeleteAsync();