Ejemplo n.º 1
0
        public PublishedFundingService(IPublishedFundingDataService publishedFundingDataService,
                                       IPublishingResiliencePolicies publishingResiliencePolicies,
                                       IPoliciesService policiesService,
                                       IOrganisationGroupGenerator organisationGroupGenerator,
                                       IPublishedFundingChangeDetectorService publishedFundingChangeDetectorService,
                                       IPublishedFundingDateService publishedFundingDateService,
                                       IMapper mapper,
                                       ILogger logger)
        {
            Guard.ArgumentNotNull(publishedFundingDataService, nameof(publishedFundingDataService));
            Guard.ArgumentNotNull(policiesService, nameof(policiesService));
            Guard.ArgumentNotNull(publishingResiliencePolicies.PublishedFundingRepository, nameof(publishingResiliencePolicies.PublishedFundingRepository));
            Guard.ArgumentNotNull(publishedFundingChangeDetectorService, nameof(publishedFundingChangeDetectorService));
            Guard.ArgumentNotNull(publishedFundingDateService, nameof(publishedFundingDateService));
            Guard.ArgumentNotNull(logger, nameof(logger));
            Guard.ArgumentNotNull(mapper, nameof(mapper));

            _publishedFundingDataService           = publishedFundingDataService;
            _publishingResiliencePolicy            = publishingResiliencePolicies.PublishedFundingRepository;
            _policiesService                       = policiesService;
            _organisationGroupGenerator            = organisationGroupGenerator;
            _publishedFundingChangeDetectorService = publishedFundingChangeDetectorService;
            _publishedFundingDateService           = publishedFundingDateService;
            _logger = logger;
            _mapper = mapper;
        }
        public ProviderVersionUpdateCheckService(
            IPoliciesApiClient policiesApiClient,
            ILogger logger,
            IProvidersResiliencePolicies resiliencePolicies,
            IProviderVersionsMetadataRepository providerVersionMetadata,
            IFundingDataZoneApiClient fundingDataZoneApiClient,
            ISpecificationsApiClient specificationsApiClient,
            IMapper mapper)
        {
            Guard.ArgumentNotNull(policiesApiClient, nameof(policiesApiClient));
            Guard.ArgumentNotNull(logger, nameof(logger));
            Guard.ArgumentNotNull(resiliencePolicies, nameof(resiliencePolicies));
            Guard.ArgumentNotNull(resiliencePolicies.PoliciesApiClient, nameof(resiliencePolicies.PoliciesApiClient));
            Guard.ArgumentNotNull(resiliencePolicies.ProviderVersionMetadataRepository, nameof(resiliencePolicies.ProviderVersionMetadataRepository));
            Guard.ArgumentNotNull(resiliencePolicies.FundingDataZoneApiClient, nameof(resiliencePolicies.FundingDataZoneApiClient));
            Guard.ArgumentNotNull(resiliencePolicies.SpecificationsApiClient, nameof(resiliencePolicies.SpecificationsApiClient));
            Guard.ArgumentNotNull(providerVersionMetadata, nameof(providerVersionMetadata));
            Guard.ArgumentNotNull(fundingDataZoneApiClient, nameof(fundingDataZoneApiClient));
            Guard.ArgumentNotNull(specificationsApiClient, nameof(specificationsApiClient));
            Guard.ArgumentNotNull(mapper, nameof(mapper));

            _policiesApiClient        = policiesApiClient;
            _providerVersionMetadata  = providerVersionMetadata;
            _fundingDataZoneApiClient = fundingDataZoneApiClient;
            _specificationsApiClient  = specificationsApiClient;
            _mapper = mapper;
            _logger = logger;
            _policiesApiClientPolicy        = resiliencePolicies.PoliciesApiClient;
            _providerVersionMetadataPolicy  = resiliencePolicies.ProviderVersionMetadataRepository;
            _fundingDataZoneApiClientPolicy = resiliencePolicies.FundingDataZoneApiClient;
            _specificationsApiClientPolicy  = resiliencePolicies.SpecificationsApiClient;
        }
        public JobManagementService(
            IJobRepository jobRepository,
            INotificationService notificationService,
            IJobDefinitionsService jobDefinitionsService,
            IJobsResiliencePolicies resiliencePolicies,
            ILogger logger,
            IValidator <CreateJobValidationModel> createJobValidator,
            IMessengerService messengerService,
            ICacheProvider cacheProvider)
        {
            Guard.ArgumentNotNull(jobRepository, nameof(jobRepository));
            Guard.ArgumentNotNull(notificationService, nameof(notificationService));
            Guard.ArgumentNotNull(jobDefinitionsService, nameof(jobDefinitionsService));
            Guard.ArgumentNotNull(logger, nameof(logger));
            Guard.ArgumentNotNull(createJobValidator, nameof(createJobValidator));
            Guard.ArgumentNotNull(messengerService, nameof(messengerService));
            Guard.ArgumentNotNull(resiliencePolicies?.JobRepository, nameof(resiliencePolicies.JobRepository));
            Guard.ArgumentNotNull(resiliencePolicies?.JobDefinitionsRepository, nameof(resiliencePolicies.JobDefinitionsRepository));
            Guard.ArgumentNotNull(resiliencePolicies?.CacheProviderPolicy, nameof(resiliencePolicies.CacheProviderPolicy));
            Guard.ArgumentNotNull(cacheProvider, nameof(cacheProvider));

            _jobRepository         = jobRepository;
            _notificationService   = notificationService;
            _jobDefinitionsService = jobDefinitionsService;
            _jobsRepositoryPolicy  = resiliencePolicies.JobRepository;
            _logger              = logger;
            _createJobValidator  = createJobValidator;
            _messengerService    = messengerService;
            _cacheProvider       = cacheProvider;
            _cacheProviderPolicy = resiliencePolicies.CacheProviderPolicy;
        }
