aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Shares/SharedFileCache.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/slskd/Shares/SharedFileCache.cs')
-rw-r--r--src/slskd/Shares/SharedFileCache.cs44
1 files changed, 42 insertions, 2 deletions
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<SharedFileCacheState> State { get; } = new ManagedState<SharedFileCacheState>();
private SemaphoreSlim SyncRoot { get; } = new SemaphoreSlim(1);
+ private CancellationTokenSource CancellationTokenSource { get; set; }
/// <summary>
/// Returns the contents of the cache.
@@ -126,13 +127,28 @@ namespace slskd.Shares
}
/// <summary>
+ /// Starts a scan of the configured shares and fills the cache.
+ /// </summary>
+ /// <param name="shares">The list of shares from which to fill the cache.</param>
+ /// <param name="filters">The list of regular expressions used to exclude files or paths from scanning.</param>
+ /// <returns>The operation context.</returns>
+ public Task StartFillAsync(IEnumerable<Share> shares, IEnumerable<Regex> filters)
+ {
+ return FillInternalAsync(shares, filters, continueAsync: true);
+ }
+
+ /// <summary>
/// Scans the configured shares and fills the cache.
/// </summary>
/// <param name="shares">The list of shares from which to fill the cache.</param>
/// <param name="filters">The list of regular expressions used to exclude files or paths from scanning.</param>
- /// <param name="cancellationToken">The optional cancellation token to monitor for cancellation requests.</param>
/// <returns>The operation context.</returns>
- public async Task FillAsync(IEnumerable<Share> shares, IEnumerable<Regex> filters, CancellationToken cancellationToken = default)
+ public Task FillAsync(IEnumerable<Share> shares, IEnumerable<Regex> filters)
+ {
+ return FillInternalAsync(shares, filters, continueAsync: false);
+ }
+
+ private async Task FillInternalAsync(IEnumerable<Share> shares, IEnumerable<Regex> 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();
}
}
@@ -463,6 +488,21 @@ namespace slskd.Shares
}
/// <summary>
+ /// Cancels the currently running fill operation, if one is running.
+ /// </summary>
+ /// <returns>A value indicating whether a fill operation was cancelled.</returns>
+ public bool TryCancelFill()
+ {
+ if (CancellationTokenSource != null)
+ {
+ CancellationTokenSource.Cancel();
+ return true;
+ }
+
+ return false;
+ }
+
+ /// <summary>
/// Attempts to load the cache from disk.
/// </summary>
/// <returns>A value indicating whether the load was successful.</returns>