Add yield for orleans + 1min to 2h timeout for grain message + more exception send to sentry

This commit is contained in:
2025-11-07 12:40:24 +07:00
parent bc4c4c7684
commit 2dc34f07d8
3 changed files with 54 additions and 16 deletions

View File

@@ -82,6 +82,10 @@ public class BacktestTradingBotGrain : Grain, IBacktestTradingBotGrain
var initialBalance = config.BotTradingBalance;
var fixedCandles = new HashSet<Candle>();
var lastYieldTime = DateTime.UtcNow;
const int yieldIntervalMs = 5000; // Yield control every 5 seconds to prevent timeout
const int candlesPerBatch = 100; // Process in batches to allow Orleans to check for cancellation
// Process all candles following the exact pattern from GetBacktestingResult
foreach (var candle in candles)
{
@@ -94,6 +98,16 @@ public class BacktestTradingBotGrain : Grain, IBacktestTradingBotGrain
currentCandle++;
// Yield control periodically to prevent Orleans from thinking the grain is stuck
// This helps prevent timeout issues during long-running backtests
var timeSinceLastYield = (DateTime.UtcNow - lastYieldTime).TotalMilliseconds;
if (timeSinceLastYield >= yieldIntervalMs || currentCandle % candlesPerBatch == 0)
{
// Yield control back to Orleans scheduler
await Task.Yield();
lastYieldTime = DateTime.UtcNow;
}
// Log progress every 10%
var currentPercentage = (currentCandle * 100) / totalCandles;
if (currentPercentage >= lastLoggedPercentage + 10)