Replaces the in-memory media library scan diff with a database-centric job pipeline

This commit is contained in:
BinaryAlley committed 2026-08-09 13:17:51 -07:00
1 parent 1dc3da3da1
commit 9905aa08f9
38 files changed
+1032 -987

No files matched your search

@@ -0,0 +1,26 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Domain.Common.Events;
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryAggregate.ValueObjects;
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.ValueObjects;
using System;
using System.Diagnostics;
#endregion
namespace Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.Events;
/// <summary>
/// Domain event raised when a media library item was present in the media library scan snapshot of a previous scan, but it is no longer present on disk.
/// </summary>
/// <param name="Id">The unique identifier of the domain event.</param>
/// <param name="LibraryId">The unique identifier of the library to which the deleted media library item belonged.</param>
/// <param name="MediaLibraryScanCompositeId">Model for tracking media library scans.</param>
/// <param name="Path">The path of the media library item that was deleted.</param>
/// <param name="OccurredOnUtc">The date and time when the domain event occurred.</param>
[DebuggerDisplay("Id: {Id}; Path: {Path}")]
public record LibraryMediaItemDeletedDomainEvent(
Guid Id,
LibraryId LibraryId,
MediaLibraryScanCompositeId MediaLibraryScanCompositeId,
string Path,
DateTime OccurredOnUtc
) : IDomainEvent;
@@ -15,10 +15,12 @@ namespace Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.Lib
/// <param name="LibraryId">The unique identifier of the library whose scan has failed.</param>
/// <param name="MediaLibraryScanCompositeId">Model for tracking media library scans.</param>
/// <param name="OccurredOnUtc">The date and time when the domain event occurred.</param>
/// <param name="ErrorMessage">The optional descriptive message of the failure, used for diagnostic purposes.</param>
[DebuggerDisplay("Id: {Id}")]
public record LibraryScanFailedDomainEvent(
Guid Id,
LibraryId LibraryId,
MediaLibraryScanCompositeId MediaLibraryScanCompositeId,
DateTime OccurredOnUtc
DateTime OccurredOnUtc,
string? ErrorMessage = null
) : IDomainEvent;
@@ -0,0 +1,21 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryAggregate.ValueObjects;
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.Services.Jobs;
using System.Collections.Generic;
#endregion
namespace Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.Services.Hooks;
/// <summary>
/// Registry of the media library scan jobs that are injected by plugins at the defined hook points of the media library scan job graph.
/// </summary>
public interface IScanJobRegistry
{
/// <summary>
/// Gets the media library scan jobs registered for the provided <paramref name="hookName"/>.
/// </summary>
/// <param name="hookName">The name of the hook point at which the media library scan jobs were injected.</param>
/// <param name="libraryId">The unique identifier of the media library upon which the scan is performed.</param>
/// <returns>The collection of media library scan jobs registered at the provided hook point.</returns>
IEnumerable<IMediaLibraryScanJob> GetJobsForHook(string hookName, LibraryId libraryId);
}
@@ -0,0 +1,32 @@
namespace Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.Services.Hooks;
/// <summary>
/// Defines the named junction points of the media library scan job graph where plugin jobs can be injected.
/// </summary>
public static class ScanJobHooks
{
/// <summary>
/// The hook that runs after the file system discovery job, and before the scan diff job.
/// </summary>
public const string AFTER_FILE_SYSTEM_DISCOVERY = "AfterFileSystemDiscovery";
/// <summary>
/// The hook that runs after the scan diff job, and before the scan hash job.
/// </summary>
public const string AFTER_SCAN_DIFF = "AfterScanDiff";
/// <summary>
/// The hook that runs after the scan hash job, and before any metadata enrichment jobs.
/// </summary>
public const string BEFORE_METADATA_ENRICHMENT = "BeforeMetadataEnrichment";
/// <summary>
/// The hook that runs after any metadata enrichment jobs, and before the scan results save job.
/// </summary>
public const string AFTER_METADATA_ENRICHMENT = "AfterMetadataEnrichment";
/// <summary>
/// The hook that runs before the scan results save job, which should always be the last job in the directed acyclic job graph.
/// </summary>
public const string BEFORE_SCAN_RESULTS_SAVE = "BeforeScanResultsSave";
}
@@ -1,8 +0,0 @@
namespace Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.Services.Jobs;
/// <summary>
/// Interface for the media library scan job for discovering file system items.
/// </summary>
public interface IFileSystemDiscoveryJob : IMediaLibraryScanJob
{
}
@@ -1,8 +0,0 @@
namespace Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.Services.Jobs;
/// <summary>
/// Interface for the media library scan job for comparing hashes.
/// </summary>
public interface IHashComparerJob : IMediaLibraryScanJob
{
}
@@ -0,0 +1,8 @@
namespace Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.Services.Jobs;
/// <summary>
/// Interface for the media library scan job for computing the differences between the files on disk and the media library scan snapshot of the previous scan.
/// </summary>
public interface IMediaLibraryScanDiffJob : IMediaLibraryScanJob
{
}
@@ -0,0 +1,8 @@
namespace Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.Services.Jobs;
/// <summary>
/// Interface for the media library scan job for computing the content hashes of the files that changed since the previous scan.
/// </summary>
public interface IMediaLibraryScanHashJob : IMediaLibraryScanJob
{
}
@@ -0,0 +1,8 @@
namespace Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.Services.Jobs;
/// <summary>
/// Interface for the media library scan job for persisting the results of the current scan. This should always be the last job in the directed acyclic job graph.
/// </summary>
public interface IMediaLibraryScanResultsSaveJob : IMediaLibraryScanJob
{
}
@@ -1,9 +0,0 @@
namespace Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.Services.Jobs;
/// <summary>
/// Interface for the media library scan job for retrieving persistence data from any eventual previous scans.
/// </summary>
public interface IRepositoryMetadataDiscoveryJob : IMediaLibraryScanJob
{
}
@@ -1,9 +0,0 @@
namespace Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.Services.Jobs;
/// <summary>
/// Interface for the media library scan job for persisting data of the current scan. This should always be the last job in the directed acyclic job graph.
/// </summary>
public interface IRepositoryMetadataSaveJob : IMediaLibraryScanJob
{
}
@@ -1,13 +0,0 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.Services.Jobs;
#endregion
namespace Lumina.Domain.Core.BoundedContexts.WrittenContentLibraryBoundedContext.BookLibraryAggregate.Services.Jobs;
/// <summary>
/// Interface for the media library scan job for filtering only files that have known book formats extensions.
/// </summary>
public interface IBooksFileExtensionsFilterJob : IMediaLibraryScanJob
{
}
@@ -0,0 +1,10 @@
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.Services.Jobs;
namespace Lumina.Domain.Core.BoundedContexts.WrittenContentLibraryBoundedContext.BookLibraryAggregate.Services.Jobs;
/// <summary>
/// Interface for the media library scan job for discovering books file system items.
/// </summary>
public interface IBooksFileSystemDiscoveryJob : IMediaLibraryScanJob
{
}
@@ -1,6 +1,7 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Domain.Common.Enums.MediaLibrary;
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryAggregate.ValueObjects;
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.Services.Hooks;
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.Services.Jobs;
using Lumina.Domain.Core.BoundedContexts.WrittenContentLibraryBoundedContext.BookLibraryAggregate.Services.Jobs;
using System.Collections.Generic;
@@ -14,57 +15,81 @@ namespace Lumina.Domain.Core.BoundedContexts.WrittenContentLibraryBoundedContext
internal class BookLibraryTypeScanner : IBookLibraryTypeScanner
{
private readonly IMediaLibraryScanJobFactory _mediaScanJobFactory;
private readonly IScanJobRegistry _scanJobRegistry;
/// <inheritdoc/>
/// <summary>
/// The media library type that this media library scanner supports.
/// </summary>
public LibraryType SupportedType { get; } = LibraryType.Book;
/// <summary>
/// Initializes a new instance of the <see cref="BookLibraryTypeScanner"/> class.
/// </summary>
/// <param name="mediaScanJobFactory">Injected factory for creating media library scan jobs.</param>
public BookLibraryTypeScanner(IMediaLibraryScanJobFactory mediaScanJobFactory)
/// <param name="scanJobRegistry">Injected registry of the media library scan jobs injected by plugins at the defined hook points.</param>
public BookLibraryTypeScanner(IMediaLibraryScanJobFactory mediaScanJobFactory, IScanJobRegistry scanJobRegistry)
{
_mediaScanJobFactory = mediaScanJobFactory;
_scanJobRegistry = scanJobRegistry;
}
/// <inheritdoc/>
public IEnumerable<IMediaLibraryScanJob> CreateScanJobsForLibrary(LibraryId libraryId, bool downloadMedatadaFromWeb)
/// <summary>
/// Creates the media library scan jobs for the provided media library.
/// </summary>
/// <param name="libraryId">The unique identifier of the media library for which to create the media library scan jobs.</param>
/// <param name="downloadMetadataFromWeb">Whether the library permits downloading data from the web, or not.</param>
/// <returns>A collection of media library scan jobs.</returns>
public IEnumerable<IMediaLibraryScanJob> CreateScanJobsForLibrary(LibraryId libraryId, bool downloadMetadataFromWeb)
{
// declare the list of jobs that this scanner requires
IFileSystemDiscoveryJob fileSystemDiscoveryJob = _mediaScanJobFactory.CreateJob<IFileSystemDiscoveryJob>(libraryId);
IRepositoryMetadataDiscoveryJob repositoryMetadataDiscoveryJob = _mediaScanJobFactory.CreateJob<IRepositoryMetadataDiscoveryJob>(libraryId);
IBooksFileExtensionsFilterJob booksFileExtensionsFilterJob = _mediaScanJobFactory.CreateJob<IBooksFileExtensionsFilterJob>(libraryId);
IHashComparerJob hashComparerJob = _mediaScanJobFactory.CreateJob<IHashComparerJob>(libraryId);
IBooksFileSystemDiscoveryJob fileSystemDiscoveryJob = _mediaScanJobFactory.CreateJob<IBooksFileSystemDiscoveryJob>(libraryId);
IMediaLibraryScanDiffJob mediaLibraryScanDiffJob = _mediaScanJobFactory.CreateJob<IMediaLibraryScanDiffJob>(libraryId);
IMediaLibraryScanHashJob mediaLibraryScanHashJob = _mediaScanJobFactory.CreateJob<IMediaLibraryScanHashJob>(libraryId);
IGoodReadsMetadataScrapJob goodReadsMetadataScrapJob = _mediaScanJobFactory.CreateJob<IGoodReadsMetadataScrapJob>(libraryId);
IRepositoryMetadataSaveJob repositoryMetadataSaveJob = _mediaScanJobFactory.CreateJob<IRepositoryMetadataSaveJob>(libraryId);
IMediaLibraryScanResultsSaveJob mediaLibraryScanResultsSaveJob = _mediaScanJobFactory.CreateJob<IMediaLibraryScanResultsSaveJob>(libraryId);
// establish the hierarchical relationships between jobs
fileSystemDiscoveryJob.AddChild(booksFileExtensionsFilterJob);
repositoryMetadataDiscoveryJob.AddChild(hashComparerJob);
booksFileExtensionsFilterJob.AddChild(hashComparerJob);
booksFileExtensionsFilterJob.AddParent(fileSystemDiscoveryJob);
// establish the hierarchical relationships between jobs, splicing the plugin jobs at the defined hook points
IMediaLibraryScanJob previousJob = fileSystemDiscoveryJob;
previousJob = SpliceJobsAtHook(previousJob, ScanJobHooks.AFTER_FILE_SYSTEM_DISCOVERY, libraryId);
previousJob.AddChild(mediaLibraryScanDiffJob);
mediaLibraryScanDiffJob.AddParent(previousJob);
hashComparerJob.AddParent(booksFileExtensionsFilterJob);
hashComparerJob.AddParent(repositoryMetadataDiscoveryJob);
previousJob = SpliceJobsAtHook(mediaLibraryScanDiffJob, ScanJobHooks.AFTER_SCAN_DIFF, libraryId);
previousJob.AddChild(mediaLibraryScanHashJob);
mediaLibraryScanHashJob.AddParent(previousJob);
if (downloadMedatadaFromWeb)
previousJob = SpliceJobsAtHook(mediaLibraryScanHashJob, ScanJobHooks.BEFORE_METADATA_ENRICHMENT, libraryId);
if (downloadMetadataFromWeb)
{
hashComparerJob.AddChild(goodReadsMetadataScrapJob);
goodReadsMetadataScrapJob.AddChild(repositoryMetadataSaveJob);
goodReadsMetadataScrapJob.AddParent(hashComparerJob);
repositoryMetadataSaveJob.AddParent(goodReadsMetadataScrapJob);
}
else // TODO: perform some action with local files?
{
hashComparerJob.AddChild(repositoryMetadataSaveJob);
repositoryMetadataSaveJob.AddParent(hashComparerJob);
previousJob.AddChild(goodReadsMetadataScrapJob);
goodReadsMetadataScrapJob.AddParent(previousJob);
previousJob = goodReadsMetadataScrapJob;
}
previousJob = SpliceJobsAtHook(previousJob, ScanJobHooks.AFTER_METADATA_ENRICHMENT, libraryId);
previousJob = SpliceJobsAtHook(previousJob, ScanJobHooks.BEFORE_SCAN_RESULTS_SAVE, libraryId);
previousJob.AddChild(mediaLibraryScanResultsSaveJob);
mediaLibraryScanResultsSaveJob.AddParent(previousJob);
// return the top level jobs that will be triggered when the scan will be started
yield return fileSystemDiscoveryJob;
yield return repositoryMetadataDiscoveryJob;
}
/// <summary>
/// Splices the plugin jobs registered at the provided <paramref name="hookName"/> after the <paramref name="previousJob"/>, and returns the last job of the extended chain.
/// </summary>
/// <param name="previousJob">The job after which the plugin jobs are spliced.</param>
/// <param name="hookName">The name of the hook point at which the plugin jobs are injected.</param>
/// <param name="libraryId">The unique identifier of the media library upon which the scan is performed.</param>
/// <returns>The last job of the extended chain, which can be used as the parent for the next job.</returns>
private IMediaLibraryScanJob SpliceJobsAtHook(IMediaLibraryScanJob previousJob, string hookName, LibraryId libraryId)
{
IMediaLibraryScanJob currentJob = previousJob;
foreach (IMediaLibraryScanJob pluginJob in _scanJobRegistry.GetJobsForHook(hookName, libraryId))
{
currentJob.AddChild(pluginJob);
pluginJob.AddParent(currentJob);
currentJob = pluginJob;
}
return currentJob;
}
}