Example #1
0
        public async Task SavePublishedProviderContents(TemplateMetadataContents templateMetadataContents, TemplateMapping templateMapping, IEnumerable <PublishedProvider> publishedProvidersToUpdate, IPublishedProviderContentsGenerator generator, bool publishAll = false)
        {
            _logger.Information("Saving published provider contents");
            List <Task>   allTasks  = new List <Task>();
            SemaphoreSlim throttler = new SemaphoreSlim(initialCount: _publishingEngineOptions.SavePublishedProviderContentsConcurrencyCount);

            foreach (PublishedProvider provider in publishedProvidersToUpdate)
            {
                await throttler.WaitAsync();

                allTasks.Add(
                    Task.Run(async() =>
                {
                    try
                    {
                        IEnumerable <PublishedProviderVersion> publishedProviderVersions = publishAll ?
                                                                                           await _publishedProviderVersioningService.GetVersions(provider) :
                                                                                           new[] { provider.Current };

                        foreach (PublishedProviderVersion publishedProviderVersion in publishedProviderVersions)
                        {
                            string contents = generator.GenerateContents(publishedProviderVersion, templateMetadataContents, templateMapping);

                            if (string.IsNullOrWhiteSpace(contents))
                            {
                                throw new RetriableException($"Generator failed to generate content for published provider version with id: '{publishedProviderVersion.Id}'");
                            }

                            try
                            {
                                await _publishedProviderVersionService.SavePublishedProviderVersionBody(
                                    publishedProviderVersion.FundingId, contents, publishedProviderVersion.SpecificationId);
                            }
                            catch (Exception ex)
                            {
                                throw new RetriableException(ex.Message);
                            }

                            try
                            {
                                await _publishedProviderIndexerService.IndexPublishedProvider(publishedProviderVersion);
                            }
                            catch (Exception ex)
                            {
                                throw new RetriableException(ex.Message);
                            }
                        }
                    }
                    finally
                    {
                        throttler.Release();
                    }
                }));
            }

            await TaskHelper.WhenAllAndThrow(allTasks.ToArray());
        }
        public void ThrowsExceptionWhenPublishedProviderVersionServiceThrowsException()
        {
            // Arrange
            TemplateMetadataContents            templateMetadataContents           = Substitute.For <TemplateMetadataContents>();
            TemplateMapping                     templateMapping                    = Substitute.For <TemplateMapping>();
            IPublishedProviderContentsGenerator publishedProviderContentsGenerator = Substitute.For <IPublishedProviderContentsGenerator>();

            List <PublishedProvider> publishedProvidersToUpdate = new List <PublishedProvider>();

            PublishedProviderVersion publishedProviderVersion = NewPublishedProviderVersion(providerVersion => providerVersion
                                                                                            .WithProviderId(ProviderVersionProviderId)
                                                                                            .WithFundingPeriodId(ProviderVersionFundingPeriodId)
                                                                                            .WithFundingStreamId(ProviderVersionFundingStreamId)
                                                                                            .WithVersion(ProviderVersionVersion)
                                                                                            .WithSpecificationId(ProviderVersionSpecificationId));

            PublishedProvider publishedProvider = NewPublishedProvider(provider => provider.WithCurrent(publishedProviderVersion));

            publishedProvidersToUpdate.Add(publishedProvider);

            publishedProviderContentsGenerator
            .GenerateContents(publishedProviderVersion, templateMetadataContents, templateMapping)
            .Returns("contents");

            string exceptionMessage = "Exception Message";

            _publishedProviderVersionService
            .SavePublishedProviderVersionBody(publishedProviderVersion.FundingId, Arg.Any <string>(), publishedProviderVersion.SpecificationId)
            .Throws(new Exception(exceptionMessage));

            // Act
            Func <Task> invocation = async() => await _publishedProviderContentPersistanceService.SavePublishedProviderContents(
                templateMetadataContents, templateMapping, publishedProvidersToUpdate, publishedProviderContentsGenerator);

            // Assert
            ThenExceptionShouldBeThrown(exceptionMessage, invocation);
        }