From fd8506c266fdf9766bd1f0f766794580d628d2e0 Mon Sep 17 00:00:00 2001 From: stoj Date: Wed, 20 Sep 2023 20:30:20 +0800 Subject: [PATCH] feeder: gracefully handle failure to retrieve the VPS feed --- ClrVpin/Feeder/FeederViewModel.cs | 87 ++++++++++++++++++------------- ClrVpin/Logging/Logger.cs | 10 +++- ClrVpin/Merger/MergerViewModel.cs | 2 +- 3 files changed, 61 insertions(+), 38 deletions(-) diff --git a/ClrVpin/Feeder/FeederViewModel.cs b/ClrVpin/Feeder/FeederViewModel.cs index 3996e19e..4fdd5970 100644 --- a/ClrVpin/Feeder/FeederViewModel.cs +++ b/ClrVpin/Feeder/FeederViewModel.cs @@ -1,12 +1,14 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net.Http; using System.Text.Json; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using ClrVpin.Controls; using ClrVpin.Extensions; +using ClrVpin.Home; using ClrVpin.Logging; using ClrVpin.Models.Feeder; using ClrVpin.Models.Settings; @@ -103,54 +105,69 @@ private void FixFeedOptionSelected() private async void Start() { - Logger.Info($"Feeder started, settings={JsonSerializer.Serialize(Settings)}"); + ProgressViewModel progress = null; - _window.Hide(); - Logger.Clear(); + try + { + Logger.Info($"Feeder started, settings={JsonSerializer.Serialize(Settings)}"); - var progress = new ProgressViewModel("Feeding"); - progress.Show(_window); + progress = new ProgressViewModel("Feeding"); - var localGames = new List(); - if (MatchFuzzy.IsActive) - { - try - { - progress.Update("Loading database"); - localGames = await DatabaseUtils.ReadGamesFromDatabases(Settings.GetFixableContentTypes()); - Logger.Info($"Loading database complete, duration={progress.Duration}", true); - } - catch (Exception) + _window.Hide(); + Logger.Clear(); + + progress.Show(_window); + + var localGames = new List(); + if (MatchFuzzy.IsActive) { - progress.Close(); - _window.TryShow(); - return; + try + { + progress.Update("Loading database"); + localGames = await DatabaseUtils.ReadGamesFromDatabases(Settings.GetFixableContentTypes()); + Logger.Info($"Loading database complete, duration={progress.Duration}", true); + } + catch (Exception) + { + progress.Close(); + _window.TryShow(); + return; + } } - } - progress.Update("Fetching online database"); - var onlineGames = await FeederUtils.ReadGamesFromOnlineDatabase(); + progress.Update("Fetching online database"); + var onlineGames = await FeederUtils.ReadGamesFromOnlineDatabase(); - progress.Update("Fixing online database"); - var feedFixStatistics = FeederFix.FixOnlineDatabase(onlineGames); - Logger.Info($"Loading online database complete, duration={progress.Duration}", true); + progress.Update("Fixing online database"); + var feedFixStatistics = FeederFix.FixOnlineDatabase(onlineGames); + Logger.Info($"Loading online database complete, duration={progress.Duration}", true); - progress.Update("Matching online to local database(s)"); - await FeederUtils.MatchOnlineToLocalAsync(localGames, onlineGames, UpdateProgress); - Logger.Info($"Matching local and online databases complete, duration={progress.Duration}", true); + progress.Update("Matching online to local database(s)"); + await FeederUtils.MatchOnlineToLocalAsync(localGames, onlineGames, UpdateProgress); + Logger.Info($"Matching local and online databases complete, duration={progress.Duration}", true); - progress.Update("Matching local to online database"); - var gameItems = await FeederUtils.MergeOnlineAndLocalGamesAsync(localGames, onlineGames, UpdateProgress); - Logger.Info($"Matching local and online databases complete, duration={progress.Duration}", true); + progress.Update("Matching local to online database"); + var gameItems = await FeederUtils.MergeOnlineAndLocalGamesAsync(localGames, onlineGames, UpdateProgress); + Logger.Info($"Matching local and online databases complete, duration={progress.Duration}", true); - progress.Update("Preparing Results"); + progress.Update("Preparing Results"); - progress.Close(); + progress.Close(); - await ShowResults(progress.Duration, gameItems, localGames, feedFixStatistics); - Logger.Info($"Feeder rendered, duration={progress.Duration}", true); + await ShowResults(progress.Duration, gameItems, localGames, feedFixStatistics); + Logger.Info($"Feeder rendered, duration={progress.Duration}", true); - void UpdateProgress(string detail, int current, int total) => progress.Update(null, null, detail, current, total); + void UpdateProgress(string detail, int current, int total) => progress.Update(null, null, detail, current, total); + } + catch (HttpRequestException e) + { + progress?.Close(); + Logger.Warn(e, "Feeder"); + + await Notification.ShowWarning(HomeWindow.HomeDialogHost, "Feeder Was Unsuccessful", "Unable to access the internet. Please check your internet connection and then try again.", + $"{Logger.GetLogs(1)}", showCloseButton:true); + _window.Close(); + } } private async Task ShowResults(TimeSpan duration, IList gameItems, IList localGames, Dictionary fixStatistics) diff --git a/ClrVpin/Logging/Logger.cs b/ClrVpin/Logging/Logger.cs index 1ba3caaf..1782707d 100644 --- a/ClrVpin/Logging/Logger.cs +++ b/ClrVpin/Logging/Logger.cs @@ -74,16 +74,22 @@ public static void Warn(string message, bool isDiagnostic = false) Add(Level.Warn, message); } + public static void Warn(Exception exception, string message) + { + _logger.Warn(exception, FormatErrorMessage(message)); + Add(Level.Warn, $"{message}\n{exception}", sync: true); + } + public static void Error(Exception exception, string message) { _logger.Error(exception, FormatErrorMessage(message)); - Add(Level.Error, $"{message}\n{exception}"); + Add(Level.Error, $"{message}\n{exception}", sync: true); } public static void Error(string message) { _logger.Error(FormatErrorMessage(message)); - Add(Level.Error, message); + Add(Level.Error, message, sync: true); } private static string FormatErrorMessage(string message) => $"\n******* ERROR *******\n{message}"; diff --git a/ClrVpin/Merger/MergerViewModel.cs b/ClrVpin/Merger/MergerViewModel.cs index 6a31b629..d9fb3e6f 100644 --- a/ClrVpin/Merger/MergerViewModel.cs +++ b/ClrVpin/Merger/MergerViewModel.cs @@ -227,7 +227,7 @@ private async void Start() { progress?.Close(); Logger.Error(e, "Merger"); - await Notification.ShowWarning(HomeWindow.HomeDialogHost, "Merger Was Unsuccessful", "Access Error: please check that the folder is not read only and then try again.", + await Notification.ShowWarning(HomeWindow.HomeDialogHost, "Merger Was Unsuccessful", "Access Error: please check that the folder is not read only and try again.", $"{Logger.GetLogs(1)}", showCloseButton:true); _window.Close(); }