fix: 解決kafka會遺漏commit已處理的meesage的問題

This commit is contained in:
saingchildren 2026-04-30 10:09:12 +08:00
parent 33638bdd36
commit 33606ca9eb

View File

@ -64,8 +64,9 @@ public class KafkaConsumer : BackgroundService
await BulkInsertToDatabaseAsync(buffer, stoppingToken);
var latestOffset = buffer.Last();
consumer.Commit(latestOffset);
var offsets = buffer.GroupBy(msg => msg.TopicPartition)
.Select(g => new TopicPartitionOffset(g.Key, g.Last().Offset + 1)).ToList();
consumer.Commit(offsets);
_logger.LogInformation($"✅ [成功] 已寫入 {buffer.Count} 筆資料至 PostgreSQL。");