Fix: Fixed an issue where the Sync Now button became unresponsive due to duplicate API calls.

This commit is contained in:
2025-12-02 09:55:57 +09:00
parent 15e7636a92
commit aa4517aaf5
2 changed files with 2 additions and 50 deletions
-50
View File
@@ -473,56 +473,6 @@ async def sync_events(request: Request):
return StreamingResponse(event_generator(), media_type="text/event-stream")
@app.post("/api/sync")
async def api_sync(payload: SyncRequest):
server_config.load()
try:
sync_mode = payload.mode or SyncMode(server_config.sync_mode)
except ValueError as exc:
raise HTTPException(status_code=400, detail=str(exc))
# Update config temporarily for this sync if needed, but sync_manager reads from config.
# If payload overrides config, we might need to handle that.
# However, sync_manager._perform_sync reads from server_config.
# If we want to support one-off sync with custom params via sync_manager, we need to update sync_manager.
# For now, let's assume payload params should be saved or used.
# But sync_manager is designed to run background tasks too.
# If we want to keep the existing behavior of api_sync (blocking and returning stats),
# we can use sync_manager.run_sync(wait=True).
# But we need to make sure sync_manager uses the params from payload if provided.
# Since sync_manager reads from server_config, let's update server_config if payload has values.
# Or better, pass params to sync_manager.run_sync?
# sync_manager._perform_sync currently hardcodes reading from server_config.
# Let's stick to the requirement: "watchdog当发现更改时,执行同步,同步时UI页面也会显示正在同步状态。"
# This implies we need a shared state.
# If I change api_sync to use sync_manager, I need to ensure it supports the custom params.
# But payload.local_path and payload.mode are optional.
# Let's modify sync_manager to accept overrides.
# But wait, sync_manager is a singleton.
# For this task, I will just wrap the existing logic in sync_manager.run_sync(wait=True)
# AND I will modify sync_manager to allow passing explicit args to _perform_sync.
# But first, let's update api_sync to use sync_manager.run_sync(wait=True)
# AND we need to handle the parameter passing.
# Actually, looking at sync_manager implementation I just wrote:
# def _perform_sync(self):
# server_config.load()
# return sync_all_playlists(local_dir=server_config.local_path, mode=SyncMode(server_config.sync_mode))
# It ignores arguments. This is a limitation.
# I should update SyncManager to accept kwargs for sync_all_playlists.
# Let's update SyncManager first.
pass
@app.post("/api/sync")
async def api_sync(payload: SyncRequest):
server_config.load()
+2
View File
@@ -159,6 +159,7 @@ def _read_text_if_exists(path: str) -> tuple[str, bool]:
def _save_playlist_to_folder(filename: str, paths: Sequence[str], folder: str) -> str:
_ensure_test_dir(folder)
file_path = os.path.join(folder, filename)
logger.info(f"Saving playlist to: {file_path}")
new_content = save_paths(paths)
@@ -585,6 +586,7 @@ def sync_all_playlists(
server_config.load()
compiled_rules = _compile_regex_rules(server_config.path_rules)
_ensure_test_dir(test_folder)
logger.info(f"Syncing playlists to test folder: {test_folder}")
local_playlists = _load_local_playlists(local_dir)
remote_playlists = _fetch_remote_playlists()
playlist_names: set[str] = set(local_playlists.keys())