More process control refinements

This commit is contained in:
Michael Bucari-Tovo 2021-08-11 21:07:07 -06:00
parent 687591e08e
commit 1c239dc546
8 changed files with 30 additions and 94 deletions

View File

@ -1,62 +0,0 @@
using System;
using System.Threading.Tasks;
using DataLayer;
using Dinah.Core.ErrorHandling;
namespace FileLiberator
{
/// <summary>
/// Download DRM book and decrypt audiobook files
///
/// Processes:
/// Download: download aax file: the DRM encrypted audiobook
/// Decrypt: remove DRM encryption from audiobook. Store final book
/// Backup: perform all steps (downloaded, decrypt) still needed to get final book
/// </summary>
public class BackupBook : IProcessable
{
public event EventHandler<LibraryBook> Begin;
public event EventHandler<string> StatusUpdate;
public event EventHandler<LibraryBook> Completed;
public DownloadDecryptBook DownloadDecryptBook { get; }
public DownloadPdf DownloadPdf { get; }
public BackupBook(DownloadDecryptBook downloadDecryptBook, DownloadPdf downloadPdf)
{
DownloadDecryptBook = downloadDecryptBook;
DownloadPdf = downloadPdf;
}
public bool Validate(LibraryBook libraryBook)
=> !ApplicationServices.TransitionalFileLocator.Audio_Exists(libraryBook.Book);
// do NOT use ConfigureAwait(false) on ProcessAsync()
// often calls events which prints to forms in the UI context
public async Task<StatusHandler> ProcessAsync(LibraryBook libraryBook)
{
Begin?.Invoke(this, libraryBook);
try
{
{
var statusHandler = await DownloadDecryptBook.TryProcessAsync(libraryBook);
if (statusHandler.HasErrors)
return statusHandler;
}
{
var statusHandler = await DownloadPdf.TryProcessAsync(libraryBook);
if (statusHandler.HasErrors)
return statusHandler;
}
return new StatusHandler();
}
finally
{
Completed?.Invoke(this, libraryBook);
}
}
}
}

View File

@ -13,7 +13,7 @@
<!-- <PublishSingleFile>true</PublishSingleFile> -->
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<Version>5.4.9.239</Version>
<Version>5.4.9.252</Version>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">

View File

@ -1,9 +1,5 @@
using DataLayer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LibationWinForms.BookLiberation
{
@ -16,12 +12,12 @@ namespace LibationWinForms.BookLiberation
#region IProcessable event handler overrides
public override void OnBegin(object sender, LibraryBook libraryBook)
{
InfoLogAction($"Convert Step, Begin: {libraryBook.Book}");
LogMe.Info($"Convert Step, Begin: {libraryBook.Book}");
base.OnBegin(sender, libraryBook);
}
public override void OnCompleted(object sender, LibraryBook libraryBook)
=> InfoLogAction($"Convert Step, Completed: {libraryBook.Book}{Environment.NewLine}");
=> LogMe.Info($"Convert Step, Completed: {libraryBook.Book}{Environment.NewLine}");
#endregion
}

View File

@ -12,12 +12,12 @@ namespace LibationWinForms.BookLiberation
#region IProcessable event handler overrides
public override void OnBegin(object sender, LibraryBook libraryBook)
{
InfoLogAction($"Download & Decrypt Step, Begin: {libraryBook.Book}");
LogMe.Info($"Download & Decrypt Step, Begin: {libraryBook.Book}");
base.OnBegin(sender, libraryBook);
}
public override void OnCompleted(object sender, LibraryBook libraryBook)
=> InfoLogAction($"Download & Decrypt Step, Completed: {libraryBook.Book}{Environment.NewLine}");
=> LogMe.Info($"Download & Decrypt Step, Completed: {libraryBook.Book}{Environment.NewLine}");
#endregion
}

View File

@ -19,9 +19,9 @@ namespace LibationWinForms.BookLiberation
private string narratorNames;
#region ProcessBaseForm overrides
public override void SetProcessable(IStreamable streamProcessable, Action<string> infoLog)
public override void SetProcessable(IStreamable streamProcessable, LogMe logMe)
{
base.SetProcessable(streamProcessable, infoLog);
base.SetProcessable(streamProcessable, logMe);
if (Streamable is not null && Streamable is IAudioDecodable audioDecodable)
{
@ -35,7 +35,6 @@ namespace LibationWinForms.BookLiberation
Disposed += OnUnsubscribeAll;
}
}
#endregion

View File

