Fix unsubscribe + reduce bot update db query

This commit is contained in:
2025-09-15 00:42:24 +07:00
parent b0d2dcc6b9
commit d2dbee9a5f
3 changed files with 27 additions and 5 deletions

View File

@@ -67,12 +67,26 @@ public class CandleStoreGrain : Grain, ICandleStoreGrain, IAsyncObserver<Candle>
public override async Task OnDeactivateAsync(DeactivationReason reason, CancellationToken cancellationToken)
{
// Unsubscribe from the stream
// Unsubscribe from the stream with proper error handling
if (_streamSubscription != null)
{
try
{
await _streamSubscription.UnsubscribeAsync();
_logger.LogDebug("Successfully unsubscribed from stream for grain {GrainKey}", this.GetPrimaryKeyString());
}
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
_logger.LogWarning(ex, "Failed to unsubscribe from stream during deactivation for grain {GrainKey}. This is normal during shutdown.",
this.GetPrimaryKeyString());
}
finally
{
_streamSubscription = null;
}
}
await base.OnDeactivateAsync(reason, cancellationToken);
}

View File

@@ -129,7 +129,7 @@ public class PriceFetcherGrain : Grain, IPriceFetcherGrain, IRemindable
// Get the last candle date from database
var existingCandles = await _candleRepository.GetCandles(exchange, ticker, timeframe,
DateTime.UtcNow.AddDays(-30), DateTime.UtcNow.AddDays(1), 20);
DateTime.UtcNow.AddDays(-30), DateTime.UtcNow.AddDays(1), 1);
var isFirstCall = !existingCandles.Any();

View File

@@ -296,6 +296,8 @@ namespace Managing.Application.ManageBot
var existingBot = await _botRepository.GetBotByIdentifierAsync(bot.Identifier);
// Check if bot already exists in database
await ServiceScopeHelpers.WithScopedService<IBotRepository>(
_scopeFactory,
@@ -311,7 +313,13 @@ namespace Managing.Application.ManageBot
"Created new bot statistics for bot {BotId}: Wins={Wins}, Losses={Losses}, PnL={PnL}, ROI={ROI}%, Volume={Volume}, Fees={Fees}",
bot.Identifier, bot.TradeWins, bot.TradeLosses, bot.Pnl, bot.Roi, bot.Volume, bot.Fees);
}
else
else if (existingBot.Status != bot.Status
|| existingBot.Pnl != Math.Round(bot.Pnl, 8)
|| existingBot.Roi != Math.Round(bot.Roi, 8)
|| existingBot.Volume != Math.Round(bot.Volume, 8)
|| existingBot.Fees != Math.Round(bot.Fees, 8)
|| existingBot.LongPositionCount != bot.LongPositionCount
|| existingBot.ShortPositionCount != bot.ShortPositionCount)
{
_tradingBotLogger.LogInformation("Update bot statistics for bot {BotId}",
bot.Identifier);