mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-01 18:57:24 +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
+9
-9
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="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>
|
||||
List<ChangedFileSystemFile> HashFiles(
|
||||
Task<List<ChangedFileSystemFile>> HashFilesAsync(
|
||||
List<FileInfo> files,
|
||||
Dictionary<string, LibraryScanResultEntity> previousScanResults,
|
||||
Func<Task> callback,
|
||||
|
||||
+5
-5
@@ -134,8 +134,8 @@ internal sealed class HashComparerJob : MediaLibraryScanJob, IHashComparerJob
|
||||
lastUpdateTime = now;
|
||||
}
|
||||
}
|
||||
int processedFiles = 0;
|
||||
List<ChangedFileSystemFile> processed = fileHashService.HashFiles(filesToHash, libraryScanResultEntities,
|
||||
int processedFilesCount = 0;
|
||||
List<ChangedFileSystemFile> processedFiles = await fileHashService.HashFilesAsync(filesToHash, libraryScanResultEntities,
|
||||
async () =>
|
||||
{
|
||||
// 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
|
||||
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)
|
||||
return;
|
||||
// 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);
|
||||
Status = LibraryScanJobStatus.Completed;
|
||||
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
|
||||
foreach (IMediaLibraryScanJob child in Children)
|
||||
await child.ExecuteAsync(id, files, cancellationToken).ConfigureAwait(false);
|
||||
await child.ExecuteAsync(id, processedFiles, cancellationToken).ConfigureAwait(false);
|
||||
}, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,8 +33,8 @@ internal class FileHashService : IFileHashService
|
||||
/// <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>
|
||||
/// <returns>A collection of files that changed since last library scan, along with their hashes.</returns>
|
||||
public List<ChangedFileSystemFile> HashFiles(
|
||||
List<FileInfo> files,
|
||||
public async Task<List<ChangedFileSystemFile>> HashFilesAsync(
|
||||
List<FileInfo> files,
|
||||
Dictionary<string, LibraryScanResultEntity> previousScanResults,
|
||||
Func<Task> callback,
|
||||
CancellationToken cancellationToken)
|
||||
@@ -49,7 +49,7 @@ internal class FileHashService : IFileHashService
|
||||
CancellationToken = cancellationToken,
|
||||
MaxDegreeOfParallelism = Environment.ProcessorCount
|
||||
};
|
||||
Parallel.ForEach(files, parallelOptions, async file =>
|
||||
await Parallel.ForEachAsync(files, parallelOptions, async (file, cancellationToken) =>
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
try
|
||||
|
||||
Reference in new issue
Block a user