From d3009fbb67f76337949faa99cd7180d23451f8ca Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 24 Sep 2022 12:41:17 -0500 Subject: add sync/async overloads for filling, make scans cancellable --- src/slskd/Shares/SharedFileCache.cs | 44 +++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) (limited to 'src/slskd/Shares/SharedFileCache.cs') diff --git a/src/slskd/Shares/SharedFileCache.cs b/src/slskd/Shares/SharedFileCache.cs index 07dc6ba5..0fb3c2d0 100644 --- a/src/slskd/Shares/SharedFileCache.cs +++ b/src/slskd/Shares/SharedFileCache.cs @@ -63,6 +63,7 @@ namespace slskd.Shares private ISoulseekFileFactory SoulseekFileFactory { get; } private IManagedState State { get; } = new ManagedState(); private SemaphoreSlim SyncRoot { get; } = new SemaphoreSlim(1); + private CancellationTokenSource CancellationTokenSource { get; set; } /// /// Returns the contents of the cache. @@ -125,14 +126,29 @@ namespace slskd.Shares return directories.Values.OrderBy(d => d.Name); } + /// + /// Starts a scan of the configured shares and fills the cache. + /// + /// The list of shares from which to fill the cache. + /// The list of regular expressions used to exclude files or paths from scanning. + /// The operation context. + public Task StartFillAsync(IEnumerable shares, IEnumerable filters) + { + return FillInternalAsync(shares, filters, continueAsync: true); + } + /// /// Scans the configured shares and fills the cache. /// /// The list of shares from which to fill the cache. /// The list of regular expressions used to exclude files or paths from scanning. - /// The optional cancellation token to monitor for cancellation requests. /// The operation context. - public async Task FillAsync(IEnumerable shares, IEnumerable filters, CancellationToken cancellationToken = default) + public Task FillAsync(IEnumerable shares, IEnumerable filters) + { + return FillInternalAsync(shares, filters, continueAsync: false); + } + + private async Task FillInternalAsync(IEnumerable shares, IEnumerable filters, bool continueAsync = true) { // obtain the semaphore, or fail if it can't be obtained immediately, indicating that a scan is running. if (!await SyncRoot.WaitAsync(millisecondsTimeout: 0)) @@ -141,6 +157,9 @@ namespace slskd.Shares throw new ShareScanInProgressException("Shared files are already being scanned."); } + CancellationTokenSource = new CancellationTokenSource(); + var cancellationToken = CancellationTokenSource.Token; + try { State.SetValue(state => state with @@ -164,6 +183,11 @@ namespace slskd.Shares Log.Debug("Starting shared file scan"); + if (continueAsync) + { + await Task.Yield(); + } + var timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); var sw = new Stopwatch(); @@ -369,6 +393,7 @@ namespace slskd.Shares } finally { + CancellationTokenSource = null; SyncRoot.Release(); } } @@ -462,6 +487,21 @@ namespace slskd.Shares } } + /// + /// Cancels the currently running fill operation, if one is running. + /// + /// A value indicating whether a fill operation was cancelled. + public bool TryCancelFill() + { + if (CancellationTokenSource != null) + { + CancellationTokenSource.Cancel(); + return true; + } + + return false; + } + /// /// Attempts to load the cache from disk. /// -- cgit v1.2.3