fix: Kafka consumer poison pill causes infinite retry

This commit is contained in:
voltsrage
2026-06-21 17:36:00 +08:00
parent 0d0bba19e6
commit 2d22ff06b6
10 changed files with 175 additions and 10 deletions
@@ -39,6 +39,8 @@ public class SepsisEngineService : BackgroundService
_logger.LogInformation(
"SepsisEngineService started — consumer group: sepsis-engine (qSOFA screening only)");
var guard = new PoisonPillGuard("sepsis-engine", _kafkaOptions.MaxPoisonRetries, _logger);
try
{
while (!stoppingToken.IsCancellationRequested)
@@ -68,6 +70,7 @@ public class SepsisEngineService : BackgroundService
evt.EncounterId, evt.ObservationCode, evt.Value);
consumer.Commit(result);
guard.OnSuccess();
}
catch (OperationCanceledException)
{
@@ -75,8 +78,14 @@ public class SepsisEngineService : BackgroundService
}
catch (Exception ex)
{
if (result is not null && guard.ShouldSkip(result, ex))
{
consumer.Commit(result);
continue;
}
_logger.LogError(ex,
"SepsisEngine failed on topic={Topic} offset={Offset} — not committing",
"SepsisEngine failed on topic={Topic} offset={Offset} — will retry",
result?.Topic, result?.Offset.Value);
await Task.Delay(2000, stoppingToken);
}