Fix fetch and restart bot

This commit is contained in:
2025-10-04 18:31:50 +07:00
parent 15eba0fc3c
commit 343b85dada
3 changed files with 30 additions and 27 deletions

View File

@@ -248,13 +248,21 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
public async Task StopAsync()
{
// Check if bot has open positions in database before allowing stop
var hasOpenPositions = await HasOpenPositionsInDatabaseAsync();
if (hasOpenPositions)
await StopAsyncInternal(false);
}
private async Task StopAsyncInternal(bool isRestarting)
{
// Only check for open positions if this is not part of a restart operation
if (!isRestarting)
{
_logger.LogWarning("Cannot stop LiveTradingBotGrain {GrainId} - bot has open positions in database",
this.GetPrimaryKey());
throw new InvalidOperationException("Cannot stop bot while it has open positions. Please close all positions first.");
var hasOpenPositions = await HasOpenPositionsInDatabaseAsync();
if (hasOpenPositions)
{
_logger.LogWarning("Cannot stop LiveTradingBotGrain {GrainId} - bot has open positions in database",
this.GetPrimaryKey());
throw new InvalidOperationException("Cannot stop bot while it has open positions. Please close all positions first.");
}
}
// The check is now against the registry status
@@ -578,18 +586,10 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
{
_logger.LogInformation("Restarting LiveTradingBotGrain {GrainId}", this.GetPrimaryKey());
// Check if bot has open positions in database before allowing restart
var hasOpenPositions = await HasOpenPositionsInDatabaseAsync();
if (hasOpenPositions)
{
_logger.LogWarning("Cannot restart LiveTradingBotGrain {GrainId} - bot has open positions in database",
this.GetPrimaryKey());
throw new InvalidOperationException("Cannot restart bot while it has open positions. Please close all positions first.");
}
try
{
await StopAsync();
// Use internal stop method that bypasses open position check for restart
await StopAsyncInternal(true);
// Add a small delay to ensure stop operations complete
await Task.Delay(100);