private async Task ProcessMessageAsync(ConsumeResult <TKey, TMessage> consumeResult, CancellationToken token) { if (consumeResult != null) { var consumeContext = new ConsumeContext <TKey, TMessage>(consumeResult, _consumer, Configuration.ConsumerGroup); _log.Debug($"Begin consuming offset {consumeContext.Offset} on partition {consumeContext.Partition} "); await _pipeline.ExecuteAsync(consumeContext, token).ConfigureAwait(false); _log.Debug($"Finished consuming offset {consumeContext.Offset} on partition {consumeContext.Partition} "); } }
public Task StartAsync(CancellationToken token) { _log.Information($"Starting stream for topic {_topic}"); if (Configuration.TopicCreationEnabled) { _topicCreator.CreateAll(Configuration.TopicConfigurations).Wait(token); } _consumer.Subscribe(_topic); return(Task.Factory.StartNew(async() => { while (!token.IsCancellationRequested) { try { var consumeResult = _consumer.Consume(100); if (consumeResult != null) { var consumeContext = new ConsumeContext <TKey, TMessage>(consumeResult, _consumer, Configuration.ConsumerGroup); _log.Debug($"Begin consuming offset {consumeContext.Offset} on partition {consumeContext.Partition} "); await _pipeline.ExecuteAsync(consumeContext, token).ConfigureAwait(false); _log.Debug($"Finished consuming offset {consumeContext.Offset} on partition {consumeContext.Partition} "); } } catch (ConsumeException ce) when(ce.InnerException is MalformedMessageException && _configuration.ShouldSkipMalformedMessages) { _log.Error(ce, $"A malformed message was encountered on topic { _topic}. Skipping message. Skipping offset {ce.ConsumerRecord.Offset} on partition {ce.ConsumerRecord.Partition.Value}"); _consumer.Commit(); } catch (Exception ex) { _log.Error(ex, $"An error occurred processing messages from topic {_topic}"); _onError(ex); } } }, token, TaskCreationOptions.LongRunning, TaskScheduler.Default).Unwrap()); }