Esempio n. 1
0
		/// <summary>IoC constructor.</summary>>
		public CloudProvisioning(ICloudConfigurationSettings settings, ILog log)
		{
			_log = log;
			_retryPolicy = AzureManagementPolicies.TransientServerErrorBackOff;

			// try get settings and certificate
			_deploymentId = CloudEnvironment.AzureDeploymentId;
			_subscriptionId = settings.SelfManagementSubscriptionId ?? Maybe.String;
			var certificateThumbprint = settings.SelfManagementCertificateThumbprint ?? Maybe.String;
			if (certificateThumbprint.HasValue)
			{
				_certificate = CloudEnvironment.GetCertificate(certificateThumbprint.Value);
			}

			// early evaluate management status for intrinsic fault states, to skip further processing
			if (!_deploymentId.HasValue || !_subscriptionId.HasValue || !certificateThumbprint.HasValue)
			{
				_status = ManagementStatus.ConfigurationMissing;
				return;
			}
			if (!_certificate.HasValue)
			{
				_status = ManagementStatus.CertificateMissing;
				return;
			}

			// ok, now try find service matching the deployment
			_enabled = true;
			TryFindDeployment();
		}
Esempio n. 2
0
        /// <summary>
        ///	Returns access token for making authorized API calls
        /// </summary>
        /// <returns></returns>
        public Task <OAuthCredentials> GetPermanentCredentials(string temporaryToken, string temporaryTokenSecret, string verifierCode)
        {
            Condition.Requires(temporaryToken).IsNotNullOrEmpty();
            Condition.Requires(temporaryTokenSecret).IsNotNullOrEmpty();
            Condition.Requires(verifierCode).IsNotNullOrEmpty();

            var mark = Mark.CreateNew();

            var requestParameters = new Dictionary <string, string>
            {
                { "oauth_token", temporaryToken },
                { "oauth_verifier", verifierCode }
            };

            return(Policy.HandleResult <OAuthCredentials>(credentials => credentials == null)
                   .WaitAndRetryAsync(Config.RetryAttempts,
                                      retryCount => TimeSpan.FromSeconds(ActionPolicy.GetDelayBeforeNextAttempt(retryCount)),
                                      (entityRaw, timeSpan, retryCount, context) =>
            {
                string retryDetails = CreateMethodCallInfo(EtsyEndPoint.GetAccessTokenUrl, mark, additionalInfo: this.AdditionalLogInfo());
                EtsyLogger.LogTraceRetryStarted(timeSpan.Seconds, retryCount, retryDetails);
            })
                   .ExecuteAsync(async() =>
            {
                OAuthCredentials credentials = null;
                string url = Config.ApiBaseUrl + EtsyEndPoint.GetAccessTokenUrl;

                try
                {
                    var oauthParameters = Authenticator.GetOAuthRequestParameters(url, "GET", temporaryTokenSecret, requestParameters);
                    url = Authenticator.GetUrl(url, oauthParameters);

                    EtsyLogger.LogStarted(this.CreateMethodCallInfo(url, mark, additionalInfo: this.AdditionalLogInfo()));

                    HttpResponseMessage response = await HttpClient.GetAsync(url).ConfigureAwait(false);
                    var result = response.Content.ReadAsStringAsync().Result;

                    ThrowIfError(response, result);

                    EtsyLogger.LogEnd(this.CreateMethodCallInfo(url, mark, methodResult: result.ToJson(), additionalInfo: this.AdditionalLogInfo()));

                    var queryParams = Misc.ParseQueryParams(result);
                    queryParams.TryGetValue("oauth_token", out var token);
                    queryParams.TryGetValue("oauth_token_secret", out var tokenSecret);

                    if (!(string.IsNullOrEmpty(token) ||
                          string.IsNullOrEmpty(tokenSecret)))
                    {
                        credentials = new OAuthCredentials(null, token, tokenSecret);
                    }
                }
                catch (Exception exception)
                {
                    var etsyException = new EtsyException(this.CreateMethodCallInfo(url, mark, additionalInfo: this.AdditionalLogInfo()), exception);
                    EtsyLogger.LogTraceException(etsyException);
                }

                return credentials;
            }));
        }
Esempio n. 3
0
        static void Run(ActionPolicy policy, Action <Mock <ISpy> > verify)
        {
            var spy = Pleasure.Spy();

            spy.Setup(r => r.Is()).Throws <ArgumentException>();
            var exception = Catch.Exception(() => policy.Do(() => spy.Object.Is()));

            exception.ShouldNotBeNull();
            verify(spy);
        }
Esempio n. 4
0
 public ExpectedSarsa(DiscreteEnvironment environment,
                      ActionPolicy policy,
                      double stepSize, double discount)
 {
     this.environment    = environment;
     this.policy         = policy;
     this.stepSize       = stepSize;
     this.discount       = discount;
     ActionValueFunction = new DiscreteActionValueFunction(environment);
 }
