Ejemplo n.º 1
0
        public AzureAtomicReader(CloudBlobClient storageClient, IDocumentStrategy strategy)
        {
            this._strategy = strategy;
            var folder = strategy.GetEntityBucket <TEntity>();

            this._containerDirectory = storageClient.GetBlobDirectory(folder);
            this._logger             = NetcoLogger.GetLogger(this.GetType());
            this._ap = ActionPolicyAsync.From((exception =>
            {
                var storageException = exception as StorageException;
                if (storageException == null)
                {
                    return(false);
                }

                switch (storageException.RequestInformation.HttpStatusCode)
                {
                case ( int )HttpStatusCode.InternalServerError:
                case ( int )HttpStatusCode.ServiceUnavailable:
                    return(true);

                default:
                    return(false);
                }
            })).Retry(200, (ex, i) =>
            {
                this._logger.Log().Trace(ex, "Retrying Azure API GET call: {0}/200", i);
                var secondsDelay = 0.2 + 0.1 * _random.Next(-1, 1);                   // randomize wait time
                Task.Delay(TimeSpan.FromSeconds(secondsDelay)).Wait();
            });
        }
Ejemplo n.º 2
0
        public static ActionPolicyAsync CreateSubmitAsync(Func <string> additionalLogInfo = null)
        {
            return(ActionPolicyAsync.Handle <Exception>().RetryAsync(RetryCount, async(ex, i) =>
            {
                var delay = GetDelay(ex, i);
                var message = CreateRetryMessage(additionalLogInfo, i, delay, ex);
                ChannelAdvisorLogger.LogTrace(ex, message);

                await Task.Delay(delay).ConfigureAwait(false);
            }));
        }
        public MagentoServiceLowLevel()
        {
            this.RepeatOnAuthProblemAsync = ActionPolicyAsync.From((exception =>
            {
                var webException = (exception as MagentoWebException)?.InnerException as WebException;
                if (webException == null)
                {
                    return(false);
                }

                switch (webException.Status)
                {
                case WebExceptionStatus.ProtocolError:
                    var response = webException.Response as HttpWebResponse;
                    if (response == null)
                    {
                        return(false);
                    }
                    switch (response.StatusCode)
                    {
                    case HttpStatusCode.Unauthorized:
                        return(true);

                    default:
                        return(false);
                    }

                default:
                    return(false);
                }
            }))
                                            .RetryAsync(3, async(ex, i) =>
            {
                await this._reauthorizeLock.WaitAsync();
                var reauthorizationsCountPropagation = this.reauthorizationsCount;
                this._reauthorizeLock.Release();
                await this._reauthorizeLock.WaitAsync();
                try
                {
                    if (reauthorizationsCountPropagation != this.reauthorizationsCount)
                    {
                        return;
                    }
                    Interlocked.Increment(ref this.reauthorizationsCount);
                    MagentoLogger.Log().Trace(ex, "Retrying Magento API call due to authorization problem for the {0} time", i);
                    await this.ReauthorizeAsync().ConfigureAwait(false);
                    await Task.Delay(TimeSpan.FromSeconds(0.5 + i)).ConfigureAwait(false);
                }
                finally
                {
                    this._reauthorizeLock.Release();
                }
            });
        }
        public async Task <List <BigCommerceCategory> > GetCategoriesAsync(CancellationToken token)
        {
            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 = await ActionPolicyAsync.Handle <Exception>().RetryAsync(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;
                    }

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

                await this.CreateApiDelay(categoriesWithinPage.Limits, token);                  //API requirement

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

                foreach (var product in categoriesWithinPage.Response.Data)
                {
                    var productCatURL = product.Category_URL;

                    categories.Add(new BigCommerceCategory
                    {
                        Id           = product.Id,
                        Category_URL = new BigCommerceCategoryURL()
                        {
                            Url = productCatURL.Url
                        },
                        Category_Name = product.Name
                    });
                }

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

            return(categories);
        }
 public static ActionPolicyAsync GetAsync(string marker, string url)
 {
     return(ActionPolicyAsync.Handle <Exception>().RetryAsync(RetryCount, async(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);
         await Task.Delay(delay);
     }));
 }
        public async Task <List <BigCommerceProduct> > GetProductsAsync(CancellationToken token, 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 = await ActionPolicyAsync.Handle <Exception>().RetryAsync(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;
                    }

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

                await this.CreateApiDelay(productsWithinPage.Limits, token);                 //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;

                    products.Add(new BigCommerceProduct
                    {
                        Id  = product.Id,
                        Sku = product.Sku,
                        InventoryTracking = this.ToCompatibleWithV2InventoryTrackingEnum(product.InventoryTracking),
                        Upc               = product.Upc,
                        Name              = product.Name,
                        Description       = product.Description,
                        Price             = product.Price,
                        SalePrice         = product.SalePrice,
                        RetailPrice       = product.RetailPrice,
                        CostPrice         = product.CostPrice,
                        Weight            = product.Weight,
                        BrandId           = product.BrandId,
                        Quantity          = product.Quantity,
                        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)
            {
                await base.FillWeightUnitAsync(products, token, marker);

                await base.FillBrandsAsync(products, token, marker);
            }

            return(products);
        }