diff --git a/src/KKManager.Updater/Downloader/UpdateDownloadCoordinator.cs b/src/KKManager.Updater/Downloader/UpdateDownloadCoordinator.cs index 2488274..e33ebd9 100644 --- a/src/KKManager.Updater/Downloader/UpdateDownloadCoordinator.cs +++ b/src/KKManager.Updater/Downloader/UpdateDownloadCoordinator.cs @@ -51,21 +51,36 @@ public static UpdateDownloadCoordinator Create(IEnumerable updateTas public async Task RunUpdate(CancellationToken cancellationToken) { - // One thread per server - var allSources = _updateItems - .SelectMany(x => x.DownloadSources.Keys) - .Distinct(); - - var runningTasks = new List>(); - foreach (var updateSource in allSources) + try { - var updateSourceInfo = new DownloadSourceInfo(updateSource); - var task = Task.Run(async () => await UpdateThread(updateSourceInfo, cancellationToken), - cancellationToken); - runningTasks.Add(new Tuple(task, updateSourceInfo)); + // One thread per server + var allSources = _updateItems + .SelectMany(x => x.DownloadSources.Keys) + .Distinct(); + + var runningTasks = new List>(); + foreach (var updateSource in allSources) + { + var updateSourceInfo = new DownloadSourceInfo(updateSource); + var task = Task.Run(async () => await UpdateThread(updateSourceInfo, cancellationToken), + cancellationToken); + runningTasks.Add(new Tuple(task, updateSourceInfo)); + } + + await Task.WhenAll(runningTasks.Select(x => x.Item1)); + + cancellationToken.ThrowIfCancellationRequested(); } + catch (OperationCanceledException) + { + foreach (var updateItem in _updateItems) + { + if(updateItem.Status == UpdateDownloadStatus.Downloading || updateItem.Status == UpdateDownloadStatus.Waiting) + updateItem.MarkAsCancelled(); + } - await Task.WhenAll(runningTasks.Select(x => x.Item1)); + throw; + } } /// @@ -121,6 +136,8 @@ await RetryHelper.RetryOnExceptionAsync( { if (e is OperationCanceledException) { + currentDownloadItem.MarkAsCancelled(e); + if (cancellationToken.IsCancellationRequested) return; else diff --git a/src/KKManager.Updater/Downloader/UpdateDownloadItem.cs b/src/KKManager.Updater/Downloader/UpdateDownloadItem.cs index 604a708..9af3d9f 100644 --- a/src/KKManager.Updater/Downloader/UpdateDownloadItem.cs +++ b/src/KKManager.Updater/Downloader/UpdateDownloadItem.cs @@ -63,5 +63,11 @@ public void TryMarkSourceAsFailed(UpdateSourceBase source, Exception exception) } } } + + public void MarkAsCancelled(Exception cancelException = null) + { + Status = UpdateDownloadStatus.Cancelled; + if (cancelException != null) Exceptions.Add(cancelException); + } } } \ No newline at end of file diff --git a/src/KKManager.Updater/Downloader/UpdateDownloadStatus.cs b/src/KKManager.Updater/Downloader/UpdateDownloadStatus.cs index c6f4b47..abb815e 100644 --- a/src/KKManager.Updater/Downloader/UpdateDownloadStatus.cs +++ b/src/KKManager.Updater/Downloader/UpdateDownloadStatus.cs @@ -5,6 +5,7 @@ internal enum UpdateDownloadStatus Downloading, Waiting, Finished, - Failed + Failed, + Cancelled } } \ No newline at end of file