public async Task Initialize(TimeSpan timeout)
{
bool shouldCreate = false;
try
{
if (_options.ShouldInitWithLastOffset)
{
CurrentOffset = await _consumer.FetchLastOffset();
_logger.Verbose("KafkaQueueAdapterReceiver - Initialized with latest offset. Offset is {0}", CurrentOffset);
}
else
{
CurrentOffset = await _consumer.FetchOffset(_options.ConsumerGroupName);
_logger.Verbose("KafkaQueueAdapterReceiver - Initialized with ConsumerGroupOffset offset. ConsumerGroup is {0} Offset is {1}", _options.ConsumerGroupName, CurrentOffset);
}
}
catch (KafkaApplicationException ex)
{
// This is the error kafka returns when the consumer group doesn't exist
if (ex.ErrorCode == (int)ErrorResponseCode.UnknownTopicOrPartition)
{
shouldCreate = true;
}
else
{
throw;
}
}
if (shouldCreate)
{
CurrentOffset = await _consumer.FetchLastOffset();
await _consumer.UpdateOrCreateOffset(_options.ConsumerGroupName, CurrentOffset);
_logger.Verbose("KafkaQueueAdapterReceiver - Offset was not found for ConsumerGroup {0}, saved the latest offset for the ConsumerGroup. Offset is {1}", _options.ConsumerGroupName, CurrentOffset);
}
CounterActiveReceivers.Increment();
}