Wait for each Plex Wish List add batch to actually finish before sending the next #49

Merged
neerdoc merged 1 commit from macsome_8 into main 2026-07-28 20:02:25 +00:00
Owner

Chunking into batches of 25 (previous commit) wasn't enough on its own:
live logs showed a 496-track add reporting success in ~20 seconds, while
Music Assistant's own background-tasks view showed multiple "Add items to
playlist" tasks still queued/running well after our sync had already
finished. Confirmed in MA's own server source
(controllers/music/media/playlists.py): add_playlist_tracks queues a
managed background task and returns immediately without awaiting it -
return self.mass.tasks.run_background_task(...). Firing 20 batches ~1s
apart queued a backlog faster than MA could actually process it.

The server does return that BackgroundTask (with an id we can poll via
tasks/get, Scope.SYSTEM_READ) - the music_assistant_client wrapper we were
calling just discards it and returns None. Now calls client.send_command
directly to get the task id back, then polls tasks/get until the task
reaches a real terminal status (success/partial_success/failed/cancelled)
before moving to the next batch, and reports accurate added/failed counts
based on the task's actual outcome instead of assuming success whenever
the initial RPC call didn't raise.

Falls back to the old assume-success behavior if the server response
doesn't include a task id (older MA versions), and if tasks/get itself
fails (e.g. the configured MA token lacks permission), the batch is
counted as failed rather than crashing the sync.

Chunking into batches of 25 (previous commit) wasn't enough on its own: live logs showed a 496-track add reporting success in ~20 seconds, while Music Assistant's own background-tasks view showed multiple "Add items to playlist" tasks still queued/running well after our sync had already finished. Confirmed in MA's own server source (controllers/music/media/playlists.py): add_playlist_tracks queues a managed background task and returns immediately without awaiting it - `return self.mass.tasks.run_background_task(...)`. Firing 20 batches ~1s apart queued a backlog faster than MA could actually process it. The server does return that BackgroundTask (with an id we can poll via tasks/get, Scope.SYSTEM_READ) - the music_assistant_client wrapper we were calling just discards it and returns None. Now calls client.send_command directly to get the task id back, then polls tasks/get until the task reaches a real terminal status (success/partial_success/failed/cancelled) before moving to the next batch, and reports accurate added/failed counts based on the task's actual outcome instead of assuming success whenever the initial RPC call didn't raise. Falls back to the old assume-success behavior if the server response doesn't include a task id (older MA versions), and if tasks/get itself fails (e.g. the configured MA token lacks permission), the batch is counted as failed rather than crashing the sync.
Chunking into batches of 25 (previous commit) wasn't enough on its own:
live logs showed a 496-track add reporting success in ~20 seconds, while
Music Assistant's own background-tasks view showed multiple "Add items to
playlist" tasks still queued/running well after our sync had already
finished. Confirmed in MA's own server source
(controllers/music/media/playlists.py): add_playlist_tracks queues a
managed background task and returns immediately without awaiting it -
`return self.mass.tasks.run_background_task(...)`. Firing 20 batches ~1s
apart queued a backlog faster than MA could actually process it.

The server does return that BackgroundTask (with an id we can poll via
tasks/get, Scope.SYSTEM_READ) - the music_assistant_client wrapper we were
calling just discards it and returns None. Now calls client.send_command
directly to get the task id back, then polls tasks/get until the task
reaches a real terminal status (success/partial_success/failed/cancelled)
before moving to the next batch, and reports accurate added/failed counts
based on the task's actual outcome instead of assuming success whenever
the initial RPC call didn't raise.

Falls back to the old assume-success behavior if the server response
doesn't include a task id (older MA versions), and if tasks/get itself
fails (e.g. the configured MA token lacks permission), the batch is
counted as failed rather than crashing the sync.
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
WulfScience-FOSS/ha-music-own!49
No description provided.