private async Task ProfileBatchRequests(CancellationToken token,
                                                dynamic context,
                                                IEnumerable <BatchProfilingRequestModel> batchProfilingRequests)
        {
            IBatchProfilingContext batchProfileRequestContext = (IBatchProfilingContext)context;

            foreach (BatchProfilingRequestModel request in batchProfilingRequests)
            {
                ValidatedApiResponse <IEnumerable <BatchProfilingResponseModel> > response = await _profilingResilience.ExecuteAsync(()
                                                                                                                                     => _profiling.GetBatchProfilePeriods(request));

                IEnumerable <BatchProfilingResponseModel> batchProfilingResponseModels = response?.Content;

                if (batchProfilingResponseModels == null)
                {
                    throw new NonRetriableException(
                              $"Unable to complete batch profiling. Missing results from profiling service for {request.FundingStreamId} {request.FundingPeriodId} {request.FundingLineCode} Provider Type={request.ProviderType}, Provider Sub Type={request.ProviderSubType}");
                }

                foreach (BatchProfilingResponseModel batchProfilingResponse in batchProfilingResponseModels)
                {
                    batchProfileRequestContext.ReconcileBatchProfilingResponse(batchProfilingResponse);
                }
            }
        }
        private Task <(bool isComplete, IEnumerable <BatchProfilingRequestModel>)> ProduceBatchProfileRequests(CancellationToken token,
                                                                                                               dynamic context)
        {
            IBatchProfilingContext batchProfileRequestContext = (IBatchProfilingContext)context;

            while (batchProfileRequestContext.HasPages)
            {
                return(Task.FromResult((false, batchProfileRequestContext.NextPage().AsEnumerable())));
            }

            return(Task.FromResult((true, ArraySegment <BatchProfilingRequestModel> .Empty.AsEnumerable())));
        }
        public async Task ProfileBatches(IBatchProfilingContext batchProfilingContext)
        {
            Guard.ArgumentNotNull(batchProfilingContext, nameof(batchProfilingContext));

            batchProfilingContext.InitialiseItems(1, _options.BatchSize);

            IProducerConsumer producerConsumer = _producerConsumerFactory.CreateProducerConsumer(ProduceBatchProfileRequests,
                                                                                                 ProfileBatchRequests,
                                                                                                 10,
                                                                                                 _options.ConsumerCount,
                                                                                                 _logger);

            await producerConsumer.Run(batchProfilingContext);
        }