Esempio n. 5
0
        public static ActionPolicy CreateSubmit(Func <string> additionalLogInfo = null)
        {
            return(ActionPolicy.Handle <Exception>().Retry(RetryCount, (ex, i) =>
            {
                var delay = GetDelay(ex, i);
                var message = CreateRetryMessage(additionalLogInfo, i, delay, ex);
                ChannelAdvisorLogger.LogTrace(ex, message);

                SystemUtil.Sleep(delay);
            }));
        }
        public List <BigCommerceCategory> GetCategories()
        {
            var categories = new List <BigCommerceCategory>();
            var marker     = this.GetMarker();

            for (var i = 1; i < int.MaxValue; i++)
            {
                var endpoint             = "";    //mainEndpoint.ConcatParams(ParamsBuilder.CreateGetNextPageParams(new BigCommerceCommandConfig(i, RequestMaxLimit)));
                var categoriesWithinPage = ActionPolicy.Handle <Exception>().Retry(ActionPolicies.RetryCount, (ex, retryAttempt) =>
                {
                    if (PageAdjuster.TryAdjustPageIfResponseTooLarge(new PageInfo(i, this.RequestMaxLimit), this.RequestMinLimit, ex, out var newPageInfo))
                    {
                        i = newPageInfo.Index;
                        this.RequestMaxLimit = newPageInfo.Size;
                    }

                    ActionPolicies.LogRetryAndWait(ex, marker, endpoint, retryAttempt);
                }).Get(() => {
                    return(this._webRequestServices.GetResponseByRelativeUrl <BigCommerceCategoryInfoData>(BigCommerceCommand.GetCategoriesV3, endpoint, marker));
                });

                this.CreateApiDelay(categoriesWithinPage.Limits).Wait();                 //API requirement

                if (categoriesWithinPage.Response == null)
                {
                    break;
                }

                foreach (var category in categoriesWithinPage.Response.Data)
                {
                    var CatURL = category.Category_URL;

                    categories.Add(new BigCommerceCategory
                    {
                        Id           = category.Id,
                        Category_URL = new BigCommerceCategoryURL()
                        {
                            Url = CatURL.Url
                        },
                        Category_Name = category.Name,
                        IsVisible     = category.IsVisible
                    });
                }

                if (categoriesWithinPage.Response.Data.Length < RequestMaxLimit)
                {
                    break;
                }
            }


            return(categories);
        }
 public static ActionPolicy Get(string marker, string url)
 {
     return(ActionPolicy.Handle <Exception>().Retry(RetryCount, (ex, retryAttempt) => {
         var delay = TimeSpan.FromSeconds(5 + 20 * retryAttempt);
         BigCommerceLogger.LogTraceException(new RetryInfo()
         {
             Mark = marker,
             Url = url,
             CurrentRetryAttempt = retryAttempt,
             TotalRetriesAttempts = RetryCount,
             DelayInSeconds = delay.TotalSeconds,
             Category = MessageCategoryEnum.Warning
         }, ex);
         SystemUtil.Sleep(delay);
     }));
 }
