Add more logs for auto-close on owned keys

This commit is contained in:
2025-11-23 22:57:30 +07:00
parent c7c89c903f
commit 6429501b70
2 changed files with 40 additions and 25 deletions

View File

@@ -4,6 +4,7 @@ using Managing.Application.Abstractions.Services;
using Managing.Application.Orleans; using Managing.Application.Orleans;
using Managing.Application.Shared; using Managing.Application.Shared;
using Managing.Core; using Managing.Core;
using Managing.Core.Exceptions;
using Managing.Domain.Accounts; using Managing.Domain.Accounts;
using Managing.Domain.Bots; using Managing.Domain.Bots;
using Managing.Domain.Indicators; using Managing.Domain.Indicators;
@@ -330,14 +331,16 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
_scopeFactory, _scopeFactory,
async tradingService => await tradingService.GetPositionsByInitiatorIdentifierAsync(botId)); async tradingService => await tradingService.GetPositionsByInitiatorIdentifierAsync(botId));
var stillOpenPositions = positions?.Where(p => p.IsOpen() || p.Status.Equals(PositionStatus.New)).ToList() ?? var stillOpenPositions =
positions?.Where(p => p.IsOpen() || p.Status.Equals(PositionStatus.New)).ToList() ??
new List<Position>(); new List<Position>();
if (stillOpenPositions.Any()) if (stillOpenPositions.Any())
{ {
_logger.LogWarning( _logger.LogWarning(
"Bot {GrainId} still has {Count} open positions after closure attempt: {Positions}", "Bot {GrainId} still has {Count} open positions after closure attempt: {Positions}",
botId, stillOpenPositions.Count, string.Join(", ", stillOpenPositions.Select(p => p.Identifier))); botId, stillOpenPositions.Count,
string.Join(", ", stillOpenPositions.Select(p => p.Identifier)));
} }
else else
{ {
@@ -347,7 +350,8 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
catch (Exception ex) catch (Exception ex)
{ {
// Don't fail the stop operation if we can't verify positions // Don't fail the stop operation if we can't verify positions
_logger.LogWarning(ex, "Could not verify position closure status for bot {GrainId}, continuing with stop", _logger.LogWarning(ex,
"Could not verify position closure status for bot {GrainId}, continuing with stop",
this.GetPrimaryKey()); this.GetPrimaryKey());
} }
} }
@@ -403,7 +407,8 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogWarning(ex, "Failed to write state for bot {GrainId}, but bot is stopped", this.GetPrimaryKey()); _logger.LogWarning(ex, "Failed to write state for bot {GrainId}, but bot is stopped",
this.GetPrimaryKey());
} }
try try
@@ -412,7 +417,8 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogWarning(ex, "Failed to save bot status for bot {GrainId}, but bot is stopped", this.GetPrimaryKey()); _logger.LogWarning(ex, "Failed to save bot status for bot {GrainId}, but bot is stopped",
this.GetPrimaryKey());
} }
try try
@@ -421,7 +427,8 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogWarning(ex, "Failed to update bot registry for bot {GrainId}, but bot is stopped", this.GetPrimaryKey()); _logger.LogWarning(ex, "Failed to update bot registry for bot {GrainId}, but bot is stopped",
this.GetPrimaryKey());
} }
_logger.LogInformation("LiveTradingBotGrain {GrainId} stopped successfully", this.GetPrimaryKey()); _logger.LogInformation("LiveTradingBotGrain {GrainId} stopped successfully", this.GetPrimaryKey());
@@ -443,7 +450,7 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
_logger.LogError(ex, "Failed to stop LiveTradingBotGrain {GrainId}", this.GetPrimaryKey()); _logger.LogError(ex, "Failed to stop LiveTradingBotGrain {GrainId}", this.GetPrimaryKey());
// Log Orleans-specific exceptions with additional context for debugging // Log Orleans-specific exceptions with additional context for debugging
if (Managing.Core.Exceptions.OrleansExceptionHelper.IsOrleansException(ex)) if (OrleansExceptionHelper.IsOrleansException(ex))
{ {
_logger.LogWarning( _logger.LogWarning(
"Orleans exception detected during bot stop: {ExceptionType} - {Message}. " + "Orleans exception detected during bot stop: {ExceptionType} - {Message}. " +
@@ -599,6 +606,8 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
// Check if copy trading authorization is still valid // Check if copy trading authorization is still valid
if (_state.State.Config.IsForCopyTrading && _state.State.Config.MasterBotIdentifier.HasValue) if (_state.State.Config.IsForCopyTrading && _state.State.Config.MasterBotIdentifier.HasValue)
{ {
_logger.LogInformation("Checking copy trading authorization for bot {GrainId}", this.GetPrimaryKey());
// Check if copy trading validation should be bypassed (for testing) // Check if copy trading validation should be bypassed (for testing)
var enableValidation = Environment.GetEnvironmentVariable("ENABLE_COPY_TRADING_VALIDATION")? var enableValidation = Environment.GetEnvironmentVariable("ENABLE_COPY_TRADING_VALIDATION")?
.Equals("true", StringComparison.OrdinalIgnoreCase) ?? true; .Equals("true", StringComparison.OrdinalIgnoreCase) ?? true;
@@ -703,6 +712,7 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
// TODO : Turn off the bot if an error occurs // TODO : Turn off the bot if an error occurs
_logger.LogError(ex, "Error during bot execution cycle for LiveTradingBotGrain {GrainId}", _logger.LogError(ex, "Error during bot execution cycle for LiveTradingBotGrain {GrainId}",
this.GetPrimaryKey()); this.GetPrimaryKey());
SentrySdk.CaptureException(ex);
} }
} }
@@ -1273,12 +1283,16 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
// Verify if position was actually closed despite timeout // Verify if position was actually closed despite timeout
try try
{ {
var updatedPositions = await ServiceScopeHelpers.WithScopedService<ITradingService, IEnumerable<Position>>( var updatedPositions = await ServiceScopeHelpers
.WithScopedService<ITradingService, IEnumerable<Position>>(
_scopeFactory, _scopeFactory,
async tradingService => await tradingService.GetPositionsByInitiatorIdentifierAsync(botId)); async tradingService =>
await tradingService.GetPositionsByInitiatorIdentifierAsync(botId));
var updatedPosition = updatedPositions?.FirstOrDefault(p => p.Identifier == position.Identifier); var updatedPosition =
if (updatedPosition != null && !updatedPosition.IsOpen() && updatedPosition.Status != PositionStatus.New) updatedPositions?.FirstOrDefault(p => p.Identifier == position.Identifier);
if (updatedPosition != null && !updatedPosition.IsOpen() &&
updatedPosition.Status != PositionStatus.New)
{ {
_logger.LogInformation( _logger.LogInformation(
"Position {PositionId} was actually closed despite timeout. Status: {Status}", "Position {PositionId} was actually closed despite timeout. Status: {Status}",
@@ -1289,7 +1303,8 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
{ {
_logger.LogWarning( _logger.LogWarning(
"Position {PositionId} may not be closed. Status: {Status}", "Position {PositionId} may not be closed. Status: {Status}",
position.Identifier, updatedPosition != null ? updatedPosition.Status.ToString() : "null"); position.Identifier,
updatedPosition != null ? updatedPosition.Status.ToString() : "null");
// Continue with other positions - we'll verify all positions at the end // Continue with other positions - we'll verify all positions at the end
} }
} }

View File

@@ -254,10 +254,10 @@ public class TradingBotBase : ITradingBot
ExecutionCount++; ExecutionCount++;
Logger.LogInformation( Logger.LogInformation(
"Bot Status {Name} - ServerDate: {ServerDate}, LastCandleDate: {LastCandleDate}, Signals: {SignalCount}, Executions: {ExecutionCount}, Positions: {PositionCount}", "[{CopyTrading}][{AgentName}] Bot Status {Name} - ServerDate: {ServerDate}, LastCandleDate: {LastCandleDate}, Signals: {SignalCount}, Executions: {ExecutionCount}, Positions: {PositionCount}",
Config.Name, DateTime.UtcNow, LastCandle?.Date, Signals.Count, ExecutionCount, Positions.Count); Config.IsForCopyTrading ? "CopyTrading" : "LiveTrading", Account.User.AgentName, Config.Name, DateTime.UtcNow, LastCandle?.Date, Signals.Count, ExecutionCount, Positions.Count);
Logger.LogInformation("[{Name}] Internal Positions : {Position}", Config.Name, Logger.LogInformation("[{AgentName}] Internal Positions : {Position}", Account.User.AgentName,
string.Join(", ", string.Join(", ",
Positions.Values.Select(p => $"{p.SignalIdentifier} - Status: {p.Status}"))); Positions.Values.Select(p => $"{p.SignalIdentifier} - Status: {p.Status}")));
} }