Some minor refactors on modlist downloading

Metrics send in parallel.  Exceptions caught/rethrown on tcs
This commit is contained in:
Justin Swanson 2020-01-18 13:50:40 -06:00
parent 80508aa494
commit 32bb4cd8e9

View File

@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
@ -31,7 +31,7 @@ namespace Wabbajack
private readonly ObservableAsPropertyHelper<bool> _Exists;
public bool Exists => _Exists.Value;
public string Location => Path.Combine(Consts.ModListDownloadFolder, Metadata.Links.MachineURL + ExtensionManager.Extension);
public string Location { get; }
[Reactive]
public double ProgressPercent { get; private set; }
@ -52,6 +52,7 @@ namespace Wabbajack
{
_parent = parent;
Metadata = metadata;
Location = Path.Combine(Consts.ModListDownloadFolder, Metadata.Links.MachineURL + ExtensionManager.Extension);
IsBroken = metadata.ValidationSummary.HasFailures;
OpenWebsiteCommand = ReactiveCommand.Create(() => Process.Start($"https://www.wabbajack.org/modlist/{Metadata.Links.MachineURL}"));
ExecuteCommand = ReactiveCommand.CreateFromObservable<Unit, Unit>(
@ -136,24 +137,33 @@ namespace Wabbajack
.ToGuiProperty(this, nameof(LoadingImage));
}
private Task Download()
private async Task Download()
{
ProgressPercent = 0d;
var queue = new WorkQueue(1);
var sub = queue.Status.Select(i => i.ProgressPercent)
.Subscribe(percent => ProgressPercent = percent);
TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
var metric = Metrics.Send(Metrics.Downloading, Metadata.Title);
queue.QueueTask(async () =>
using (var queue = new WorkQueue(1))
using (queue.Status.Select(i => i.ProgressPercent)
.Subscribe(percent => ProgressPercent = percent))
{
var downloader = DownloadDispatcher.ResolveArchive(Metadata.Links.Download);
await downloader.Download(new Archive{ Name = Metadata.Title, Size = Metadata.DownloadMetadata?.Size ?? 0}, Location);
Location.FileHashCached();
sub.Dispose();
tcs.SetResult(true);
});
var tcs = new TaskCompletionSource<bool>();
queue.QueueTask(async () =>
{
try
{
var downloader = DownloadDispatcher.ResolveArchive(Metadata.Links.Download);
await downloader.Download(new Archive { Name = Metadata.Title, Size = Metadata.DownloadMetadata?.Size ?? 0 }, Location);
Location.FileHashCached();
tcs.SetResult(true);
}
catch (Exception ex)
{
tcs.SetException(ex);
}
});
return tcs.Task;
await Task.WhenAll(
tcs.Task,
Metrics.Send(Metrics.Downloading, Metadata.Title));
}
}
}
}