mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 16:11:10 +00:00
Merge pull request #128 from BinaryAlley/refactor/enable-asynchronous-parallel-processing-in-media-library-scan-job
Enables async parallel processing in media library scan job
This commit is contained in:
3 files changed
+8
-8
No files matched your search
@@ -23,7 +23,7 @@ public interface IFileHashService
|
|||||||
/// <param name="callback">Callback to invoke during processing of elements.</param>
|
/// <param name="callback">Callback to invoke during processing of elements.</param>
|
||||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||||
/// <returns>A collection of files that changed since last library scan, along with their hashes.</returns>
|
/// <returns>A collection of files that changed since last library scan, along with their hashes.</returns>
|
||||||
List<ChangedFileSystemFile> HashFiles(
|
Task<List<ChangedFileSystemFile>> HashFilesAsync(
|
||||||
List<FileInfo> files,
|
List<FileInfo> files,
|
||||||
Dictionary<string, LibraryScanResultEntity> previousScanResults,
|
Dictionary<string, LibraryScanResultEntity> previousScanResults,
|
||||||
Func<Task> callback,
|
Func<Task> callback,
|
||||||
|
|||||||
+5
-5
@@ -134,8 +134,8 @@ internal sealed class HashComparerJob : MediaLibraryScanJob, IHashComparerJob
|
|||||||
lastUpdateTime = now;
|
lastUpdateTime = now;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
int processedFiles = 0;
|
int processedFilesCount = 0;
|
||||||
List<ChangedFileSystemFile> processed = fileHashService.HashFiles(filesToHash, libraryScanResultEntities,
|
List<ChangedFileSystemFile> processedFiles = await fileHashService.HashFilesAsync(filesToHash, libraryScanResultEntities,
|
||||||
async () =>
|
async () =>
|
||||||
{
|
{
|
||||||
// check if enough time has passed since last update
|
// check if enough time has passed since last update
|
||||||
@@ -144,7 +144,7 @@ internal sealed class HashComparerJob : MediaLibraryScanJob, IHashComparerJob
|
|||||||
{
|
{
|
||||||
// increment the number of processed elements progress
|
// increment the number of processed elements progress
|
||||||
publishJobProgressResult = await PublishJobProgress(
|
publishJobProgressResult = await PublishJobProgress(
|
||||||
publisher, compositeKey, Interlocked.Increment(ref processedFiles), files.Count, cancellationToken).ConfigureAwait(false);
|
publisher, compositeKey, Interlocked.Increment(ref processedFilesCount), files.Count, cancellationToken).ConfigureAwait(false);
|
||||||
if (publishJobProgressResult.IsError)
|
if (publishJobProgressResult.IsError)
|
||||||
return;
|
return;
|
||||||
// update the last update time
|
// update the last update time
|
||||||
@@ -156,11 +156,11 @@ internal sealed class HashComparerJob : MediaLibraryScanJob, IHashComparerJob
|
|||||||
await publisher!.Publish(new LibraryScanProgressChangedDomainEvent(Guid.NewGuid(), LibraryId, compositeKey, DateTime.UtcNow), cancellationToken).ConfigureAwait(false);
|
await publisher!.Publish(new LibraryScanProgressChangedDomainEvent(Guid.NewGuid(), LibraryId, compositeKey, DateTime.UtcNow), cancellationToken).ConfigureAwait(false);
|
||||||
Status = LibraryScanJobStatus.Completed;
|
Status = LibraryScanJobStatus.Completed;
|
||||||
sw.Stop();
|
sw.Stop();
|
||||||
Console.WriteLine($"Ended file hashing in {sw.ElapsedMilliseconds}ms, hashed {processed.Count} files.");
|
Console.WriteLine($"Ended file hashing in {sw.ElapsedMilliseconds}ms, hashed {processedFiles.Count} files.");
|
||||||
|
|
||||||
// call each linked child with the obtained payload
|
// call each linked child with the obtained payload
|
||||||
foreach (IMediaLibraryScanJob child in Children)
|
foreach (IMediaLibraryScanJob child in Children)
|
||||||
await child.ExecuteAsync(id, files, cancellationToken).ConfigureAwait(false);
|
await child.ExecuteAsync(id, processedFiles, cancellationToken).ConfigureAwait(false);
|
||||||
}, cancellationToken).ConfigureAwait(false);
|
}, cancellationToken).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ internal class FileHashService : IFileHashService
|
|||||||
/// <param name="callback">Callback to invoke during processing of elements.</param>
|
/// <param name="callback">Callback to invoke during processing of elements.</param>
|
||||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||||
/// <returns>A collection of files that changed since last library scan, along with their hashes.</returns>
|
/// <returns>A collection of files that changed since last library scan, along with their hashes.</returns>
|
||||||
public List<ChangedFileSystemFile> HashFiles(
|
public async Task<List<ChangedFileSystemFile>> HashFilesAsync(
|
||||||
List<FileInfo> files,
|
List<FileInfo> files,
|
||||||
Dictionary<string, LibraryScanResultEntity> previousScanResults,
|
Dictionary<string, LibraryScanResultEntity> previousScanResults,
|
||||||
Func<Task> callback,
|
Func<Task> callback,
|
||||||
@@ -49,7 +49,7 @@ internal class FileHashService : IFileHashService
|
|||||||
CancellationToken = cancellationToken,
|
CancellationToken = cancellationToken,
|
||||||
MaxDegreeOfParallelism = Environment.ProcessorCount
|
MaxDegreeOfParallelism = Environment.ProcessorCount
|
||||||
};
|
};
|
||||||
Parallel.ForEach(files, parallelOptions, async file =>
|
await Parallel.ForEachAsync(files, parallelOptions, async (file, cancellationToken) =>
|
||||||
{
|
{
|
||||||
cancellationToken.ThrowIfCancellationRequested();
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
try
|
try
|
||||||
|
|||||||
Reference in new issue
Block a user