Ejemplo n.º 4
0
        private Task <HttpResponseMessage> DoPostAsync(HttpMethod method, string url, Dictionary <string, string> form, string authorizationToken = null, string requestId = null, string authorizationMethod = "Bearer")
        {
            if (method != HttpMethod.Post && method != HttpMethod.Put)
            {
                throw new ArgumentException("Method value must be either post or put", nameof(method));
            }
            var origin = GetOriginFromUrl(url);

            AsyncPolicy a = Policy.Handle <HttpRequestException>().RetryAsync();

            return(HttpInvoker(origin, async() =>
            {
                var requestMasssge = new HttpRequestMessage(method, url);
                SetAuthorizationHeader(requestMasssge);

                requestMasssge.Content = new FormUrlEncodedContent(form);

                if (authorizationToken != null)
                {
                    requestMasssge.Headers.Authorization = new AuthenticationHeaderValue(authorizationMethod, authorizationToken);
                }
                if (requestId != null)
                {
                    requestMasssge.Headers.Add("x-requestid", requestId);
                }
                var response = await _httpClient.SendAsync(requestMasssge);

                if (response.StatusCode == HttpStatusCode.BadRequest)
                {
                    throw new HttpRequestException();
                }
                return response;
            }));
        }
Ejemplo n.º 5
0
        public static AsyncPolicy GenerateCosmosPolicy(IAsyncPolicy[] chainedPolicies = null)
        {
            AsyncPolicy documentClientExceptionRetry = Policy.Handle <CosmosException>(e => (int)e.StatusCode != 429)
                                                       .WaitAndRetryAsync(new[] { TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(15), TimeSpan.FromSeconds(30) });

            return(GenerateCosmosPolicyInternal(documentClientExceptionRetry, chainedPolicies));
        }
Ejemplo n.º 6
0
        public TemplateCreateCommandValidator(
            IPolicyRepository policyRepository,
            IPolicyResiliencePolicies policyResiliencePolicies)
        {
            Guard.ArgumentNotNull(policyRepository, nameof(policyRepository));
            Guard.ArgumentNotNull(policyResiliencePolicies?.PolicyRepository, nameof(policyResiliencePolicies.PolicyRepository));

            AsyncPolicy policyRepositoryPolicy = policyResiliencePolicies.PolicyRepository;

            RuleFor(x => x.Description).Length(0, 1000);


            RuleFor(x => x.FundingStreamId)
            .NotEmpty()
            .WithMessage("Missing funding stream id")
            .MustAsync(async(command, propertyValue, context, cancellationToken) =>
            {
                FundingStream fundingStream = await policyRepositoryPolicy.ExecuteAsync(() => policyRepository.GetFundingStreamById(command.FundingStreamId));
                return(fundingStream != null);
            })
            .WithMessage("Funding stream id does not exist");

            RuleFor(x => x.FundingPeriodId)
            .NotEmpty()
            .WithMessage("Missing funding period id")
            .MustAsync(async(command, propertyValue, context, cancellationToken) =>
            {
                FundingPeriod fundingPeriod = await policyRepositoryPolicy.ExecuteAsync(() => policyRepository.GetFundingPeriodById(command.FundingPeriodId));
                return(fundingPeriod != null);
            })
            .WithMessage("Funding period id does not exist");
        }
Ejemplo n.º 7
0
        public static Task RaiseExceptionAndOrCancellationAsync <TException>(this AsyncPolicy policy, ExceptionAndOrCancellationScenario scenario, CancellationTokenSource cancellationTokenSource, Action onExecute, Func <int, TException> exceptionFactory) where TException : Exception
        {
            int counter = 0;

            CancellationToken cancellationToken = cancellationTokenSource.Token;

            return(policy.ExecuteAsync(ct =>
            {
                onExecute();

                counter++;

                if (scenario.AttemptDuringWhichToCancel.HasValue && counter >= scenario.AttemptDuringWhichToCancel.Value)
                {
                    cancellationTokenSource.Cancel();
                }

                if (scenario.ActionObservesCancellation)
                {
                    ct.ThrowIfCancellationRequested();
                }

                if (counter <= scenario.NumberOfTimesToRaiseException)
                {
                    throw exceptionFactory(counter);
                }

                return TaskHelper.EmptyTask;
            }, cancellationToken));
        }
