mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 15:11:10 +00:00
Merge pull request #119 from BinaryAlley/refactor/fix-race-condition-chance-for-child-job-execution-in-library-scanning
Fixes a race condition chance for child job execution in library scanning module
This commit is contained in:
8 files changed
+18
-13
No files matched your search
@@ -6,7 +6,6 @@ using Lumina.Application.Common.Infrastructure.Authorization.Policies.Over18;
|
|||||||
using Lumina.Application.Common.Infrastructure.Security;
|
using Lumina.Application.Common.Infrastructure.Security;
|
||||||
using Lumina.Application.Common.Infrastructure.Time;
|
using Lumina.Application.Common.Infrastructure.Time;
|
||||||
using Lumina.Application.Core.MediaLibrary.Management.Progress;
|
using Lumina.Application.Core.MediaLibrary.Management.Progress;
|
||||||
using Lumina.Application.Core.MediaLibrary.Management.Services.Scanning.Queue;
|
|
||||||
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.Services.Cancellation;
|
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.Services.Cancellation;
|
||||||
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.Services.Jobs;
|
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.Services.Jobs;
|
||||||
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.Services.Queue;
|
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.Services.Queue;
|
||||||
@@ -19,6 +18,7 @@ using Lumina.Infrastructure.Core.MediaLibrary.Management.Scanning.Cancellation;
|
|||||||
using Lumina.Infrastructure.Core.MediaLibrary.Management.Scanning.Jobs.Common;
|
using Lumina.Infrastructure.Core.MediaLibrary.Management.Scanning.Jobs.Common;
|
||||||
using Lumina.Infrastructure.Core.MediaLibrary.Management.Scanning.Jobs.WrittenContent.Books;
|
using Lumina.Infrastructure.Core.MediaLibrary.Management.Scanning.Jobs.WrittenContent.Books;
|
||||||
using Lumina.Infrastructure.Core.MediaLibrary.Management.Scanning.Progress;
|
using Lumina.Infrastructure.Core.MediaLibrary.Management.Scanning.Progress;
|
||||||
|
using Lumina.Infrastructure.Core.MediaLibrary.Management.Scanning.Queue;
|
||||||
using Lumina.Infrastructure.Core.Security;
|
using Lumina.Infrastructure.Core.Security;
|
||||||
using Lumina.Infrastructure.Core.Time;
|
using Lumina.Infrastructure.Core.Time;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|||||||
+3
-2
@@ -55,9 +55,10 @@ internal sealed class FileSystemDiscoveryJob : MediaLibraryScanJob, IFileSystemD
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
parentsPayloadsExecuted++; // increment the number of parents that finished their execution and called this job
|
// increment the number of parents that finished their execution and called this job (beware race conditions, jobs run in parallel)
|
||||||
|
int parentsCompleted = Interlocked.Increment(ref parentsPayloadsExecuted);
|
||||||
// only execute this job's payload when it has no parents, or when all the parents finished their execution
|
// only execute this job's payload when it has no parents, or when all the parents finished their execution
|
||||||
if (Parents.Count == 0 || parentsPayloadsExecuted == Parents.Count)
|
if (Parents.Count == 0 || parentsCompleted == Parents.Count)
|
||||||
{
|
{
|
||||||
// this needs to be wrapped in a task because even though this job is processed in a "fire and forget" async manner, it still does synchronous
|
// this needs to be wrapped in a task because even though this job is processed in a "fire and forget" async manner, it still does synchronous
|
||||||
// file system processing that takes time, and would block the processing of scan jobs in the in-memory queue
|
// file system processing that takes time, and would block the processing of scan jobs in the in-memory queue
|
||||||
|
|||||||
+3
-2
@@ -44,9 +44,10 @@ internal sealed class HashComparerJob : MediaLibraryScanJob, IHashComparerJob
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
parentsPayloadsExecuted++; // increment the number of parents that finished their execution and called this job
|
// increment the number of parents that finished their execution and called this job (beware race conditions, jobs run in parallel)
|
||||||
|
int parentsCompleted = Interlocked.Increment(ref parentsPayloadsExecuted);
|
||||||
// only execute this job's payload when it has no parents, or when all the parents finished their execution
|
// only execute this job's payload when it has no parents, or when all the parents finished their execution
|
||||||
if (Parents.Count == 0 || parentsPayloadsExecuted == Parents.Count)
|
if (Parents.Count == 0 || parentsCompleted == Parents.Count)
|
||||||
{
|
{
|
||||||
Status = LibraryScanJobStatus.Running;
|
Status = LibraryScanJobStatus.Running;
|
||||||
Console.WriteLine("started file hashing");
|
Console.WriteLine("started file hashing");
|
||||||
|
|||||||
+3
-2
@@ -43,9 +43,10 @@ internal sealed class RepositoryMetadataDiscoveryJob : MediaLibraryScanJob, IRep
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
parentsPayloadsExecuted++; // increment the number of parents that finished their execution and called this job
|
// increment the number of parents that finished their execution and called this job (beware race conditions, jobs run in parallel)
|
||||||
|
int parentsCompleted = Interlocked.Increment(ref parentsPayloadsExecuted);
|
||||||
// only execute this job's payload when it has no parents, or when all the parents finished their execution
|
// only execute this job's payload when it has no parents, or when all the parents finished their execution
|
||||||
if (Parents.Count == 0 || parentsPayloadsExecuted == Parents.Count)
|
if (Parents.Count == 0 || parentsCompleted == Parents.Count)
|
||||||
{
|
{
|
||||||
// this needs to be wrapped in a task because even though this job is processed in a "fire and forget" async manner, it still does synchronous
|
// this needs to be wrapped in a task because even though this job is processed in a "fire and forget" async manner, it still does synchronous
|
||||||
// file system processing that takes time, and would block the processing of scan jobs in the in-memory queue
|
// file system processing that takes time, and would block the processing of scan jobs in the in-memory queue
|
||||||
|
|||||||
+3
-2
@@ -43,9 +43,10 @@ internal sealed class RepositoryMetadataSaveJob : MediaLibraryScanJob, IReposito
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
parentsPayloadsExecuted++; // increment the number of parents that finished their execution and called this job
|
// increment the number of parents that finished their execution and called this job (beware race conditions, jobs run in parallel)
|
||||||
|
int parentsCompleted = Interlocked.Increment(ref parentsPayloadsExecuted);
|
||||||
// only execute this job's payload when it has no parents, or when all the parents finished their execution
|
// only execute this job's payload when it has no parents, or when all the parents finished their execution
|
||||||
if (Parents.Count == 0 || parentsPayloadsExecuted == Parents.Count)
|
if (Parents.Count == 0 || parentsCompleted == Parents.Count)
|
||||||
{
|
{
|
||||||
// this needs to be wrapped in a task because even though this job is processed in a "fire and forget" async manner, it still does synchronous
|
// this needs to be wrapped in a task because even though this job is processed in a "fire and forget" async manner, it still does synchronous
|
||||||
// file system processing that takes time, and would block the processing of scan jobs in the in-memory queue
|
// file system processing that takes time, and would block the processing of scan jobs in the in-memory queue
|
||||||
|
|||||||
+3
-2
@@ -45,9 +45,10 @@ internal sealed class GoodReadsMetadataScrapJob : MediaLibraryScanJob, IGoodRead
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
parentsPayloadsExecuted++; // increment the number of parents that finished their execution and called this job
|
// increment the number of parents that finished their execution and called this job (beware race conditions, jobs run in parallel)
|
||||||
|
int parentsCompleted = Interlocked.Increment(ref parentsPayloadsExecuted);
|
||||||
// only execute this job's payload when it has no parents, or when all the parents finished their execution
|
// only execute this job's payload when it has no parents, or when all the parents finished their execution
|
||||||
if (Parents.Count == 0 || parentsPayloadsExecuted == Parents.Count)
|
if (Parents.Count == 0 || parentsCompleted == Parents.Count)
|
||||||
{
|
{
|
||||||
Status = LibraryScanJobStatus.Running;
|
Status = LibraryScanJobStatus.Running;
|
||||||
Console.WriteLine("started goodreads metadata");
|
Console.WriteLine("started goodreads metadata");
|
||||||
|
|||||||
+1
-1
@@ -4,7 +4,7 @@ using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.Library
|
|||||||
using System.Threading.Channels;
|
using System.Threading.Channels;
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
namespace Lumina.Application.Core.MediaLibrary.Management.Services.Scanning.Queue;
|
namespace Lumina.Infrastructure.Core.MediaLibrary.Management.Scanning.Queue;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Provides an in-memory message queue for handling <see cref="IMediaLibraryScanJob"/> instances.
|
/// Provides an in-memory message queue for handling <see cref="IMediaLibraryScanJob"/> instances.
|
||||||
|
|||||||
+1
-1
@@ -10,7 +10,7 @@ using System.Threading;
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
namespace Lumina.Application.Core.MediaLibrary.Management.Services.Scanning.Queue;
|
namespace Lumina.Infrastructure.Core.MediaLibrary.Management.Scanning.Queue;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Background service for processing media library scan jobs from an in-memory message queue.
|
/// Background service for processing media library scan jobs from an in-memory message queue.
|
||||||
|
|||||||
Reference in new issue
Block a user