From 76b5e09f722d18924e02759a9afa28ebf9bca356 Mon Sep 17 00:00:00 2001 From: Michael Bucari-Tovo Date: Thu, 23 Jun 2022 21:17:43 -0600 Subject: [PATCH] Add error handling around all ui click handlers for book downloads. --- .../QueryObjects/LibraryBookQueries.cs | 8 ++++ Source/LibationWinForms/Form1.Liberate.cs | 24 ++++++++--- Source/LibationWinForms/Form1.ProcessQueue.cs | 43 +++++++++++-------- Source/LibationWinForms/Form1.VisibleBooks.cs | 21 +++++++-- 4 files changed, 71 insertions(+), 25 deletions(-) diff --git a/Source/DataLayer/QueryObjects/LibraryBookQueries.cs b/Source/DataLayer/QueryObjects/LibraryBookQueries.cs index 2dbd0ddc..823952e6 100644 --- a/Source/DataLayer/QueryObjects/LibraryBookQueries.cs +++ b/Source/DataLayer/QueryObjects/LibraryBookQueries.cs @@ -88,5 +88,13 @@ namespace DataLayer s.Series.AudibleSeriesId == parent.Book.AudibleProductId ) == true ).ToList(); + + public static IEnumerable UnLiberated(this IEnumerable bookList) + => bookList + .Where( + lb => + lb.Book.UserDefinedItem.BookStatus is LiberatedStatus.NotLiberated or LiberatedStatus.PartialDownload + || lb.Book.UserDefinedItem.PdfStatus is LiberatedStatus.NotLiberated or LiberatedStatus.PartialDownload + ); } } diff --git a/Source/LibationWinForms/Form1.Liberate.cs b/Source/LibationWinForms/Form1.Liberate.cs index d9ab6667..256b977e 100644 --- a/Source/LibationWinForms/Form1.Liberate.cs +++ b/Source/LibationWinForms/Form1.Liberate.cs @@ -1,4 +1,5 @@ -using System; +using DataLayer; +using System; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; @@ -10,11 +11,24 @@ namespace LibationWinForms private void Configure_Liberate() { } //GetLibrary_Flat_NoTracking() may take a long time on a hugh library. so run in new thread - private async void beginBookBackupsToolStripMenuItem_Click(object _ = null, EventArgs __ = null) + private void beginBookBackupsToolStripMenuItem_Click(object _ = null, EventArgs __ = null) { - SetQueueCollapseState(false); - await Task.Run(() => processBookQueue1.AddDownloadDecrypt(ApplicationServices.DbContexts.GetLibrary_Flat_NoTracking() - .Where(lb => lb.Book.UserDefinedItem.PdfStatus is DataLayer.LiberatedStatus.NotLiberated || lb.Book.UserDefinedItem.BookStatus is DataLayer.LiberatedStatus.NotLiberated))); + try + { + SetQueueCollapseState(false); + + Serilog.Log.Logger.Information("Begin backing up all library books"); + + processBookQueue1.AddDownloadDecrypt( + ApplicationServices.DbContexts + .GetLibrary_Flat_NoTracking() + .UnLiberated() + ); + } + catch (Exception ex) + { + Serilog.Log.Logger.Error(ex, "An error occurred while backing up all library books"); + } } private async void beginPdfBackupsToolStripMenuItem_Click(object sender, EventArgs e) diff --git a/Source/LibationWinForms/Form1.ProcessQueue.cs b/Source/LibationWinForms/Form1.ProcessQueue.cs index 22d12329..86767aa9 100644 --- a/Source/LibationWinForms/Form1.ProcessQueue.cs +++ b/Source/LibationWinForms/Form1.ProcessQueue.cs @@ -22,27 +22,36 @@ namespace LibationWinForms this.Width = width; } - private void ProductsDisplay_LiberateClicked(object sender, LibraryBook e) + private void ProductsDisplay_LiberateClicked(object sender, LibraryBook libraryBook) { - if (e.Book.UserDefinedItem.BookStatus != LiberatedStatus.Liberated) + try { - SetQueueCollapseState(false); - processBookQueue1.AddDownloadDecrypt(e); - } - else if (e.Book.UserDefinedItem.PdfStatus is not null and LiberatedStatus.NotLiberated) - { - SetQueueCollapseState(false); - processBookQueue1.AddDownloadPdf(e); - } - else if (e.Book.Audio_Exists()) - { - // liberated: open explorer to file - var filePath = AudibleFileStorage.Audio.GetPath(e.Book.AudibleProductId); - if (!Go.To.File(filePath?.ShortPathName)) + if (libraryBook.Book.UserDefinedItem.BookStatus is LiberatedStatus.NotLiberated or LiberatedStatus.PartialDownload) { - var suffix = string.IsNullOrWhiteSpace(filePath) ? "" : $":\r\n{filePath}"; - MessageBox.Show($"File not found" + suffix); + Serilog.Log.Logger.Information("Begin single book backup of {libraryBook}", libraryBook); + SetQueueCollapseState(false); + processBookQueue1.AddDownloadDecrypt(libraryBook); } + else if (libraryBook.Book.UserDefinedItem.PdfStatus is LiberatedStatus.NotLiberated) + { + Serilog.Log.Logger.Information("Begin single pdf backup of {libraryBook}", libraryBook); + SetQueueCollapseState(false); + processBookQueue1.AddDownloadPdf(libraryBook); + } + else if (libraryBook.Book.Audio_Exists()) + { + // liberated: open explorer to file + var filePath = AudibleFileStorage.Audio.GetPath(libraryBook.Book.AudibleProductId); + if (!Go.To.File(filePath?.ShortPathName)) + { + var suffix = string.IsNullOrWhiteSpace(filePath) ? "" : $":\r\n{filePath}"; + MessageBox.Show($"File not found" + suffix); + } + } + } + catch (Exception ex) + { + Serilog.Log.Logger.Error(ex, "An error occurred while handling the stop light button click for {libraryBook}", libraryBook); } } diff --git a/Source/LibationWinForms/Form1.VisibleBooks.cs b/Source/LibationWinForms/Form1.VisibleBooks.cs index e44cadec..b6516870 100644 --- a/Source/LibationWinForms/Form1.VisibleBooks.cs +++ b/Source/LibationWinForms/Form1.VisibleBooks.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; using ApplicationServices; +using DataLayer; using Dinah.Core.Threading; using LibationWinForms.Dialogs; @@ -48,10 +49,24 @@ namespace LibationWinForms }); } - private async void liberateVisible(object sender, EventArgs e) + private void liberateVisible(object sender, EventArgs e) { - SetQueueCollapseState(false); - await Task.Run(() => processBookQueue1.AddDownloadDecrypt(productsDisplay.GetVisible())); + try + { + SetQueueCollapseState(false); + + Serilog.Log.Logger.Information("Begin backing up visible library books"); + + processBookQueue1.AddDownloadDecrypt( + productsDisplay + .GetVisible() + .UnLiberated() + ); + } + catch (Exception ex) + { + Serilog.Log.Logger.Error(ex, "An error occurred while backing up visible library books"); + } } private void replaceTagsToolStripMenuItem_Click(object sender, EventArgs e)