aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Shares/SharedFileCache.cs
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2022-09-24 16:23:04 -0500
committerJP Dillingham <jpdillingham@gmail.com>2022-09-24 16:23:04 -0500
commit6484666fc88126f3216846409e3f1b81b2a78677 (patch)
tree3dcc6db90ec073b2c702e1efe423fa4045b6e82e /src/slskd/Shares/SharedFileCache.cs
parentc42c892408faf7481d111e866a654ea37866d94b (diff)
downloadslskd-6484666fc88126f3216846409e3f1b81b2a78677.tar.gz
undo separation of start/scan, improve cancellation logic
Diffstat (limited to 'src/slskd/Shares/SharedFileCache.cs')
-rw-r--r--src/slskd/Shares/SharedFileCache.cs70
1 files changed, 29 insertions, 41 deletions
diff --git a/src/slskd/Shares/SharedFileCache.cs b/src/slskd/Shares/SharedFileCache.cs
index dd399e68..17fc5fb5 100644
--- a/src/slskd/Shares/SharedFileCache.cs
+++ b/src/slskd/Shares/SharedFileCache.cs
@@ -127,28 +127,12 @@ 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>
/// <returns>The operation context.</returns>
- 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)
+ public async Task FillAsync(IEnumerable<Share> shares, IEnumerable<Regex> filters)
{
// obtain the semaphore, or fail if it can't be obtained immediately, indicating that a scan is running.
if (!await SyncRoot.WaitAsync(millisecondsTimeout: 0))
@@ -160,12 +144,17 @@ namespace slskd.Shares
CancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(Program.MasterCancellationTokenSource.Token);
var cancellationToken = CancellationTokenSource.Token;
+ // send control back to the calling context. if we don't do this, the caller will block until
+ // directories have been enumerated.
+ await Task.Yield();
+
try
{
State.SetValue(state => state with
{
Filling = true,
Filled = false,
+ Cancelled = false,
FillProgress = 0,
Directories = 0,
Files = 0,
@@ -183,11 +172,6 @@ namespace slskd.Shares
Log.Debug("Starting shared file scan");
- if (continueAsync)
- {
- await Task.Yield();
- }
-
var timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
var sw = new Stopwatch();
@@ -320,16 +304,7 @@ namespace slskd.Shares
cached += addedFiles;
Log.Debug("Finished scanning {Directory}: {Added} files added and {Filtered} filtered", directory, addedFiles, filteredFiles);
-
- try
- {
- State.SetValue(state => state with { FillProgress = current / (double)unmaskedDirectories.Count, Files = cached });
- }
- catch (Exception ex)
- {
- Log.Error(ex, "Failed to set cache state following scan of {Directory}: {Message}", directory, ex.Message);
- throw;
- }
+ State.SetValue(state => state with { FillProgress = current / (double)unmaskedDirectories.Count, Files = cached });
}));
}
@@ -337,17 +312,28 @@ namespace slskd.Shares
workers.ForEach(w => w.Start());
Log.Debug("All workers started");
- Log.Debug("Filling DirectoryChannel...");
- foreach (var directory in unmaskedDirectories)
+ try
{
- await channel.Writer.WriteAsync(directory, cancellationToken);
- }
+ try
+ {
+ Log.Debug("Filling DirectoryChannel...");
+ foreach (var directory in unmaskedDirectories)
+ {
+ await channel.Writer.WriteAsync(directory, cancellationToken);
+ }
- channel.Writer.Complete();
- Log.Debug("DirectoryChannel filled");
+ Log.Debug("DirectoryChannel filled");
+ }
+ catch (OperationCanceledException)
+ {
+ Log.Warning("Shared file scan cancellation requested");
+ Log.Debug("DirectoryChannel fill aborted");
+ }
+ finally
+ {
+ channel.Writer.Complete();
+ }
- try
- {
Log.Debug("Waiting for workers to finish working...");
await Task.WhenAll(workers.Select(w => w.Completed));
Log.Debug("All workers finished");
@@ -363,7 +349,9 @@ namespace slskd.Shares
catch (OperationCanceledException)
{
// important! don't try to delete stale files in this case; unscanned records will have a stale timestamp
- Log.Warning("Shared file scan cancelled by user");
+ Log.Warning("Shared file scan cancelled successfully");
+
+ State.SetValue(state => state with { Cancelled = true });
}
Log.Debug("Backing up shared file cache database...");