aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2021-05-29 16:54:14 -0500
committerJP Dillingham <jpdillingham@gmail.com>2021-05-29 16:54:14 -0500
commit9ba6bedaac35cc31d23fc3ba4808f813b214cb91 (patch)
treecff6d725bd5fdd3e6a4f634961f0c4d7538b87d8 /src
parentc3108fcdb83d1c4ad032971dcb2be1d9bdc04983 (diff)
downloadslskd-9ba6bedaac35cc31d23fc3ba4808f813b214cb91.tar.gz
prevent out of band search updates, save final state
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Core/Search/Services/ISearchService.cs3
-rw-r--r--src/slskd/Core/Search/Services/SearchService.cs27
2 files changed, 15 insertions, 15 deletions
diff --git a/src/slskd/Core/Search/Services/ISearchService.cs b/src/slskd/Core/Search/Services/ISearchService.cs
index f56f2ab3..d696617d 100644
--- a/src/slskd/Core/Search/Services/ISearchService.cs
+++ b/src/slskd/Core/Search/Services/ISearchService.cs
@@ -37,9 +37,8 @@ namespace slskd.Search
/// <param name="query">The search query.</param>
/// <param name="scope">The search scope.</param>
/// <param name="options">Search options.</param>
- /// <param name="includeResponses">A value indicating whether to include search responses in the result.</param>
/// <returns>The completed search.</returns>
- Task<Search> CreateAsync(Guid id, SearchQuery query, SearchScope scope, SearchOptions options = null, bool includeResponses = true);
+ Task<Search> CreateAsync(Guid id, SearchQuery query, SearchScope scope, SearchOptions options = null);
/// <summary>
/// Finds a single search matching the specified <paramref name="expression"/>.
diff --git a/src/slskd/Core/Search/Services/SearchService.cs b/src/slskd/Core/Search/Services/SearchService.cs
index 48f7e600..e3c1bbef 100644
--- a/src/slskd/Core/Search/Services/SearchService.cs
+++ b/src/slskd/Core/Search/Services/SearchService.cs
@@ -70,7 +70,7 @@ namespace slskd.Search
/// <param name="options">Search options.</param>
/// <param name="includeResponses">A value indicating whether to include search responses in the result.</param>
/// <returns>The completed search.</returns>
- public async Task<Search> CreateAsync(Guid id, SearchQuery query, SearchScope scope, SearchOptions options = null, bool includeResponses = true)
+ public async Task<Search> CreateAsync(Guid id, SearchQuery query, SearchScope scope, SearchOptions options = null)
{
var token = Client.GetNextToken();
var cancellationTokenSource = new CancellationTokenSource();
@@ -110,16 +110,15 @@ namespace slskd.Search
options,
cancellationToken: cancellationTokenSource.Token);
+ CancellationTokens.TryRemove(id, out _);
+
search.FileCount = soulseekSearch.FileCount;
search.LockedFileCount = soulseekSearch.LockedFileCount;
search.ResponseCount = soulseekSearch.ResponseCount;
search.State = soulseekSearch.State;
search.EndedAt = DateTime.UtcNow;
-
- if (includeResponses)
- {
- search.Responses = responses;
- }
+ search.Responses = responses;
+ SaveSearchState(search);
return search;
}
@@ -185,20 +184,22 @@ namespace slskd.Search
private void UpdateSearchState(Search search, SoulseekSearch soulseekSearch)
{
- _ = Task.Run(async () =>
+ if (CancellationTokens.ContainsKey(search.Id))
{
search.FileCount = soulseekSearch.FileCount;
search.LockedFileCount = soulseekSearch.LockedFileCount;
search.ResponseCount = soulseekSearch.ResponseCount;
search.State = soulseekSearch.State;
- var context = ContextFactory.CreateDbContext();
+ SaveSearchState(search);
+ }
+ }
- context.Update(search);
- await context.SaveChangesAsync();
- }).ContinueWith(task =>
- Log.LogError(task.Exception, "Failed to update state of {SearchId}", search.Id),
- TaskContinuationOptions.OnlyOnFaulted);
+ private void SaveSearchState(Search search)
+ {
+ var context = ContextFactory.CreateDbContext();
+ context.Update(search);
+ context.SaveChanges();
}
}
} \ No newline at end of file