@ -8,10 +8,10 @@ namespace LibationWinForms.BookLiberation
{
public class ProcessBaseForm : StreamBaseForm
{
protected Action<string> InfoLogAction { get; private set; }
public virtual void SetProcessable(IStreamable streamable, Action<string> infoLog)
protected LogMe LogMe { get; private set; }
public virtual void SetProcessable(IStreamable streamable, LogMe logMe)
{
InfoLogAction = infoLog;
LogMe = logMe;
SetStreamable(streamable);
if (Streamable is not null && Streamable is IProcessable processable)
@ -38,9 +38,9 @@ namespace LibationWinForms.BookLiberation
}
#region IProcessable event handlers
public virtual void OnBegin(object sender, LibraryBook libraryBook) => InfoLogAction($"Begin: {libraryBook.Book}");
public virtual void OnStatusUpdate(object sender, string statusUpdate) => InfoLogAction("- " + statusUpdate);
public virtual void OnCompleted(object sender, LibraryBook libraryBook) => InfoLogAction($"Completed: {libraryBook.Book}{Environment.NewLine}");
public virtual void OnBegin(object sender, LibraryBook libraryBook) => LogMe.Info($"Begin: {libraryBook.Book}");
public virtual void OnStatusUpdate(object sender, string statusUpdate) => LogMe.Info("- " + statusUpdate);
public virtual void OnCompleted(object sender, LibraryBook libraryBook) => LogMe.Info($"Completed: {libraryBook.Book}{Environment.NewLine}");
#endregion
}
}

View File

@ -1,16 +1,10 @@
using DataLayer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LibationWinForms.BookLiberation
{
internal class PdfDownloadForm : DownloadForm
{
public override void OnBegin(object sender, LibraryBook libraryBook) => InfoLogAction($"PDF Step, Begin: {libraryBook.Book}");
public override void OnCompleted(object sender, LibraryBook libraryBook) => InfoLogAction($"PDF Step, Completed: {libraryBook.Book}");
public override void OnBegin(object sender, LibraryBook libraryBook) => LogMe.Info($"PDF Step, Begin: {libraryBook.Book}");
public override void OnCompleted(object sender, LibraryBook libraryBook) => LogMe.Info($"PDF Step, Completed: {libraryBook.Book}");
}
}

View File

@ -86,11 +86,20 @@ namespace LibationWinForms.BookLiberation
await new BackupLoop(logMe, convertBook, automatedBackupsForm).RunBackupAsync();
}
private static BackupBook CreateBackupBook(EventHandler<LibraryBook> completedAction, LogMe logMe)
private static IProcessable CreateBackupBook(EventHandler<LibraryBook> completedAction, LogMe logMe)
{
var downloadPdf = CreateStreamProcessable<DownloadPdf, DownloadForm>(completedAction, logMe);
var downloadDecryptBook = CreateStreamProcessable<DownloadDecryptBook, AudioDecryptForm>(completedAction, logMe);
return new BackupBook(downloadDecryptBook, downloadPdf);
var downloadPdf = CreateStreamProcessable<DownloadPdf, PdfDownloadForm>(null, logMe);
//Chain pdf download on DownloadDecryptBook.Completed
async void onDownloadDecryptBookCompleted(object sender, LibraryBook e)
{
await downloadPdf.TryProcessAsync(e);
completedAction(sender, e);
}
var downloadDecryptBook = CreateStreamProcessable<DownloadDecryptBook, AudioDecryptForm>(onDownloadDecryptBookCompleted, logMe);
return downloadDecryptBook;
}
public static async Task BackupAllPdfsAsync(EventHandler<LibraryBook> completedAction = null)
@ -122,7 +131,7 @@ namespace LibationWinForms.BookLiberation
}
/// <summary>
/// Create a new <see cref="IStreamProcessable"/> and which creates a new <see cref="ProcessBaseForm"/> on <see cref="IProcessable.Begin"/>.
/// Create a new <see cref="IStreamProcessable"/> and links it to a new <see cref="ProcessBaseForm"/>.
/// </summary>
/// <typeparam name="TStrProc">The <see cref="IStreamProcessable"/> derrived type to create.</typeparam>
/// <typeparam name="TForm">The <see cref="ProcessBaseForm"/> derrived form to create on <see cref="IProcessable.Begin"/> and and be Shown on <see cref="IStreamable.StreamingBegin"/></typeparam>
@ -138,7 +147,7 @@ namespace LibationWinForms.BookLiberation
{
var processForm = new TForm();
Action<string> logAction = logMe is null ? (s) => { } : logMe.Info;
processForm.SetProcessable(strProc, logAction);
processForm.SetProcessable(strProc, logMe);
processForm.OnBegin(sender, libraryBook);
};