fix: Kafka consumer poison pill causes infinite retry
This commit is contained in:
@@ -54,6 +54,8 @@ public class EsIndexerService : BackgroundService
|
||||
|
||||
_logger.LogInformation("EsIndexerService started. Subscribed to 5 topics.");
|
||||
|
||||
var guard = new PoisonPillGuard("es-indexer", _kafkaOptions.MaxPoisonRetries, _logger);
|
||||
|
||||
try
|
||||
{
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
@@ -64,6 +66,7 @@ public class EsIndexerService : BackgroundService
|
||||
result = consumer.Consume(stoppingToken);
|
||||
await DispatchAsync(result.Topic, result.Message.Value, stoppingToken);
|
||||
consumer.Commit(result);
|
||||
guard.OnSuccess();
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
@@ -71,10 +74,15 @@ public class EsIndexerService : BackgroundService
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (result is not null && guard.ShouldSkip(result, ex))
|
||||
{
|
||||
consumer.Commit(result);
|
||||
continue;
|
||||
}
|
||||
|
||||
_logger.LogError(ex,
|
||||
"EsIndexer failed processing topic={Topic} offset={Offset} — not committing",
|
||||
"EsIndexer failed processing topic={Topic} offset={Offset} — will retry",
|
||||
result?.Topic, result?.Offset.Value);
|
||||
// Do not commit: message will be redelivered on restart
|
||||
await Task.Delay(1000, stoppingToken);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user