private async Task FetchAndProcess(TimeSpan startupDelay, CancellationToken cancellationToken) { var numberRetrievedInLastBatch = 0; if (startupDelay > TimeSpan.Zero) { await Task.Delay(startupDelay).ConfigureAwait(false); } while (!cancellationToken.IsCancellationRequested) { try { var messages = await messageSource .WaitForNextMessageBatch( batchSizeProvider.CalculateNewRecommendedBatchSize(numberRetrievedInLastBatch), cancellationToken) .ConfigureAwait(false); numberRetrievedInLastBatch = messages?.Count ?? 0; await pipeline .Process(messages, cancellationToken) .ConfigureAwait(false); } catch (TaskCanceledException) { break; } catch (Exception exception) { // Cannot stop the loop as a subscriber would silently just fail. Need to log and try again. logger.LogError(exception, "Unhandled exception when checking for messages."); } } }