Esempio n. 8
0
		/// <summary>
		/// Static Constructor
		/// </summary>
		static AzurePolicies()
		{
			// Instrumentation
			ExecutionCounters.Default.RegisterRange(new[]
				{
					CountOnTransientServerError = new ExecutionCounter("Policies.ServerErrorRetryWait", 0, 0),
					CountOnTransientTableError = new ExecutionCounter("Policies.TableErrorRetryWait", 0, 0),
					CountOnSlowInstantiation = new ExecutionCounter("Policies.SlowInstantiationRetryWait", 0, 0)
				});

			// Initialize Policies
			TransientServerErrorBackOff = ActionPolicy.With(TransientServerErrorExceptionFilter)
				.Retry(30, OnTransientServerErrorRetry);

			TransientTableErrorBackOff = ActionPolicy.With(TransientTableErrorExceptionFilter)
				.Retry(30, OnTransientTableErrorRetry);

			SlowInstantiation = ActionPolicy.With(SlowInstantiationExceptionFilter)
				.Retry(30, OnSlowInstantiationRetry);
		}
        public List <BigCommerceProduct> GetProducts(bool includeExtendedInfo)
        {
            var mainEndpoint = "?include=variants,images";
            var products     = new List <BigCommerceProduct>();
            var marker       = this.GetMarker();

            for (var i = 1; i < int.MaxValue; i++)
            {
                var endpoint           = mainEndpoint.ConcatParams(ParamsBuilder.CreateGetNextPageParams(new BigCommerceCommandConfig(i, RequestMaxLimit)));
                var productsWithinPage = ActionPolicy.Handle <Exception>().Retry(ActionPolicies.RetryCount, (ex, retryAttempt) =>
                {
                    if (PageAdjuster.TryAdjustPageIfResponseTooLarge(new PageInfo(i, this.RequestMaxLimit), this.RequestMinLimit, ex, out var newPageInfo))
                    {
                        i = newPageInfo.Index;
                        this.RequestMaxLimit = newPageInfo.Size;
                    }

                    ActionPolicies.LogRetryAndWait(ex, marker, endpoint, retryAttempt);
                }).Get(() => {
                    return(this._webRequestServices.GetResponseByRelativeUrl <BigCommerceProductInfoData>(BigCommerceCommand.GetProductsV3, endpoint, marker));
                });

                this.CreateApiDelay(productsWithinPage.Limits).Wait();                 //API requirement

                if (productsWithinPage.Response == null)
                {
                    break;
                }

                foreach (var product in productsWithinPage.Response.Data)
                {
                    var productImageThumbnail = product.Images.FirstOrDefault(img => img.IsThumbnail);

                    var additional_images = product.Images;

                    var custom_url = product.Product_URL;

                    products.Add(new BigCommerceProduct
                    {
                        Id = product.Id,
                        InventoryTracking = this.ToCompatibleWithV2InventoryTrackingEnum(product.InventoryTracking),
                        Upc               = product.Upc,
                        Sku               = product.Sku,
                        Name              = product.Name,
                        Description       = product.Description,
                        Price             = product.Price,
                        IsProductVisible  = product.IsVisible,
                        Condition         = product.Condition,
                        Availability      = product.Availability,
                        ProductType       = product.Type,
                        SalePrice         = product.SalePrice,
                        RetailPrice       = product.RetailPrice,
                        CostPrice         = product.CostPrice,
                        Weight            = product.Weight,
                        BrandId           = product.BrandId,
                        Quantity          = product.Quantity,
                        Product_URL       = custom_url.ProductURL,
                        Categories        = product.Categories,
                        ThumbnailImageURL = new BigCommerceProductPrimaryImages()
                        {
                            StandardUrl = productImageThumbnail != null ? productImageThumbnail.UrlStandard : string.Empty
                        },
                        ProductOptions = product.Variants.Select(x => new BigCommerceProductOption
                        {
                            Id        = x.Id,
                            ProductId = x.ProductId,
                            Sku       = x.Sku,
                            Quantity  = x.Quantity,
                            Upc       = x.Upc,
                            Price     = x.Price,
                            CostPrice = x.CostPrice,
                            Weight    = x.Weight,
                            ImageFile = x.ImageUrl
                        }).ToList(),
                        Main_Images = product.Images.Select(y => new BigCommerceImage
                        {
                            UrlStandard = y.UrlStandard,
                            IsThumbnail = y.IsThumbnail
                        }).ToList()
                    });
                }

                if (productsWithinPage.Response.Data.Count < RequestMaxLimit)
                {
                    break;
                }
            }

            if (includeExtendedInfo)
            {
                base.FillWeightUnit(products, marker);
                base.FillBrands(products, marker);
            }

            return(products);
        }
Esempio n. 10
0
        public void GetDelayBeforeNextAttempt([Values(1, 3, 9)] int retryCount)
        {
            var delay = ActionPolicy.GetDelayBeforeNextAttempt(retryCount);

            Assert.AreEqual(5 + 20 * retryCount, delay);
        }
		/// <summary>
		/// Static Constructor
		/// </summary>
		static AzureManagementPolicies()
		{
			// Initialize Policies
			TransientServerErrorBackOff = ActionPolicy.With(TransientServerErrorExceptionFilter)
				.Retry(30, OnTransientServerErrorRetry);
		}
        public void Initialize(Action <InitScheduler> initializeAction = null)
        {
            var init = new InitScheduler();

            initializeAction.Do(action => action(init));
            Task.Factory.StartNew(() =>
            {
                try
                {
                    while (init.Conditional())
                    {
                        var dispatcher = IoCFactory.Instance.TryResolve <IDispatcher>();
                        foreach (var pair in dispatcher.Query(new GetExpectedDelayToSchedulerQuery
                        {
                            FetchSize = init.FetchSize,
                            Date = DateTime.UtcNow
                        }, init.Setting))
                        {
                            var ids = pair.Value.Select(r => r.Id).ToArray();
                            dispatcher.Push(new ChangeDelayToSchedulerStatusCommand
                            {
                                Ids    = ids,
                                Status = DelayOfStatus.InProgress
                            }, init.Setting);

                            try
                            {
                                var policy    = ActionPolicy.Direct();
                                var composite = new CommandComposite();
                                foreach (var delayToScheduler in pair.Value)
                                {
                                    var instanceCommand = delayToScheduler.Instance;
                                    composite.Quote(instanceCommand)
                                    .WithConnectionString(instanceCommand.Setting.Connection)
                                    .WithDateBaseString(instanceCommand.Setting.DataBaseInstance)
                                    .Mute(instanceCommand.Setting.Mute);
                                }

                                composite.Quote(new ChangeDelayToSchedulerStatusCommand
                                {
                                    Ids             = ids,
                                    Status          = DelayOfStatus.Success,
                                    UpdateNextStart = true
                                }, init.Setting);
                                policy.Do(() => dispatcher.Push(composite));
                            }
                            catch (Exception ex)
                            {
                                dispatcher.Push(new ChangeDelayToSchedulerStatusCommand
                                {
                                    Ids         = ids,
                                    Status      = DelayOfStatus.Error,
                                    Description = ex.ToString()
                                }, init.Setting);
                            }
                        }

                        ////ncrunch: no coverage start
                        Thread.Sleep(init.Interval);
                    }
                }
                ////ncrunch: no coverage end
                catch (Exception ex)
                {
                    if (!string.IsNullOrWhiteSpace(init.Log_Debug))
                    {
                        LoggingFactory.Instance.LogException(init.Log_Debug, ex);
                    }
                }
            }, init.TaskCreationOptions);
        }
