Exemplo n.º 1
0
        public void Download(CancellationToken cancellationToken)
        {
            try
            {
                _logger.LogDebug("Downloading...");
                _logger.LogTrace("size = " + _size);
                for (int i = 0; i < _urls.Length; i++)
                {
                    _logger.LogTrace("urls[" + i + "].Url = " + _urls[i].Url);
                    _logger.LogTrace("urls[" + i + "].Country = " + _urls[i].Country);
                    _logger.LogTrace("urls[" + i + "].PartSize = " + _urls[i].PartSize);
                }

                _logger.LogTrace("chunksData.ChunkSize = " + _chunksData.ChunkSize);
                _logger.LogTrace("chunksData.Chunks.Length = " + _chunksData.Chunks.Length);

                Assert.MethodCalledOnlyOnce(ref _downloadHasBeenCalled, "Download");

                using (var fileStream = OpenFileStream(cancellationToken))
                {
                    bool retry;

                    do
                    {
                        bool success =
                            _urls.Any(url => TryDownload(url, fileStream, cancellationToken));

                        if (success)
                        {
                            retry = false;
                        }
                        else
                        {
                            _logger.LogWarning("All server requests have failed. Checking if retry is possible...");
                            _retryStrategy.OnRequestFailure();
                            _timeoutCalculator.OnRequestFailure();
                            retry = _retryStrategy.ShouldRetry;

                            if (!retry)
                            {
                                throw new DownloadFailureException("Download failure.");
                            }

                            _logger.LogDebug(string.Format("Retry is possible. Waiting {0}ms until before attempt...",
                                                           _retryStrategy.DelayBeforeNextTry));
                            Threading.CancelableSleep(_retryStrategy.DelayBeforeNextTry, cancellationToken);
                            _logger.LogDebug("Trying to download data once again from each server...");
                        }
                    } while (retry);
                }

                _logger.LogDebug("Downloading finished.");
            }
            catch (Exception e)
            {
                _logger.LogDebug("Download has failed.", e);
                throw;
            }
        }
Exemplo n.º 2
0
        public static void TryExecute(Action action, IRequestRetryStrategy retryStrategy, PatchKit.Unity.Patcher.Cancellation.CancellationToken cancellationToken)
        {
            do
            {
                try
                {
                    action();
                    return;
                }
                catch (IOException e)
                {
                    retryStrategy.OnRequestFailure();

                    if (!retryStrategy.ShouldRetry)
                    {
                        DebugLogger.LogError(string.Format("An IO Exception has occured: {0}. rethrowing.", e));
                        throw;
                    }

                    DebugLogger.LogWarning(string.Format("An IO Exception has occured: {0}. retrying...", e));
                    Threading.CancelableSleep(retryStrategy.DelayBeforeNextTry, cancellationToken);
                }
            } while (retryStrategy.ShouldRetry);
        }
Exemplo n.º 3
0
        public void Download(CancellationToken cancellationToken)
        {
            Assert.MethodCalledOnlyOnce(ref _downloadHasBeenCalled, "Download");

            DebugLogger.Log("Downloading.");

            var validUrls = new List <string>(_mirrorUrls);

            // getting through urls list backwards, because urls may be removed during the process,
            // and it's easier to iterate that way
            validUrls.Reverse();

            int retry = RetriesAmount;

            while (validUrls.Count > 0 && retry > 0)
            {
                for (int i = validUrls.Count - 1; i >= 0 && retry-- > 0; --i)
                {
                    string url = validUrls[i];

                    try
                    {
                        OpenFileStream();

                        Download(url, cancellationToken);

                        CloseFileStream();

                        if (_resource.HasValue)
                        {
                            var validator = new DownloadedResourceValidator();
                            validator.Validate(_destinationFilePath, _resource.Value);
                        }

                        return;
                    }
                    catch (DownloadedResourceValidationException validationException)
                    {
                        DebugLogger.LogException(validationException);
                        validUrls.Remove(url);
                    }
                    catch (DownloaderException downloaderException)
                    {
                        DebugLogger.LogException(downloaderException);
                        switch (downloaderException.Status)
                        {
                        case DownloaderExceptionStatus.EmptyStream:
                            // try another one
                            break;

                        case DownloaderExceptionStatus.CorruptData:
                            // try another one
                            break;

                        case DownloaderExceptionStatus.NotFound:
                            // remove url and try another one
                            validUrls.Remove(url);
                            break;

                        case DownloaderExceptionStatus.Other:
                            // try another one
                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                        }
                    }
                    finally
                    {
                        CloseFileStream();
                    }
                }

                DebugLogger.Log("Waiting 10 seconds before trying again...");
                Threading.CancelableSleep(10000, cancellationToken);
            }

            if (retry <= 0)
            {
                throw new DownloaderException("Too many retries, aborting.", DownloaderExceptionStatus.Other);
            }

            throw new DownloaderException("Cannot download resource.", DownloaderExceptionStatus.Other);
        }