/// <summary> /// Downloads and installs updates /// </summary> /// <param name="dispatcher"></param> /// <param name="updates">Update information</param> private void DownloadUpdates(Dispatcher dispatcher, UpdateInfo updates) { Status = UpdateStatus.DownloadingUpdates; dispatcher.Invoke(() => OnUpdateStatusChanged?.Invoke(this, Status)); Log.WriteLine(LogLevel.Verbose, "downloading updates"); Manager.DownloadReleases(updates.ReleasesToApply, p => { Log.WriteLine(LogLevel.Debug, $"downloading updates ({p}%)"); dispatcher.Invoke(() => OnUpdateProgressChanged?.Invoke(this, Status, p)); }) .ContinueWith(t => { Status = UpdateStatus.Idle; dispatcher.Invoke(() => OnUpdateStatusChanged?.Invoke(this, Status)); if (t.IsFaulted || t.IsCanceled) { Log.WriteLine(LogLevel.Warning, $"update downloader task canceled or failed - {t.Exception}"); return; } Log.WriteLine(LogLevel.Verbose, $"downloaded {updates.ReleasesToApply.Sum(r => r.Filesize) / 1024 / 1024}MiB"); ApplyUpdates(dispatcher, updates); }); }
private void ApplyUpdates(Dispatcher dispatcher, UpdateInfo updates) { Status = UpdateStatus.ApplyingUpdates; dispatcher.Invoke(() => OnUpdateStatusChanged?.Invoke(this, Status)); Log.WriteLine(LogLevel.Verbose, "applying updates"); Manager.ApplyReleases(updates, p => { Log.WriteLine(LogLevel.Debug, $"applying updates ({p}%)"); dispatcher.Invoke(() => OnUpdateProgressChanged?.Invoke(this, Status, p)); }) .ContinueWith(t => { if (t.IsFaulted || t.IsCanceled) { Status = UpdateStatus.Idle; Log.WriteLine(LogLevel.Warning, $"update install task canceled or failed - {t.Exception}"); } else { Status = UpdateStatus.ReadyToRestart; Log.WriteLine(LogLevel.Verbose, $"successfully applied {updates.ReleasesToApply.Count} update(s)"); } dispatcher.Invoke(() => OnUpdateStatusChanged?.Invoke(this, Status)); // TODO: restart the app automatically if it's been idle for some time }); }
/// <summary> /// Silently checks for updates if we are allowed to /// </summary> /// <param name="dispatcher"></param> private void CheckForUpdates(Dispatcher dispatcher) { Status = UpdateStatus.CheckingForUpdates; dispatcher.Invoke(() => OnUpdateStatusChanged?.Invoke(this, Status)); Log.WriteLine(LogLevel.Verbose, "checking for updates"); Manager.CheckForUpdate(progress: p => { Log.WriteLine(LogLevel.Debug, $"checking for updates ({p}%)"); dispatcher.Invoke(() => OnUpdateProgressChanged?.Invoke(this, Status, p)); }) .ContinueWith(t => { Status = UpdateStatus.Idle; dispatcher.Invoke(() => OnUpdateStatusChanged?.Invoke(this, Status)); if (t.IsFaulted || t.IsCanceled) { Log.WriteLine(LogLevel.Warning, $"update checker task canceled or failed - {t.Exception}"); return; } if (t.Result.ReleasesToApply.Any()) { Log.WriteLine(LogLevel.Verbose, $"found {t.Result.ReleasesToApply.Count} update(s)"); if (Application.Options.UpdatePolicy == UpdatePolicy.Automatic ) { DownloadUpdates(dispatcher, t.Result); } else if ( dispatcher.Invoke(() => UpdaterUiHelper.ShowPromptDialog(t.Result))) { DownloadUpdates(dispatcher, t.Result); dispatcher.Invoke(UpdaterUiHelper.ShowProgressDialog); } else { Log.WriteLine(LogLevel.Verbose, "operation cancelled by the user"); } } }); }