public void Download(CancellationToken cancellationToken) { Assert.MethodCalledOnlyOnce(ref _downloadHasBeenCalled, "Download"); DebugLogger.Log("Downloading."); List <ResourceUrl> validUrls = new List <ResourceUrl>(_resource.ResourceUrls); // 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) { ResourceUrl url = validUrls[i]; try { OpenFileStream(); Download(url, cancellationToken); CloseFileStream(); var validator = new DownloadedResourceValidator(); validator.Validate(_destinationFilePath, _resource); return; } catch (WebException e) { // Isn't this catching too much? DebugLogger.LogException(e); // try again break; } 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..."); Thread.Sleep(10000); } if (retry <= 0) { throw new DownloaderException("Too many retries, aborting.", DownloaderExceptionStatus.Other); } throw new DownloaderException("Cannot download resource.", DownloaderExceptionStatus.Other); }
public void Download(CancellationToken cancellationToken) { Assert.MethodCalledOnlyOnce(ref _downloadHasBeenCalled, "Download"); DebugLogger.Log("Downloading."); var validUrls = new List <string>(_mirrorUrls); 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..."); Thread.Sleep(10000); } if (retry <= 0) { throw new DownloaderException("Too many retries, aborting.", DownloaderExceptionStatus.Other); } throw new DownloaderException("Cannot download resource.", DownloaderExceptionStatus.Other); }