private async Task downloadAll(ImageDownloadQueue queue, IDownloader downloader, CancellationToken token) { while (true) { if (_abort) { return; } var task = queue.PopTaskFor(downloader); if (task == null) { return; } if (isAlreadyDownloaded(task)) { Console.WriteLine("[Skip] {0} {1}", task.QualityGroup.Name ?? string.Empty, task.Dir.Subdir); Interlocked.Add(ref _countInDownloadedDirs, task.FilesOnline.Count); ProgressChanged?.Invoke(); } else { Interlocked.Add(ref _countInDownloadedDirs, task.FilesDownloaded.Count); ProgressChanged?.Invoke(); bool success = await downloader.Download(task, token); if (success) { ImageDownloadProgressReader.WriteExistingSignatures(task); } else { if (queue.PushFailedTaskBack(downloader, task)) { Console.WriteLine("Other download source available for {0}", task.Dir.Subdir); } else { Console.WriteLine("No other download source available for {0}", task.Dir.Subdir); } } } } }
public async Task Download(string quality, IReadOnlyList <ImageDownloadProgress> allProgress, CancellationToken token) { _abort = false; var megaDownloader = new MegaDownloader(_megatools, _syncOutput); var yandexDownloader = new YandexDownloader(_syncOutput, new YandexDiskClient()); var downloaders = new List <IDownloader> { megaDownloader, yandexDownloader, }; var tasks = allProgress.Where(_ => Str.Equals(_.QualityGroup.Quality, quality)).ToArray(); deleteDuplicateSubdirs(tasks); var queue = new ImageDownloadQueue(_repository, downloaders, tasks); Console.WriteLine("Found {0} directories for quality '{1}' in configuration", queue.Count, quality); TotalCount = queue.TotalOnlineFilesCount; _countInDownloadedDirs = 0; ProgressChanged?.Invoke(); yandexDownloader.ProgressChanged += handleYandexProgress; _megatools.FileDownloaded += megaFileDownloaded; await Task.WhenAll(downloaders.Select(d => token.Run(tkn => downloadAll(queue, d, tkn)))); _megatools.FileDownloaded -= megaFileDownloaded; yandexDownloader.ProgressChanged -= handleYandexProgress; void megaFileDownloaded() { Interlocked.Increment(ref _countInDownloadedDirs); ProgressChanged?.Invoke(); } void handleYandexProgress(ImageDownloadProgress task) { Interlocked.Add(ref _countInDownloadedDirs, task.FilesOnline.Count - task.FilesDownloaded.Count); ProgressChanged?.Invoke(); } }