diff options
| author | JP Dillingham <jp@dillingham.me> | 2025-01-20 21:06:52 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-01-20 21:06:52 -0600 |
| commit | a5da8ff17f352eea5ce29cde51d6448c5e2a2f4c (patch) | |
| tree | 86e25f9271fd74e2020804ad5a87b9a64255797c /src | |
| parent | b18f7b00bc1d438bfa78ff16cb1badf6fe255e97 (diff) | |
| parent | 301890c323e2248a97c7340d403288d2e6cc4d35 (diff) | |
| download | slskd-a5da8ff17f352eea5ce29cde51d6448c5e2a2f4c.tar.gz | |
Merge pull request #1281 from slskd/search-cleanup
Clean up dangling Search records on startup
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Application.cs | 28 | ||||
| -rw-r--r-- | src/slskd/Search/SearchService.cs | 33 |
2 files changed, 49 insertions, 12 deletions
diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 05f62902..8453386b 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -307,12 +307,14 @@ namespace slskd { Log.Information("Application started"); + Log.Debug("Cleaning up any dangling records"); + // if the application shut down "uncleanly", transfers may need to be cleaned up. we deliberately don't allow these // records to be updated if the application has started to shut down so that we can do this cleanup and properly // disposition them as having failed due to an application shutdown, instead of some random exception thrown while // things are being disposed. var activeUploads = Transfers.Uploads.List(t => !t.State.HasFlag(TransferStates.Completed), includeRemoved: false) - .Where(t => !t.State.HasFlag(TransferStates.Completed)) // https://github.com/dotnet/efcore/issues/10434 + .Where(t => !t.State.HasFlag(TransferStates.Completed)) // https://github.com/slskd/slskd/issues/1280 .ToList(); foreach (var upload in activeUploads) @@ -324,7 +326,7 @@ namespace slskd } var activeDownloads = Transfers.Downloads.List(t => !t.State.HasFlag(TransferStates.Completed) && !t.Removed) - .Where(t => !t.State.HasFlag(TransferStates.Completed)) // https://github.com/dotnet/efcore/issues/10434 + .Where(t => !t.State.HasFlag(TransferStates.Completed)) // https://github.com/slskd/slskd/issues/1280 .ToList(); foreach (var download in activeDownloads) @@ -335,6 +337,28 @@ namespace slskd Transfers.Downloads.Update(download); } + /* + search records can be left 'dangling' as well. responses are held in memory and saved to the database + when the search is complete, so when a search 'dangles' all responses have been lost. to avoid discrepancies, + we need to zero the response and file counts as well as set the state and EndedAt. + */ + var activeSearches = (await Search.ListAsync(s => s.EndedAt == null || !s.State.HasFlag(SearchStates.Completed))) + .Where(s => s.EndedAt == null || !s.State.HasFlag(SearchStates.Completed)) // https://github.com/slskd/slskd/issues/1280 + .ToList(); + + foreach (var search in activeSearches) + { + Log.Debug("Cleaning up dangling search {Query} started {StartedAt}", search.SearchText, search.StartedAt); + search.Responses = []; + search.ResponseCount = 0; + search.FileCount = 0; + search.LockedFileCount = 0; + search.State = SearchStates.Completed | SearchStates.TimedOut; + search.EndedAt = DateTime.UtcNow; + + Search.Update(search); + } + // save the ids of any downloads that were active, so we can re-enqueue them after we've connected and logged in. // we need to check the database before we re-request to make sure the user didn't remove them from the UI while // the application was running, but before it was logged in. so just save the ids. diff --git a/src/slskd/Search/SearchService.cs b/src/slskd/Search/SearchService.cs index 809647b2..f91714f5 100644 --- a/src/slskd/Search/SearchService.cs +++ b/src/slskd/Search/SearchService.cs @@ -65,6 +65,15 @@ namespace slskd.Search Task<List<Search>> ListAsync(Expression<Func<Search, bool>> expression = null); /// <summary> + /// Updates the specified <paramref name="search"/>. + /// </summary> + /// <remark> + /// Round-trips the database; use accordingly. + /// </remark> + /// <param name="search">The search to update.</param> + void Update(Search search); + + /// <summary> /// Performs a search for the specified <paramref name="query"/> and <paramref name="scope"/>. /// </summary> /// <param name="id">A unique identifier for the search.</param> @@ -192,6 +201,17 @@ namespace slskd.Search } /// <summary> + /// Updates the specified <paramref name="search"/>. + /// </summary> + /// <param name="search">The search to update.</param> + public void Update(Search search) + { + using var context = ContextFactory.CreateDbContext(); + context.Update(search); + context.SaveChanges(); + } + + /// <summary> /// Performs a search for the specified <paramref name="query"/> and <paramref name="scope"/>. /// </summary> /// <param name="id">A unique identifier for the search.</param> @@ -223,20 +243,13 @@ namespace slskd.Search List<SearchResponse> responses = new(); - void UpdateAndSaveChanges(Search search) - { - using var context = ContextFactory.CreateDbContext(); - context.Update(search); - context.SaveChanges(); - } - options ??= new SearchOptions(); options = options.WithActions( stateChanged: (args) => { search = search.WithSoulseekSearch(args.Search); SearchHub.BroadcastUpdateAsync(search); - UpdateAndSaveChanges(search); + Update(search); }, responseReceived: (args) => rateLimiter.Invoke(() => { @@ -250,7 +263,7 @@ namespace slskd.Search // note that we're not actually doing anything with the response here, that's happening in the // response handler. we're only updating counts here. SearchHub.BroadcastUpdateAsync(search); - UpdateAndSaveChanges(search); + Update(search); })); var soulseekSearchTask = Client.SearchAsync( @@ -278,7 +291,7 @@ namespace slskd.Search search.EndedAt = DateTime.UtcNow; search.Responses = responses.Select(r => Response.FromSoulseekSearchResponse(r)); - UpdateAndSaveChanges(search); + Update(search); // zero responses before broadcasting, as we don't want to blast this // data out over the SignalR socket |
