Skip to content

Commit

Permalink
Added cancelled download state to download list
Browse files Browse the repository at this point in the history
  • Loading branch information
ManlyMarco committed Dec 1, 2020
1 parent 1ff2a22 commit 0bcf231
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
41 changes: 29 additions & 12 deletions src/KKManager.Updater/Downloader/UpdateDownloadCoordinator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,36 @@ public static UpdateDownloadCoordinator Create(IEnumerable<UpdateTask> updateTas

public async Task RunUpdate(CancellationToken cancellationToken)
{
// One thread per server
var allSources = _updateItems
.SelectMany(x => x.DownloadSources.Keys)
.Distinct();

var runningTasks = new List<Tuple<Task, DownloadSourceInfo>>();
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, DownloadSourceInfo>(task, updateSourceInfo));
// One thread per server
var allSources = _updateItems
.SelectMany(x => x.DownloadSources.Keys)
.Distinct();

var runningTasks = new List<Tuple<Task, DownloadSourceInfo>>();
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, DownloadSourceInfo>(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;
}
}

/// <summary>
Expand Down Expand Up @@ -121,6 +136,8 @@ await RetryHelper.RetryOnExceptionAsync(
{
if (e is OperationCanceledException)
{
currentDownloadItem.MarkAsCancelled(e);

if (cancellationToken.IsCancellationRequested)
return;
else
Expand Down
6 changes: 6 additions & 0 deletions src/KKManager.Updater/Downloader/UpdateDownloadItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
3 changes: 2 additions & 1 deletion src/KKManager.Updater/Downloader/UpdateDownloadStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ internal enum UpdateDownloadStatus
Downloading,
Waiting,
Finished,
Failed
Failed,
Cancelled
}
}

0 comments on commit 0bcf231

Please sign in to comment.