Improve CandleStore grain deactivating

This commit is contained in:
2025-10-10 22:09:30 +07:00
parent 82f8057ed1
commit bdb254809e
4 changed files with 58 additions and 11 deletions

View File

@@ -1802,7 +1802,8 @@ public class TradingBotBase : ITradingBot
}
catch (Exception ex)
{
await LogWarning($"Failed to update position status for signal {signalIdentifier}: {ex.Message}");
await LogWarning($"Failed to update position status for signal {signalIdentifier}: {ex.Message} {ex.StackTrace}");
SentrySdk.CaptureException(ex);
}
}
@@ -1911,7 +1912,6 @@ public class TradingBotBase : ITradingBot
return;
message = $"[{Config.Name}] {message}";
SentrySdk.CaptureException(new Exception(message));
try
{

View File

@@ -70,20 +70,47 @@ public class CandleStoreGrain : Grain, ICandleStoreGrain, IAsyncObserver<Candle>
public override async Task OnDeactivateAsync(DeactivationReason reason, CancellationToken cancellationToken)
{
// Unsubscribe from the stream with proper error handling
var grainKey = this.GetPrimaryKeyString();
_logger.LogInformation("CandleStoreGrain deactivating for key: {GrainKey}. Reason: {Reason}",
grainKey, reason.Description);
// Unsubscribe from the stream with proper error handling and timeout
if (_streamSubscription != null)
{
try
{
// Use a timeout to prevent hanging during shutdown
using var timeoutCts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
using var combinedCts = CancellationTokenSource.CreateLinkedTokenSource(
cancellationToken, timeoutCts.Token);
await _streamSubscription.UnsubscribeAsync();
_logger.LogDebug("Successfully unsubscribed from stream for grain {GrainKey}", this.GetPrimaryKeyString());
_logger.LogDebug("Successfully unsubscribed from stream for grain {GrainKey}", grainKey);
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
// Expected during shutdown - don't log as error
_logger.LogDebug("Stream unsubscription cancelled during shutdown for grain {GrainKey}", grainKey);
}
catch (TaskCanceledException)
{
// Expected during shutdown when pub-sub rendezvous grain is deactivated
_logger.LogDebug("Stream unsubscription timed out during shutdown for grain {GrainKey}", grainKey);
}
catch (Exception ex) when (
ex.GetType().Name == "OrleansMessageRejectionException" &&
(ex.Message.Contains("Forwarding failed") ||
ex.Message.Contains("Unable to create local activation")))
{
// Expected during shutdown when Orleans infrastructure is shutting down
_logger.LogDebug("Stream unsubscription failed due to Orleans shutdown for grain {GrainKey}: {Message}",
grainKey, ex.Message);
}
catch (Exception ex)
{
// Log the error but don't throw - this is common during shutdown when
// the pub-sub rendezvous grain may already be deactivated
// Log other unexpected errors but don't throw - this is common during shutdown
_logger.LogWarning(ex, "Failed to unsubscribe from stream during deactivation for grain {GrainKey}. This is normal during shutdown.",
this.GetPrimaryKeyString());
grainKey);
}
finally
{