fix: Kafka consumer poison pill causes infinite retry
This commit is contained in:
+10
-2
@@ -50,6 +50,8 @@ public sealed class NotificationPublisherService : BackgroundService
|
||||
_logger.LogInformation("NotificationPublisherService started — consuming {Topics}",
|
||||
string.Join(", ", _kafkaOptions.Topics.AlertGenerated, _kafkaOptions.Topics.EncounterStatusChanged));
|
||||
|
||||
var guard = new PoisonPillGuard("notification-publisher", _kafkaOptions.MaxPoisonRetries, _logger);
|
||||
|
||||
try
|
||||
{
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
@@ -75,11 +77,17 @@ public sealed class NotificationPublisherService : BackgroundService
|
||||
await HandleEncounterStatusChangedAsync(chan, props, result.Message.Value, stoppingToken);
|
||||
|
||||
consumer.Commit(result);
|
||||
guard.OnSuccess();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "NotificationPublisher failed to process message from {Topic}", result.Topic);
|
||||
// Do not commit — message will be reprocessed after consumer restart.
|
||||
if (guard.ShouldSkip(result, ex))
|
||||
{
|
||||
consumer.Commit(result);
|
||||
continue;
|
||||
}
|
||||
|
||||
_logger.LogError(ex, "NotificationPublisher failed to process message from {Topic} — will retry", result.Topic);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user