Esempio n. 13
0
 public DefaultEventBroker()
 {
     this.defaultActionPolicy = ActionPolicy.Direct();
 }
Esempio n. 14
0
 public DefaultEventBroker WithActionPolicy(ActionPolicy actionPolicy)
 {
     this.defaultActionPolicy = actionPolicy;
     return(this);
 }
Esempio n. 15
0
        /// <summary>
        ///	Returns temporary credentials and login url for customer
        /// </summary>
        /// <param name="scopes">Permissions</param>
        /// <returns></returns>
        public Task <OAuthCredentials> GetTemporaryCredentials(string[] scopes)
        {
            Condition.Requires(scopes).IsNotEmpty();

            var mark = Mark.CreateNew();

            var requestParameters = new Dictionary <string, string>
            {
                { "scopes", string.Join(" ", scopes) },
                { "oauth_callback", "oob" }
            };

            return(Policy.HandleResult <OAuthCredentials>(credentials => credentials == null)
                   .WaitAndRetryAsync(Config.RetryAttempts,
                                      retryCount => TimeSpan.FromSeconds(ActionPolicy.GetDelayBeforeNextAttempt(retryCount)),
                                      (entityRaw, timeSpan, retryCount, context) =>
            {
                string retryDetails = CreateMethodCallInfo(EtsyEndPoint.GetRequestTokenUrl, mark, additionalInfo: this.AdditionalLogInfo());
                EtsyLogger.LogTraceRetryStarted(timeSpan.Seconds, retryCount, retryDetails);
            })
                   .ExecuteAsync(async() =>
            {
                OAuthCredentials credentials = null;

                string absoluteUrl = Config.ApiBaseUrl + EtsyEndPoint.GetRequestTokenUrl;

                var oauthParameters = Authenticator.GetOAuthRequestParameters(absoluteUrl, "GET", null, requestParameters);
                string url = Authenticator.GetUrl(absoluteUrl, oauthParameters);

                try
                {
                    EtsyLogger.LogStarted(this.CreateMethodCallInfo(url, mark, additionalInfo: this.AdditionalLogInfo()));

                    HttpResponseMessage response = await HttpClient.GetAsync(url).ConfigureAwait(false);

                    var result = response.Content.ReadAsStringAsync().Result;

                    EtsyLogger.LogEnd(this.CreateMethodCallInfo(url, mark, methodResult: result.ToJson(), additionalInfo: this.AdditionalLogInfo()));

                    ThrowIfError(response, result);

                    if (!string.IsNullOrEmpty(result) &&
                        result.IndexOf("login_url", StringComparison.InvariantCulture) > -1)
                    {
                        string loginUrl = Uri.UnescapeDataString(result.Replace("login_url=", ""));

                        string[] temp = loginUrl.Split('?');

                        if (temp.Length == 2)
                        {
                            var queryParams = Misc.ParseQueryParams(temp[1]);
                            queryParams.TryGetValue("oauth_token", out var token);
                            queryParams.TryGetValue("oauth_token_secret", out var tokenSecret);

                            if (token != null && tokenSecret != null)
                            {
                                credentials = new OAuthCredentials(loginUrl, token, tokenSecret);
                            }
                        }
                    }
                }
                catch (Exception exception)
                {
                    var etsyException = new EtsyException(this.CreateMethodCallInfo(url, mark, additionalInfo: this.AdditionalLogInfo()), exception);
                    EtsyLogger.LogTraceException(etsyException);
                }

                return credentials;
            }));
        }