Fix fetch and restart bot
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user