Make library sync all-or-nothing so an interrupted sync keeps the previous cache - #32
Open
claude-on-call wants to merge 1 commit into
Open
claude-on-call wants to merge 1 commit into
claude-on-call wants to merge 1 commit into
Conversation
sync_library deleted the cache and committed, then committed each batch, so a sync interrupted partway (container restart, OOM kill, Plex dropping out) left a partial track table with track_count 0, and nothing resumed it. The delete, inserts and sync_state update now run in one transaction: a failure rolls back, a hard kill is discarded by SQLite, and the previous complete cache is kept. WAL mode keeps readers on that snapshot until the commit. A failed sync now keeps the previous cache instead of emptying it, so test_failed_sync_resets_cache_state is renamed and inverted, and a test for a failure after several batches is added. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
sync_librarydeletes every cached track and commits, then inserts and commits in batches of 500. If a sync is interrupted partway (the container is restarted or updated mid-sync, the process is OOM-killed, or Plex drops the connection), the cache is left with a partial track table andtrack_count = 0. Nothing resumes the sync after a restart, so the UI reports the library as not synced until someone re-syncs by hand.We hit this on a ~25k-track library: a container update during a sync left 10,500 tracks and a count of 0.
Change
The delete, the inserts and the
sync_stateupdate now run in one transaction, committed once at the end.Either way the previous complete cache is kept. The database is already in WAL mode, so while a sync runs, readers (filters, counts, generation) keep seeing the previous snapshot instead of a half-filled table.
Behaviour change
test_failed_sync_resets_cache_statedeliberately asserted the opposite: a failed sync empties the cache "to avoid using stale data". This PR keeps the last complete sync instead. A day-old snapshot of a music library is almost always more useful than an empty or partial one, and saving a playlist already skips tracks that no longer exist in Plex.The test is renamed to
test_failed_sync_keeps_previous_cachewith its asserts inverted. Happy to rework this if you'd rather keep the old behaviour.Testing
test_sync_failing_midway_keeps_previous_cache: several batches are written, then the sync fails; the previous tracks and count are intact.os._exit) after 1,000 of 4,000 tracks leaves the previous cache intact, and the next sync succeeds.pytest: all tests pass. Running on a live instance.🤖 Generated with Claude Code