Ejemplo n.º 8
0
        public FundingStructureService(
            ICacheProvider cacheProvider,
            ISpecificationsService specificationsService,
            ICalculationsApiClient calculationsApiClient,
            IGraphApiClient graphApiClient,
            Common.ApiClient.Policies.IPoliciesApiClient policiesApiClient,
            IValidator <UpdateFundingStructureLastModifiedRequest> validator,
            ISpecificationsResiliencePolicies resiliencePolicies)
        {
            Guard.ArgumentNotNull(specificationsService, nameof(specificationsService));
            Guard.ArgumentNotNull(policiesApiClient, nameof(policiesApiClient));
            Guard.ArgumentNotNull(calculationsApiClient, nameof(calculationsApiClient));
            Guard.ArgumentNotNull(graphApiClient, nameof(graphApiClient));
            Guard.ArgumentNotNull(cacheProvider, nameof(cacheProvider));
            Guard.ArgumentNotNull(validator, nameof(validator));

            Guard.ArgumentNotNull(resiliencePolicies, nameof(resiliencePolicies));
            Guard.ArgumentNotNull(resiliencePolicies?.PoliciesApiClient, nameof(resiliencePolicies.PoliciesApiClient));
            Guard.ArgumentNotNull(resiliencePolicies?.CacheProvider, nameof(resiliencePolicies.CacheProvider));
            Guard.ArgumentNotNull(resiliencePolicies?.CalcsApiClient, nameof(resiliencePolicies.CalcsApiClient));

            _specificationsService = specificationsService;
            _policiesApiClient     = policiesApiClient;
            _calculationsApiClient = calculationsApiClient;
            _graphApiClient        = graphApiClient;
            _cacheProvider         = cacheProvider;
            _validator             = validator;

            _policiesResilience     = resiliencePolicies.PoliciesApiClient;
            _cacheResilience        = resiliencePolicies.CacheProvider;
            _calculationsResilience = resiliencePolicies.CalcsApiClient;
        }
