Skip to content

Commit

Permalink
feeder: gracefully handle failure to retrieve the VPS feed
Browse files Browse the repository at this point in the history
  • Loading branch information
stojy committed Sep 20, 2023
1 parent bd4c27c commit fd8506c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 38 deletions.
87 changes: 52 additions & 35 deletions ClrVpin/Feeder/FeederViewModel.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<LocalGame>();
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<LocalGame>();
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<GameItem> gameItems, IList<LocalGame> localGames, Dictionary<string, int> fixStatistics)
Expand Down
10 changes: 8 additions & 2 deletions ClrVpin/Logging/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}";
Expand Down
2 changes: 1 addition & 1 deletion ClrVpin/Merger/MergerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit fd8506c

Please sign in to comment.