feat: added librespotConcurrency, which determines the threadpool of librespot api processes

This commit is contained in:
Xoconoch
2025-08-29 09:32:37 -06:00
parent 41db454414
commit e777dbeba2
7 changed files with 119 additions and 12 deletions

View File

@@ -59,7 +59,15 @@ class SSEBroadcaster:
return
# Add global task counts right before broadcasting - this is the single source of truth
enhanced_event_data = add_global_task_counts_to_event(event_data.copy())
# Skip expensive count recomputation for high-frequency callback/progress updates
try:
trigger_reason = event_data.get("trigger_reason")
except Exception:
trigger_reason = None
if trigger_reason and trigger_reason in {"callback_update", "progress_update"}:
enhanced_event_data = event_data.copy()
else:
enhanced_event_data = add_global_task_counts_to_event(event_data.copy())
event_json = json.dumps(enhanced_event_data)
sse_data = f"data: {event_json}\n\n"
@@ -133,14 +141,26 @@ def start_sse_redis_subscriber():
)
elif event_type == "summary_update":
# Task summary update - use standardized trigger
loop.run_until_complete(
trigger_sse_update(
task_id, event_data.get("reason", "update")
# Short-circuit if task no longer exists to avoid expensive processing
try:
if not get_task_info(task_id):
logger.debug(
f"SSE Redis Subscriber: summary_update for missing task {task_id}, skipping"
)
else:
loop.run_until_complete(
trigger_sse_update(
task_id, event_data.get("reason", "update")
)
)
logger.debug(
f"SSE Redis Subscriber: Processed summary update for {task_id}"
)
except Exception as _e:
logger.error(
f"SSE Redis Subscriber: Error handling summary_update for {task_id}: {_e}",
exc_info=True,
)
)
logger.debug(
f"SSE Redis Subscriber: Processed summary update for {task_id}"
)
else:
# Unknown event type - attempt to standardize and broadcast
standardized = standardize_incoming_event(event_data)
@@ -304,7 +324,7 @@ async def trigger_sse_update(task_id: str, reason: str = "task_update"):
# Find the specific task that changed
task_info = get_task_info(task_id)
if not task_info:
logger.warning(f"SSE: Task {task_id} not found for update")
logger.debug(f"SSE: Task {task_id} not found for update")
return
last_status = get_last_task_status(task_id)

View File

@@ -41,6 +41,7 @@ DEFAULT_MAIN_CONFIG = {
"saveCover": True,
"maxConcurrentDownloads": 3,
"utilityConcurrency": 1,
"librespotConcurrency": 2,
"maxRetries": 3,
"retryDelaySeconds": 5,
"retryDelayIncrease": 5,

View File

@@ -44,7 +44,11 @@ def get_client() -> LibrespotClient:
_shared_client.close()
except Exception:
pass
_shared_client = LibrespotClient(stored_credentials_path=desired_blob)
cfg = get_config_params() or {}
max_workers = int(cfg.get("librespotConcurrency", 2) or 2)
_shared_client = LibrespotClient(
stored_credentials_path=desired_blob, max_workers=max_workers
)
_shared_blob_path = desired_blob
return _shared_client
@@ -59,7 +63,9 @@ def create_client(credentials_path: str) -> LibrespotClient:
abs_path = os.path.abspath(credentials_path)
if not os.path.isfile(abs_path):
raise FileNotFoundError(f"Credentials file not found: {abs_path}")
return LibrespotClient(stored_credentials_path=abs_path)
cfg = get_config_params() or {}
max_workers = int(cfg.get("librespotConcurrency", 2) or 2)
return LibrespotClient(stored_credentials_path=abs_path, max_workers=max_workers)
def close_client(client: LibrespotClient) -> None:

View File

@@ -171,7 +171,6 @@ def download_track(
convert_to=convert_to,
bitrate=bitrate,
artist_separator=artist_separator,
spotify_metadata=spotify_metadata,
pad_number_width=pad_number_width,
)
print(