Ejemplo n.º 1
0
        public bool WriteStream(Stream stream, string path)
        {
            if (!stream.CanRead)
            {
                return(false);
            }

            bool LocalRun()
            {
                stream.Seek(0, SeekOrigin.Begin);

                using (var fileStream = new FileStream(path,
                                                       FileMode.Create,
                                                       FileAccess.Write, FileShare.ReadWrite,
                                                       4096,
                                                       FileOptions.Asynchronous))
                {
                    stream.CopyTo(fileStream);
                    fileStream.Dispose();
                }

                stream.Dispose();
                return(true);
            }

            return(RetryHelper.Do(LocalRun, TimeSpan.FromSeconds(1)));
        }
Ejemplo n.º 2
0
        public Task <OrderChange> GetOrderStatus(Order order, CancellationToken token = new CancellationToken())
        {
            return(RetryHelper.Do(async() =>
            {
                if (order == null)
                {
                    throw new ArgumentNullException(nameof(order), "CryptsyApi GetOrderStatus method.");
                }

                if (!string.IsNullOrEmpty(order.Id))
                {
                    var orderStatus = await GetCryptsyOrderStatus(order, token);
                    var avgPrice = orderStatus.TradeInfo != null && orderStatus.TradeInfo.Count > 0 ? orderStatus.TradeInfo.Average(x => x.UnitPrice) : 0m;
                    var totalFilledAmount = orderStatus.TradeInfo != null && orderStatus.TradeInfo.Count > 0 ? orderStatus.TradeInfo.Average(x => x.Quantity) : 0;

                    if (!orderStatus.Active && orderStatus.RemainQty == 0m && avgPrice != 0m && totalFilledAmount == order.Amount)
                    {
                        return new OrderChange(order, order.Amount, avgPrice, OrderStatus.Filled, DateTime.UtcNow, order.Amount);
                    }

                    if (orderStatus.Active && avgPrice != 0m && totalFilledAmount < order.Amount)
                    {
                        return new OrderChange(order, 0, avgPrice, OrderStatus.PartiallyFilled, DateTime.UtcNow, totalFilledAmount);
                    }
                }

                return new OrderChange(order, 0, 0, OrderStatus.Unknown, DateTime.UtcNow);
            }, TimeSpan.FromMilliseconds(Constant.DefaultRetryInterval), 1));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Preflight + the upload to the service
        /// </summary>
        /// <param name="parentDirectory"></param>
        /// <param name="slug">name</param>
        /// <param name="copyThisFilesSubPaths">list of files (subPath style)</param>
        /// <returns>false = fail</returns>
        internal bool MakeUpload(string parentDirectory, string slug, IEnumerable <string> copyThisFilesSubPaths)
        {
            foreach (var item in copyThisFilesSubPaths)
            {
                const string pathDelimiter = "/";
                var          toFtpPath     = PathHelper.RemoveLatestSlash(_webFtpNoLogin) + pathDelimiter +
                                             _appSettings.GenerateSlug(slug, true) + pathDelimiter +
                                             item;

                _console.Write(".");

                bool LocalUpload()
                {
                    return(Upload(parentDirectory, item, toFtpPath));
                }

                RetryHelper.Do(LocalUpload, TimeSpan.FromSeconds(10));

                if (_storage.ExistFile(parentDirectory + item))
                {
                    continue;
                }
                _console.WriteLine($"Fail > upload file => {item} {toFtpPath}");
                return(false);
            }
            return(true);
        }
Ejemplo n.º 4
0
        public void RetryFail_expect_ArgumentOutOfRangeException()
        {
            bool Test()
            {
                return(true);
            }

            // should not be negative
            RetryHelper.Do(Test, TimeSpan.Zero, 0);
        }
Ejemplo n.º 5
0
        public bool FileDelete(string path)
        {
            if (!File.Exists(path))
            {
                return(false);
            }
            bool LocalRun()
            {
                File.Delete(path);
                return(true);
            }

            return(RetryHelper.Do(LocalRun, TimeSpan.FromSeconds(2), 5));
        }
Ejemplo n.º 6
0
        public void TakeScreenshotAndGenerateThumbnail(string url, string localPath)
        {
            this.EnsureParameters(url, localPath);

            try
            {
                RetryHelper.Do(() => this.TakeScreenshotAndGenerateThumbnailInternal(url, localPath), this.ShouldRetry);
            }
            catch (Exception ex)
            {
                var exception = new WebScreenshotTakerAppException(ex);

                Log4netHelper.All(x => x.Error($"{exception.ToString()}"));

                throw exception;
            }
        }
Ejemplo n.º 7
0
        public CryptsyMarket[] GetMarkets(CancellationToken token = default(CancellationToken))
        {
            return(RetryHelper.Do(() =>
            {
                string content = PrivateQuery(new Dictionary <string, string> {
                    { "method", "getmarkets" }
                }, token).Result;
                JObject o = JObject.Parse(content);
                if (int.Parse(o["success"].ToString()) != 1)
                {
                    throw new SyntaxErrorException(o["error"].ToString());
                }

                var deserialized = JsonConvert.DeserializeObject <CryptsyMarket[]>(o["return"].ToString());
                return deserialized;
            }, TimeSpan.FromMilliseconds(Constant.DefaultRetryInterval)));
        }
Ejemplo n.º 8
0
        public void RetryFail_expect_AggregateException()
        {
            var count = 0;

            bool Test()
            {
                if (count == 2)
                {
                    throw new FormatException();                     // <= does combine it with AggregateException
                }
                count++;
                throw new ApplicationException();
            }

            var result = RetryHelper.Do(Test, TimeSpan.Zero);

            Assert.IsTrue(result);
        }
Ejemplo n.º 9
0
        public void RetrySucceed()
        {
            var count = 0;

            bool Test()
            {
                count++;
                if (count == 2)
                {
                    return(true);
                }
                throw new ApplicationException();
            }

            var result = RetryHelper.Do(Test, TimeSpan.Zero);

            Assert.IsTrue(result);
        }
Ejemplo n.º 10
0
        public async Task <T> PrivateQuery <T>(string url, Dictionary <string, string> parameters, CancellationToken token = default(CancellationToken))
        {
            string resultData = String.Empty;

            return(await Task.Run(() =>
            {
                return RetryHelper.Do(() =>
                {
                    try
                    {
                        resultData = PrivateQuery(url, parameters, token).Result;
                        return JsonConvert.DeserializeObject <T>(resultData);
                    }
                    catch (Exception ex)
                    {
                        //Log.Error("HitBtc: Can't parse json {0} to BitFinex<{1}>, URL - {2}, Exception Message - {3}", resultData, typeof(T), url, ex.Message);
                        throw;
                    }
                }, TimeSpan.FromMilliseconds(0), 10);
            }, token));
        }