Ejemplo n.º 9
0
 private AsyncPolicy <IServiceResult> GetPolicy(string route, Type returnValueType)
 {
     return(Policies.GetOrAdd(route, key =>
     {
         var service = ServiceFactory.Get(route);
         var serviceCircuitBreakerOptions = service.ServiceCircuitBreakerOptions;
         var circuitBreakerEvent = ServiceProvider.GetService <ICircuitBreakerEvent>();
         AsyncPolicy <IServiceResult> policy = Policy <IServiceResult> .Handle <Exception>().FallbackAsync <IServiceResult>(
             async ct =>
         {
             //TODO 如果多次降级,根据路由排除此node
             if (circuitBreakerEvent != null)
             {
                 await circuitBreakerEvent.OnFallback(route, service.ClientMethodInfo);
             }
             if (returnValueType == null)
             {
                 return new ServiceResult(null);
             }
             if (service.ServiceCircuitBreakerOptions.HasInjection)
             {
                 return new ServiceResult(await ScriptInjection.Run(route));
             }
             return new ServiceResult(returnValueType.IsValueType ? Activator.CreateInstance(returnValueType) : default);
         });
Ejemplo n.º 10
0
        /// <summary>
        /// Set resilience policy
        /// </summary>
        /// <param name="policy">The policy.</param>
        /// <returns></returns>
        IConsumerLoggerBuilder IConsumerLoggerBuilder.WithResiliencePolicy(AsyncPolicy policy)
        {
            var prms   = _plan.WithResiliencePolicy(policy);
            var result = new ConsumerBuilder(prms);

            return(result);
        }
        /// <summary>
        /// Starts the notifications task.
        /// </summary>
        /// <example>
        /// <code>
        /// try
        /// {
        ///     connectApi.StartNotifications();
        /// }
        /// catch (Exception)
        /// {
        ///     throw;
        /// }
        /// </code>
        /// </example>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        public async Task StartNotificationsAsync()
        {
            if (!NotificationsStarted)
            {
                if (DeliveryMethod == null)
                {
                    DeliveryMethod = NotificationDeliveryMethod.DeliveryMethod.CLIENT_INITIATED;
                }

                if (DeliveryMethod == NotificationDeliveryMethod.DeliveryMethod.SERVER_INITIATED)
                {
                    throw new CloudApiException(400, "cannot call StartNotificationsAsync when delivery method is Server Initiated");
                }

                if (GetWebhook() != null && !forceClear)
                {
                    throw new CloudApiException(400, "cannot start notifications as a webhook is already in use");
                }

                // policy handler for retries. Uses Polly (https://github.com/App-vNext/Polly#wait-and-retry)
                // it will increase the time between retries progressively until it will stop ~2 minutes
                if (retryPolicy == null)
                {
                    retryPolicy = Policy.Handle <Exception>().WaitAndRetryAsync(
                        8,
                        retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
                        (exception, timeSpan, retryCount, context) =>
                    {
                        // check that is really an ApiException before doing the retry logic
                        if (!(exception is ApiException apiException) || apiException.ErrorCode != 500)
                        {
                            Log.Error($"Error long polling in {this.GetHashCode()} - {exception.Message}");
                            StopNotifications();
                        }
                    });
        public CalculateProfileService(IProfilePatternRepository profilePatternRepository,
                                       ICacheProvider cacheProvider,
                                       IValidator <ProfileBatchRequest> batchRequestValidation,
                                       ILogger logger,
                                       IProfilingResiliencePolicies resiliencePolicies,
                                       IProducerConsumerFactory producerConsumerFactory,
                                       IFundingValueProfiler fundingValueProfiler)
        {
            Guard.ArgumentNotNull(profilePatternRepository, nameof(profilePatternRepository));
            Guard.ArgumentNotNull(cacheProvider, nameof(cacheProvider));
            Guard.ArgumentNotNull(batchRequestValidation, nameof(batchRequestValidation));
            Guard.ArgumentNotNull(logger, nameof(logger));
            Guard.ArgumentNotNull(resiliencePolicies?.ProfilePatternRepository, nameof(resiliencePolicies.ProfilePatternRepository));
            Guard.ArgumentNotNull(resiliencePolicies?.Caching, nameof(resiliencePolicies.Caching));
            Guard.ArgumentNotNull(fundingValueProfiler, nameof(fundingValueProfiler));

            _profilePatternRepository = profilePatternRepository;
            _cacheProvider            = cacheProvider;
            _logger = logger;
            _producerConsumerFactory            = producerConsumerFactory;
            _batchRequestValidation             = batchRequestValidation;
            _profilePatternRepositoryResilience = resiliencePolicies.ProfilePatternRepository;
            _cachingResilience    = resiliencePolicies.Caching;
            _fundingValueProfiler = fundingValueProfiler;
        }
        public FundingStreamProviderVersionService(IProviderVersionsMetadataRepository providerVersionMetadata,
                                                   IProviderVersionService providerVersionService,
                                                   IProviderVersionSearchService providerVersionSearch,
                                                   IValidator <SetFundingStreamCurrentProviderVersionRequest> setCurrentRequestValidator,
                                                   IProvidersResiliencePolicies resiliencePolicies,
                                                   ILogger logger,
                                                   IMapper mapper)
        {
            Guard.ArgumentNotNull(providerVersionMetadata, nameof(providerVersionMetadata));
            Guard.ArgumentNotNull(providerVersionService, nameof(providerVersionService));
            Guard.ArgumentNotNull(providerVersionSearch, nameof(providerVersionSearch));
            Guard.ArgumentNotNull(resiliencePolicies?.ProviderVersionMetadataRepository, nameof(resiliencePolicies.ProviderVersionMetadataRepository));
            Guard.ArgumentNotNull(resiliencePolicies?.ProviderVersionsSearchRepository, nameof(resiliencePolicies.ProviderVersionsSearchRepository));
            Guard.ArgumentNotNull(logger, nameof(logger));
            Guard.ArgumentNotNull(mapper, nameof(mapper));

            _providerVersionMetadata       = providerVersionMetadata;
            _providerVersionMetadataPolicy = resiliencePolicies.ProviderVersionMetadataRepository;
            _providerVersionSearchPolicy   = resiliencePolicies.ProviderVersionsSearchRepository;
            _logger = logger;
            _providerVersionSearch      = providerVersionSearch;
            _setCurrentRequestValidator = setCurrentRequestValidator;
            _providerVersionService     = providerVersionService;
            _mapper = mapper;
        }
Ejemplo n.º 14
0
        public SpecificationCalculationAnalysis(ICalcsResiliencePolicies policies,
                                                ISpecificationsApiClient specifications,
                                                ICalculationsRepository calculations,
                                                ICalculationAnalysis calculationAnalysis,
                                                IBuildProjectsService buildProjectsService,
                                                IDatasetReferenceService datasetReferenceService,
                                                IMapper mapper)
        {
            Guard.ArgumentNotNull(policies?.CalculationsRepository, nameof(policies.CalculationsRepository));
            Guard.ArgumentNotNull(policies?.SpecificationsApiClient, nameof(policies.SpecificationsApiClient));
            Guard.ArgumentNotNull(specifications, nameof(specifications));
            Guard.ArgumentNotNull(calculations, nameof(calculations));
            Guard.ArgumentNotNull(calculationAnalysis, nameof(calculationAnalysis));
            Guard.ArgumentNotNull(buildProjectsService, nameof(buildProjectsService));
            Guard.ArgumentNotNull(datasetReferenceService, nameof(datasetReferenceService));
            Guard.ArgumentNotNull(mapper, nameof(mapper));

            _specificationsResilience = policies.SpecificationsApiClient;
            _calculationsResilience   = policies.CalculationsRepository;
            _specifications           = specifications;
            _calculations             = calculations;
            _calculationAnalysis      = calculationAnalysis;
            _buildProjectsService     = buildProjectsService;
            _datasetReferenceService  = datasetReferenceService;
            _mapper = mapper;
        }
Ejemplo n.º 15
0
        public PublishedProviderRetrievalService(
            IPublishedFundingRepository publishedFundingRepository,
            IPublishingResiliencePolicies publishingResiliencePolicies,
            IProvidersApiClient providersApiClient,
            ILogger logger,
            IMapper mapper,
            IExternalApiFileSystemCacheSettings cacheSettings,
            IFileSystemCache fileSystemCache)
        {
            Guard.ArgumentNotNull(publishedFundingRepository, nameof(publishedFundingRepository));
            Guard.ArgumentNotNull(providersApiClient, nameof(providersApiClient));
            Guard.ArgumentNotNull(logger, nameof(logger));
            Guard.ArgumentNotNull(mapper, nameof(mapper));
            Guard.ArgumentNotNull(publishingResiliencePolicies, nameof(publishingResiliencePolicies));
            Guard.ArgumentNotNull(publishingResiliencePolicies.PublishedFundingRepository, nameof(publishingResiliencePolicies.PublishedFundingRepository));
            Guard.ArgumentNotNull(publishingResiliencePolicies.ProvidersApiClient, nameof(publishingResiliencePolicies.ProvidersApiClient));
            Guard.ArgumentNotNull(cacheSettings, nameof(cacheSettings));
            Guard.ArgumentNotNull(fileSystemCache, nameof(fileSystemCache));

            _publishedFundingRepository = publishedFundingRepository;
            _providersApiClient         = providersApiClient;
            _logger          = logger;
            _mapper          = mapper;
            _cacheSettings   = cacheSettings;
            _fileSystemCache = fileSystemCache;
            _publishedFundingRepositoryPolicy = publishingResiliencePolicies.PublishedFundingRepository;
            _providersApiClientPolicy         = publishingResiliencePolicies.ProvidersApiClient;
        }
        public DeletePublishedProvidersService(ICreateDeletePublishedProvidersJobs jobs,
                                               IPublishedFundingRepository publishedFundingRepository,
                                               IPublishingResiliencePolicies publishedFundingResilience,
                                               IJobManagement jobManagement,
                                               IDeleteFundingSearchDocumentsService deleteFundingSearchDocumentsService,
                                               IDeletePublishedFundingBlobDocumentsService deletePublishedFundingBlobDocumentsService,
                                               IDeselectSpecificationForFundingService deselectSpecificationForFundingService,
                                               ILogger logger) : base(jobManagement, logger)
        {
            Guard.ArgumentNotNull(jobs, nameof(jobs));
            Guard.ArgumentNotNull(publishedFundingRepository, nameof(publishedFundingRepository));
            Guard.ArgumentNotNull(publishedFundingResilience?.PublishedFundingRepository, nameof(publishedFundingResilience.PublishedFundingRepository));
            Guard.ArgumentNotNull(publishedFundingResilience?.BlobClient, nameof(publishedFundingResilience.BlobClient));
            Guard.ArgumentNotNull(logger, nameof(logger));
            Guard.ArgumentNotNull(deletePublishedFundingBlobDocumentsService, nameof(deletePublishedFundingBlobDocumentsService));
            Guard.ArgumentNotNull(deleteFundingSearchDocumentsService, nameof(deleteFundingSearchDocumentsService));
            Guard.ArgumentNotNull(deselectSpecificationForFundingService, nameof(deleteFundingSearchDocumentsService));

            _jobs = jobs;
            _publishedFundingRepository = publishedFundingRepository;
            _logger = logger;
            _deselectSpecificationForFundingService     = deselectSpecificationForFundingService;
            _deleteFundingSearchDocumentsService        = deleteFundingSearchDocumentsService;
            _deletePublishedFundingBlobDocumentsService = deletePublishedFundingBlobDocumentsService;
            _publishedFundingRepositoryPolicy           = publishedFundingResilience.PublishedFundingRepository;
        }
Ejemplo n.º 17
0
        public ProviderSnapshotDataLoadService(ILogger logger,
                                               ISpecificationsApiClient specificationsApiClient,
                                               IProviderVersionService providerVersionService,
                                               IProvidersResiliencePolicies resiliencePolicies,
                                               IFundingDataZoneApiClient fundingDataZoneApiClient,
                                               IMapper mapper,
                                               IJobManagement jobManagement) : base(jobManagement, logger)
        {
            Guard.ArgumentNotNull(logger, nameof(logger));
            Guard.ArgumentNotNull(specificationsApiClient, nameof(specificationsApiClient));
            Guard.ArgumentNotNull(providerVersionService, nameof(providerVersionService));
            Guard.ArgumentNotNull(resiliencePolicies?.SpecificationsApiClient, nameof(resiliencePolicies.SpecificationsApiClient));
            Guard.ArgumentNotNull(resiliencePolicies?.FundingDataZoneApiClient, nameof(resiliencePolicies.FundingDataZoneApiClient));
            Guard.ArgumentNotNull(fundingDataZoneApiClient, nameof(fundingDataZoneApiClient));
            Guard.ArgumentNotNull(mapper, nameof(mapper));
            Guard.ArgumentNotNull(jobManagement, nameof(jobManagement));

            _logger = logger;
            _specificationsApiClient        = specificationsApiClient;
            _providerVersionService         = providerVersionService;
            _specificationsApiClientPolicy  = resiliencePolicies.SpecificationsApiClient;
            _fundingDataZoneApiClientPolicy = resiliencePolicies.FundingDataZoneApiClient;
            _fundingDataZoneApiClient       = fundingDataZoneApiClient;
            _mapper        = mapper;
            _jobManagement = jobManagement;
        }
        public TemplatesReIndexerService(ISearchRepository <TemplateIndex> searchRepository,
                                         IPolicyResiliencePolicies policyResiliencePolicies,
                                         IPolicyRepository policyRepository,
                                         ITemplateRepository templateRepository,
                                         IJobManagement jobManagement,
                                         ILogger logger) : base(jobManagement, logger)
        {
            Guard.ArgumentNotNull(searchRepository, nameof(searchRepository));
            Guard.ArgumentNotNull(policyResiliencePolicies?.TemplatesSearchRepository,
                                  nameof(policyResiliencePolicies.TemplatesSearchRepository));
            Guard.ArgumentNotNull(policyRepository, nameof(policyRepository));
            Guard.ArgumentNotNull(policyResiliencePolicies?.PolicyRepository,
                                  nameof(policyResiliencePolicies.PolicyRepository));
            Guard.ArgumentNotNull(templateRepository, nameof(templateRepository));
            Guard.ArgumentNotNull(policyResiliencePolicies?.TemplatesRepository,
                                  nameof(policyResiliencePolicies.TemplatesRepository));
            Guard.ArgumentNotNull(jobManagement, nameof(jobManagement));
            Guard.ArgumentNotNull(logger, nameof(logger));

            _searchRepository              = searchRepository;
            _searchRepositoryResilience    = policyResiliencePolicies.TemplatesSearchRepository;
            _templatesRepository           = templateRepository;
            _templatesRepositoryResilience = policyResiliencePolicies.TemplatesRepository;
            _jobManagement = jobManagement;
            _logger        = logger;
        }
        public MigrationClients(IServiceProvider services)
        {
            ServiceProviderWrapper serviceProviderWrapper = new ServiceProviderWrapper(services);

            ICalculationsEtlResiliencePolicies policies = serviceProviderWrapper.GetService <ICalculationsEtlResiliencePolicies>();

            Guard.ArgumentNotNull(policies?.CalculationsApiClient, nameof(policies.CalculationsApiClient));
            Guard.ArgumentNotNull(policies?.SpecificationApiClient, nameof(policies.SpecificationApiClient));
            Guard.ArgumentNotNull(policies?.DataSetsApiClient, nameof(policies.DataSetsApiClient));

            _calculationsPolicy   = policies.CalculationsApiClient;
            _specificationsPolicy = policies.SpecificationApiClient;
            _dataSetsPolicy       = policies.DataSetsApiClient;

            ICalculationsApiClient   calculations   = serviceProviderWrapper.GetService <ICalculationsApiClient>();
            ISpecificationsApiClient specifications = serviceProviderWrapper.GetService <ISpecificationsApiClient>();
            IDatasetsApiClient       dataSets       = serviceProviderWrapper.GetService <IDatasetsApiClient>();

            Guard.ArgumentNotNull(calculations, nameof(ICalculationsApiClient));
            Guard.ArgumentNotNull(specifications, nameof(ISpecificationsApiClient));
            Guard.ArgumentNotNull(dataSets, nameof(IDatasetsApiClient));

            _calculations   = calculations;
            _specifications = specifications;
            _dataSets       = dataSets;
        }
Ejemplo n.º 20
0
        public void Wrapping_only_one_policy_using_static_wrap_syntax_should_throw()
        {
            AsyncPolicy singlePolicy = Policy.Handle <Exception>().RetryAsync();
            Action      config       = () => Policy.WrapAsync(new[] { singlePolicy });

            config.ShouldThrow <ArgumentException>().And.ParamName.Should().Be("policies");
        }
Ejemplo n.º 21
0
        /// <summary>
        /// Default constructor.
        /// </summary>
        ///
        /// <param name="wordLimit">The <see cref="int"/> limit for the number of
        /// words to return.  Word limit must be positive.</param>
        /// <param name="excludedWords">The <see cref="List{string}"/> containing words to exclude
        /// from the crawl.</param>
        /// <param name="baseAddress">The <see cref="string"/> base address to set in <see cref="client"/>.
        /// THIS IS USED PRIMARILY FOR TESTING. THERE IS NOT NEED TO SET THIS VALUE
        /// EXPLICITLY OUTSIDE OF TEST ENVIRONMENT.</param>
        public Crawler(
            int wordLimit = 10,
            List <string> excludedWords = null,
            string baseAddress          = null)
        {
            // initialize maps and sets

            this.wordLimit     = (wordLimit < 0) ? 10 : wordLimit;
            this.priorityQueue = new SimplePriorityQueue <string>();
            this.wordDict      = new Dictionary <string, int>();
            this.excludedWords = (excludedWords == null) ? new HashSet <string>() : excludedWords.ToHashSet <string>();

            // configure HttpClient

            if (string.IsNullOrEmpty(baseAddress))
            {
                baseAddress = HttpSettings.BaseAddress;
            }

            this.handler            = new HttpClientHandler();
            this.client             = new HttpClient(handler, false);
            this.client.BaseAddress = new Uri(baseAddress);

            // configure retry policy

            this.retryPolicy = Policy
                               .Handle <HttpRequestException>()
                               .WaitAndRetryAsync(HttpSettings.MaxRetryAttempts, i => HttpSettings.RetryInterval);
        }
        public QaSchemaService(IPoliciesApiClient policies,
                               ISpecificationsApiClient specificationsApiClient,
                               ITemplateMetadataResolver templateMetadataResolver,
                               ISqlSchemaGenerator schemaGenerator,
                               IQaRepository qaRepository,
                               IProfilingApiClient profilingClient,
                               ISqlNameGenerator sqlNames,
                               IPublishingResiliencePolicies resiliencePolicies)
        {
            Guard.ArgumentNotNull(policies, nameof(policies));
            Guard.ArgumentNotNull(specificationsApiClient, nameof(specificationsApiClient));
            Guard.ArgumentNotNull(templateMetadataResolver, nameof(templateMetadataResolver));
            Guard.ArgumentNotNull(schemaGenerator, nameof(schemaGenerator));
            Guard.ArgumentNotNull(qaRepository, nameof(qaRepository));
            Guard.ArgumentNotNull(profilingClient, nameof(profilingClient));
            Guard.ArgumentNotNull(sqlNames, nameof(sqlNames));

            _policies                 = policies;
            _specifications           = specificationsApiClient;
            _templateMetadataResolver = templateMetadataResolver;
            _schemaGenerator          = schemaGenerator;
            _qaRepository             = qaRepository;
            _profilingClient          = profilingClient;
            _sqlNames                 = sqlNames;
            _specificationResilience  = resiliencePolicies.SpecificationsApiClient;
            _policiesResilience       = resiliencePolicies.PoliciesApiClient;

            //TODO; extract all of the different table builders so that this can more easily tested
            //at the moment it needs a god test with too much setup to make much sense to anyone
        }
        public FundingLineCsvGenerator(IFundingLineCsvTransformServiceLocator transformServiceLocator,
                                       IPublishedFundingPredicateBuilder predicateBuilder,
                                       IBlobClient blobClient,
                                       IFileSystemAccess fileSystemAccess,
                                       IFileSystemCacheSettings fileSystemCacheSettings,
                                       IFundingLineCsvBatchProcessorServiceLocator batchProcessorServiceLocator,
                                       IPublishingResiliencePolicies policies,
                                       IJobManagement jobManagement,
                                       ILogger logger) : base(jobManagement, logger)
        {
            Guard.ArgumentNotNull(logger, nameof(logger));
            Guard.ArgumentNotNull(blobClient, nameof(blobClient));
            Guard.ArgumentNotNull(transformServiceLocator, nameof(transformServiceLocator));
            Guard.ArgumentNotNull(predicateBuilder, nameof(predicateBuilder));
            Guard.ArgumentNotNull(fileSystemAccess, nameof(fileSystemAccess));
            Guard.ArgumentNotNull(fileSystemCacheSettings, nameof(fileSystemCacheSettings));
            Guard.ArgumentNotNull(policies?.BlobClient, nameof(policies.BlobClient));
            Guard.ArgumentNotNull(batchProcessorServiceLocator, nameof(batchProcessorServiceLocator));

            _logger = logger;
            _batchProcessorServiceLocator = batchProcessorServiceLocator;
            _blobClient = blobClient;
            _transformServiceLocator = transformServiceLocator;
            _fileSystemAccess        = fileSystemAccess;
            _fileSystemCacheSettings = fileSystemCacheSettings;
            _blobClientPolicy        = policies.BlobClient;
        }
Ejemplo n.º 24
0
        public NukaRedisCache(IOptions <RedisCacheOptions> optionsAccessor, ILogger <NukaRedisCache> logger)
        {
            if (optionsAccessor == null)
            {
                throw new ArgumentNullException(nameof(NukaRedisCache));
            }

            _options = optionsAccessor.Value;
            _logger  = logger;

            _instance = _options.InstanceName ?? string.Empty;

            _syncPolicy = Policy
                          .Handle <RedisConnectionException>()
                          .Retry(1, (exception, i) =>
            {
                logger.LogError(exception, "redis-connection-exception {@context}",
                                new Dictionary <string, string> {
                    ["error_message"] = exception.Message
                });
                Reconnect();
            });

            _asyncPolicy = Policy
                           .Handle <RedisConnectionException>()
                           .RetryAsync(1, async(exception, i) =>
            {
                logger.LogError(exception, "redis-connection-exception {@context}",
                                new Dictionary <string, string> {
                    ["error_message"] = exception.Message
                });
                await ReconnectAsync();
            });
        }
Ejemplo n.º 25
0
        private static AsyncPolicy GenerateCosmosPolicyInternal(AsyncPolicy documentClientExceptionRetry,
                                                                IAsyncPolicy[] chainedPolicies = null)
        {
            AsyncPolicy requestRateTooLargeExceptionRetry = Policy.Handle <CosmosException>(e => (int)e.StatusCode == 429)
                                                            .WaitAndRetryAsync(new[] { TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(15), TimeSpan.FromSeconds(60), TimeSpan.FromSeconds(120) });

            AsyncPolicy operationInProgressExceptionRetry = Policy.Handle <CosmosException>(e => (int)e.StatusCode == 423)
                                                            .WaitAndRetryAsync(new[] { TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(20), TimeSpan.FromSeconds(30), TimeSpan.FromSeconds(60) });

            AsyncPolicy circuitBreaker = Policy.Handle <CosmosException>().CircuitBreakerAsync(1000, TimeSpan.FromMinutes(1));

            List <IAsyncPolicy> policies = new List <IAsyncPolicy>(8)
            {
                documentClientExceptionRetry,
                requestRateTooLargeExceptionRetry,
                operationInProgressExceptionRetry,
                circuitBreaker,
            };

            if (chainedPolicies != null && chainedPolicies.Any())
            {
                policies.AddRange(chainedPolicies);
            }

            AsyncPolicyWrap policyWrap = Policy.WrapAsync(policies.ToArray());

            return(policyWrap);
        }
        public static async Task <TResult> RaiseResultSequenceAndOrCancellationAsync <TResult>(
            this AsyncPolicy <TResult> policy, Scenario scenario, CancellationTokenSource cancellationTokenSource,
            Action onExecute, IEnumerable <TResult> resultsToRaise)
        {
            int counter = 0;

            CancellationToken cancellationToken = cancellationTokenSource.Token;

            using (var enumerator = resultsToRaise.GetEnumerator())
            {
                return(await policy.ExecuteAsync(ct =>
                {
                    onExecute();

                    counter++;

                    if (!enumerator.MoveNext())
                    {
                        throw new ArgumentOutOfRangeException(nameof(resultsToRaise), $"Not enough {typeof(TResult).Name}  values in {nameof(resultsToRaise)}.");
                    }

                    if (scenario.AttemptDuringWhichToCancel.HasValue && counter >= scenario.AttemptDuringWhichToCancel.Value)
                    {
                        cancellationTokenSource.Cancel();
                    }

                    if (scenario.ActionObservesCancellation)
                    {
                        ct.ThrowIfCancellationRequested();
                    }

                    return Task.FromResult(enumerator.Current);
                }, cancellationToken));
            }
        }
Ejemplo n.º 27
0
        public PublishedFundingStatusUpdateService(
            IPublishingResiliencePolicies publishingResiliencePolicies,
            IVersionRepository <PublishedFundingVersion> publishedFundingVersionRepository,
            IPublishedFundingIdGeneratorResolver publishedFundingIdGeneratorResolver,
            ILogger logger,
            IPublishingEngineOptions publishingEngineOptions,
            IVersionBulkRepository <PublishedFundingVersion> publishedFundingVersionBulkRepository,
            IPublishedFundingBulkRepository publishedFundingBulkRepository)
        {
            Guard.ArgumentNotNull(publishingResiliencePolicies?.PublishedFundingRepository, nameof(publishingResiliencePolicies.PublishedFundingRepository));
            Guard.ArgumentNotNull(publishingResiliencePolicies?.PublishedProviderVersionRepository, nameof(publishingResiliencePolicies.PublishedProviderVersionRepository));
            Guard.ArgumentNotNull(publishedFundingVersionRepository, nameof(publishedFundingVersionRepository));
            Guard.ArgumentNotNull(logger, nameof(logger));
            Guard.ArgumentNotNull(publishingEngineOptions, nameof(publishingEngineOptions));
            Guard.ArgumentNotNull(publishedFundingVersionBulkRepository, nameof(publishedFundingVersionBulkRepository));
            Guard.ArgumentNotNull(publishedFundingBulkRepository, nameof(publishedFundingBulkRepository));

            _publishingResiliencePolicy          = publishingResiliencePolicies.PublishedFundingRepository;
            _publishedFundingVersionRepository   = publishedFundingVersionRepository;
            _publishedFundingIdGeneratorResolver = publishedFundingIdGeneratorResolver;
            _logger = logger;
            _publishingEngineOptions = publishingEngineOptions;
            _publishedFundingVersionBulkRepository = publishedFundingVersionBulkRepository;
            _publishedFundingBulkRepository        = publishedFundingBulkRepository;
            _versionRepositoryPolicy = publishingResiliencePolicies.PublishedProviderVersionRepository;
        }
        public static AsyncPolicy GenerateRedisPolicy(params IAsyncPolicy[] chainedPolicies)
        {
            AsyncPolicy redisServerExceptionRetry = Policy.Handle <RedisServerException>()
                                                    .WaitAndRetryAsync(new[] { TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(15) });

            AsyncPolicy circuitBreakerRedisServerException = Policy.Handle <RedisServerException>().CircuitBreakerAsync(1000, TimeSpan.FromMinutes(1));

            AsyncPolicy connectionExceptionRetry = Policy.Handle <RedisConnectionException>()
                                                   .WaitAndRetryAsync(new[] { TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(15) });

            AsyncPolicy connectionExceptionCircuitBreaker = Policy.Handle <RedisConnectionException>().CircuitBreakerAsync(250, TimeSpan.FromMinutes(1));

            List <IAsyncPolicy> policies = new List <IAsyncPolicy>(8)
            {
                redisServerExceptionRetry,
                connectionExceptionRetry,
                circuitBreakerRedisServerException,
                connectionExceptionCircuitBreaker
            };

            if (!chainedPolicies.IsNullOrEmpty())
            {
                policies.AddRange(chainedPolicies);
            }

            AsyncPolicyWrap policyWrap = Policy.WrapAsync(policies.ToArray());

            return(policyWrap);
        }
Ejemplo n.º 29
0
        /// <summary>
        /// Initializes from attribute parameters. This will get the <see cref="IAmAPolicyRegistry" /> from the <see cref="IRequestContext" /> and query it for the
        /// policy identified in <see cref="UsePolicyAttribute" />
        /// </summary>
        /// <param name="initializerList">The initializer list.</param>
        /// <exception cref="System.ArgumentException">Could not find the policy for this attribute, did you register it with the command processor's container;initializerList</exception>
        public override void InitializeFromAttributeParams(params object[] initializerList)
        {
            //we expect the first and only parameter to be a string
            var policyName = (string)initializerList[0];

            _policy = Context.Policies.Get <AsyncPolicy>(policyName);
        }
Ejemplo n.º 30
0
        public virtual void AddPolicies(IServiceCollection services)
        {
            services.AddSingleton <IReadOnlyPolicyRegistry <string>, PolicyRegistry>((serviceProvider) =>
            {
                PolicyRegistry registry = new PolicyRegistry();

                Action <Exception, TimeSpan> onBreak = (exception, timespan) =>
                {
                    serviceProvider.GetRequiredService <ILogger>().Error($"Utilizando CircuitBreaker durante {timespan.TotalSeconds} segundo(s) tras el error: {exception}");
                };
                Action onReset = () =>
                {
                    serviceProvider.GetRequiredService <ILogger>().Info($"Finalizando CircuitBreaker.");
                };
                AsyncCircuitBreakerPolicy breaker = Policy
                                                    .Handle <Exception>()
                                                    .CircuitBreakerAsync(2, TimeSpan.FromSeconds(30), onBreak, onReset);

                AsyncPolicy policyCache = Policy
                                          .Handle <Exception>()
                                          .RetryAsync(3, onRetry: (exception, retryCount) =>
                {
                    serviceProvider.GetRequiredService <ILogger>().Warn($"Error en el intento {retryCount}: {exception}");
                })
                                          .WrapAsync(breaker);

                registry.Add(Claves.CLAVE_POLITICA_CACHE, policyCache);
                return(registry);
            });
        }