Adds media library scan progress tracking

This commit is contained in:
BinaryAlley committed 2025-02-18 17:24:39 +02:00
1 parent e8dbf9456c
commit 27fa8e2e4f
87 files changed
+2078 -421

No files matched your search

@@ -50,12 +50,16 @@ public class EventualConsistencyMiddleware
// only proceed if the response status code indicates success
if (context.Response.StatusCode >= 200 && context.Response.StatusCode < 300)
{
// commit the transaction: because some domain events trigger jobs that end up being processed in background hosted services, and those are
// singletons that require their own DI scope to be created, any changes made on the DbContext in this HTTP scope would not be seen on the
// DbContext created in the background service scope, because the transaction is not yet commited here; therefor, the transaction needs to be
// commited first, before any domain events are fired
// see docs/technical/achitecture/architecture-knowledge-management/architecture-decision-log/architecture-decission-record-0002.md for details:
await transaction.CommitAsync().ConfigureAwait(false);
// check if domain events exist in the channel queue and publish them
while (domainEventQueue.TryDequeue(out IDomainEvent? nextEvent))
await publisher.Publish(nextEvent);
// commit the transaction
await transaction.CommitAsync().ConfigureAwait(false);
}
}
catch (EventualConsistencyException ex)
@@ -0,0 +1,54 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using FastEndpoints;
using Lumina.Application.Common.Mapping.MediaLibrary.Management;
using Lumina.Contracts.Requests.MediaLibrary.Management;
using Lumina.Contracts.Responses.MediaLibrary.Management;
using Lumina.Presentation.Api.Common.Routes.Library.Management;
using Lumina.Presentation.Api.Core.Endpoints.Common;
using Mediator;
using Microsoft.AspNetCore.Http;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Presentation.Api.Core.Endpoints.Library.Management.GetLibraryScanProgress;
/// <summary>
/// API endpoint for the <c>/libraries/{libraryId}/scans/{scanId}/progress</c> route.
/// </summary>
public class GetLibraryScanProgressEndpoint : BaseEndpoint<GetLibraryScanProgressRequest, IResult>
{
private readonly ISender _sender;
/// <summary>
/// Initializes a new instance of the <see cref="GetLibraryScanProgressEndpoint"/> class.
/// </summary>
/// <param name="sender">Injected service for mediating commands and queries.</param>
public GetLibraryScanProgressEndpoint(ISender sender)
{
_sender = sender;
}
/// <summary>
/// Configures the API endpoint.
/// </summary>
public override void Configure()
{
Verbs(Http.GET);
Routes(ApiRoutes.Libraries.LIBRARY_SCAN_PROGRESS);
Version(1);
DontCatchExceptions();
}
/// <summary>
/// Gets the progress of a media library scan.
/// </summary>
/// <param name="request">The request containing the Id of the media library and of the scan whose progress is requested.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
public override async Task<IResult> ExecuteAsync(GetLibraryScanProgressRequest request, CancellationToken cancellationToken)
{
ErrorOr<MediaLibraryScanProgressResponse> result = await _sender.Send(request.ToQuery(), cancellationToken).ConfigureAwait(false);
return result.Match(success => TypedResults.Ok(success), Problem);
}
}
@@ -0,0 +1,131 @@
#region ========================================================================= USING =====================================================================================
using FastEndpoints;
using Lumina.Contracts.Requests.MediaLibrary.Management;
using Lumina.Contracts.Responses.MediaLibrary.Management;
using Lumina.Domain.Common.Enums.MediaLibrary;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Presentation.Api.Core.Endpoints.Library.Management.GetLibraryScanProgress;
/// <summary>
/// Class used for providing a textual description for the <see cref="GetLibraryScanProgressEndpoint"/> API endpoint, for OpenAPI.
/// </summary>
[ExcludeFromCodeCoverage]
public class GetLibraryScanProgressEndpointSummary : Summary<GetLibraryScanProgressEndpoint, GetLibraryScanProgressRequest>
{
/// <summary>
/// Initializes a new instance of the <see cref="GetLibraryScanProgressEndpointSummary"/> class.
/// </summary>
public GetLibraryScanProgressEndpointSummary()
{
Summary = "Retrieves the progress of a media library scan.";
Description = "Retrieves the progress of a media library scan, if the user making the request is an Admin, or the owner of the library.";
ExampleRequest = new GetLibraryScanProgressRequest(
LibraryId: Guid.NewGuid(),
ScanId: Guid.NewGuid()
);
RequestParam(r => r.LibraryId, "The Id of the media library whose scan progress is requested. Required.");
RequestParam(r => r.ScanId, "The Id of the media library scan whose progress is requested. Required.");
ResponseParam<MediaLibraryScanProgressResponse>(r => r.TotalJobs, "The total number of jobs to be processed by the scan.");
ResponseParam<MediaLibraryScanProgressResponse>(r => r.CompletedJobs, "The number of jobs that have been processed.");
ResponseParam<MediaLibraryScanProgressResponse>(r => r.CurrentJobProgress, "The progress of the currently processing job.");
ResponseParam<MediaLibraryScanProgressResponse>(r => r.Status, "The status of the scan.");
ResponseParam<MediaLibraryScanProgressResponse>(r => r.OverallProgressPercentage, "The ratio between the number of processed jobs and the total number of jobs to process, as percentage.");
Response(200, "The progress of the media library scan is returned.", example: new MediaLibraryScanProgressResponse(
TotalJobs: 10,
CompletedJobs: 4,
CurrentJobProgress: new MediaLibraryScanJobProgressResponse(
TotalItems: 1536,
CompletedItems: 768,
CurrentOperation: "Comparing file hashes",
ProgressPercentage: 50
),
Status: LibraryScanJobStatus.Running,
OverallProgressPercentage: 40
));
Response(401, "Authentication required.", "application/problem+json",
example: new[]
{
new
{
type = "https://tools.ietf.org/html/rfc7235#section-3.1",
status = 401,
title = "Unauthorized",
detail = "You are not authorized",
instance = "/api/v1/libraries/{libraryId}/scans/{scanId}/progress"
},
new
{
type = "https://tools.ietf.org/html/rfc7235#section-3.1",
status = 401,
title = "Unauthorized",
detail = "Invalid token: The token expired at '01/01/2024 01:00:00'",
instance = "/api/v1/libraries/{libraryId}/scans/{scanId}/progress"
},
new
{
type = "https://tools.ietf.org/html/rfc7235#section-3.1",
status = 401,
title = "Unauthorized",
detail = "The token is invalid",
instance = "/api/v1/libraries/{libraryId}/scans/{scanId}/progress"
}
}
);
Response(403, "The request failed because the user making the request is not an Admin, or the owner of the media library.", "application/problem+json",
example: new
{
type = "https://tools.ietf.org/html/rfc9110#section-15.5.4",
title = "General.Failure",
status = 403,
detail = "NotAuthorized",
instance = "/api/v1/libraries/{libraryId}/scans/{scanId}/progress",
traceId = "00-a712bbf99ca8ab485f86a762ae5ae74d-b3a2eb78813b0a5d-00"
}
);
Response(404, "The request failed because the requested media library has no scan started.", "application/problem+json",
example: new
{
type = "https://tools.ietf.org/html/rfc9110#section-15.5.5",
title = "General.NotFound",
status = 404,
detail = "LibraryScanNotFound",
instance = "/api/v1/libraries/{libraryId}/scans/{scanId}/progress",
traceId = "00-57d15dadd702dbd4aeb5dc9b7cee68ee-9330237dbb2ce0e5-00"
}
);
Response(422, "The request did not pass validation checks.", "application/problem+json",
example: new
{
type = "https://tools.ietf.org/html/rfc4918#section-11.2",
title = "General.Validation",
status = 422,
detail = "OneOrMoreValidationErrorsOccurred",
instance = "/api/v1/libraries/{libraryId}/scans/{scanId}/progress",
errors = new Dictionary<string, string[]>
{
{
"General.Validation", new[]
{
"LibraryIdCannotBeEmpty",
"LibraryScanIdCannotBeEmpty"
}
}
},
traceId = "00-2470be4248a2a5a0c6f70579975a6954-b9c3ba9544a03500-00"
}
);
}
}
@@ -47,7 +47,7 @@ public class ScanLibrariesEndpoint : BaseEndpoint<EmptyRequest, IResult>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
public override async Task<IResult> ExecuteAsync(EmptyRequest _, CancellationToken cancellationToken)
{
ErrorOr<IEnumerable<ScanLibraryResponse>> result = await _sender.Send(new ScanLibrariesCommand(), cancellationToken).ConfigureAwait(false);
ErrorOr<IEnumerable<MediaLibraryScanResponse>> result = await _sender.Send(new ScanLibrariesCommand(), cancellationToken).ConfigureAwait(false);
return result.Match(success => TypedResults.Ok(success), Problem);
}
}
@@ -22,7 +22,7 @@ public class ScanLibrariesEndpointSummary : Summary<ScanLibrariesEndpoint, Empty
Description = "Starts the scanning process of all media libraries of a user, or all libraries, if the user is an Admin.";
Response(200, "The media libraries scan was successfully started.",
example: new ScanLibraryResponse[] {
example: new MediaLibraryScanResponse[] {
new (
ScanId: Guid.NewGuid(),
LibraryId: Guid.NewGuid()
@@ -47,7 +47,7 @@ public class ScanLibraryEndpoint : BaseEndpoint<ScanLibraryRequest, IResult>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
public override async Task<IResult> ExecuteAsync(ScanLibraryRequest request, CancellationToken cancellationToken)
{
ErrorOr<ScanLibraryResponse> result = await _sender.Send(request.ToCommand(), cancellationToken).ConfigureAwait(false);
ErrorOr<MediaLibraryScanResponse> result = await _sender.Send(request.ToCommand(), cancellationToken).ConfigureAwait(false);
return result.Match(success => TypedResults.Ok(success), Problem);
}
}
@@ -23,7 +23,7 @@ public class ScanLibraryEndpointSummary : Summary<ScanLibraryEndpoint, EmptyRequ
Description = "Starts the scanning process of a media library of a user.";
Response(200, "The media library scan was successfully started.",
example: new ScanLibraryResponse(
example: new MediaLibraryScanResponse(
ScanId: Guid.NewGuid(),
LibraryId: Guid.NewGuid()
));