Follow-up to the personalized-playlists standardization PR. New
`personalized_pipeline` automation action syncs selected discover-
page playlists (Hidden Gems / Discovery Shuffle / Time Machine /
Genre / Daily Mix / Fresh Tape / The Archives / Seasonal Mix) to
the active media server + queues missing tracks for download.
Same pattern as the existing mirrored `playlist_pipeline` but two
phases instead of four — no REFRESH (no external source to re-pull)
and no DISCOVER (manager-backed snapshots are already metadata-
matched). Pipeline shape:
SNAPSHOT → SYNC → WISHLIST
Where SNAPSHOT either reads the persisted track list from
`PersonalizedPlaylistManager` (default) or refreshes it first when
`refresh_first=true` (cron use case: regenerate Hidden Gems nightly
and sync the fresh set).
Shared helper extraction:
PHASE 3 (SYNC loop) + PHASE 4 (WISHLIST tail) lifted out of mirrored
`playlist_pipeline` into `core/automation/handlers/_pipeline_shared.py`
as `run_sync_and_wishlist(deps, automation_id, playlists, sync_one_fn,
sync_id_for_fn, ...)`. Both pipelines call it. Mirrored injects
`auto_sync_playlist` as the per-playlist sync function; personalized
injects a thin wrapper that launches `_run_sync_task` directly with
a pre-built tracks_json. Same sync-state polling / progress emission
/ status counting / wishlist trigger logic — 0 duplication.
Files added:
- core/automation/handlers/_pipeline_shared.py
- core/automation/handlers/personalized_pipeline.py
- tests/automation/test_handlers_personalized_pipeline.py
Files changed:
- core/automation/handlers/playlist_pipeline.py: PHASE 3+4 replaced
with shared helper call (~100 lines deleted, 1 helper invocation
added; behavior identical).
- core/automation/deps.py: new `build_personalized_manager` field
(lazy builder so the pipeline gets a fresh PersonalizedPlaylistManager
per run).
- core/automation/handlers/__init__.py + registration.py: register
`personalized_pipeline` action with the shared `pipeline_running`
guard so it can't overlap mirrored.
- core/automation/blocks.py: new `personalized_pipeline` block
declaration with config_fields (kinds multi-select, refresh_first,
skip_wishlist).
- web_server.py: thread `_build_personalized_manager` into
AutomationDeps construction.
- All 5 automation test fixtures: `_build_deps` adds
`build_personalized_manager=lambda: None` stub.
- tests/automation/test_handler_registration.py:
EXPECTED_ACTION_NAMES + EXPECTED_GUARDED_ACTIONS gain
`personalized_pipeline`.
Trigger schema:
{
"_automation_id": "...",
"kinds": [
{"kind": "hidden_gems"},
{"kind": "time_machine", "variant": "1980s"},
{"kind": "seasonal_mix", "variant": "halloween"}
],
"refresh_first": false,
"skip_wishlist": false
}
Tests (14 new, 178 automation total):
- _track_to_sync_shape: basic shape, source ID fallback chain,
no-id returns empty string
- empty config / non-list kinds / empty kinds list all return
error + clear pipeline_running flag
- _build_payloads_for_kinds: skips invalid entries, skips kinds
with no tracks, refresh_first vs ensure dispatch, payload shape
+ sync_id format, manager exception swallowed continues
- _sync_personalized_playlist: launches background thread + returns
status='started'
- happy path: stubbed sync_states drives helper to completion, flag
cleaned up
Full suite: 3383 passed.
Note: the trigger UI block declares config_fields but the frontend
doesn't yet render the `personalized_playlist_select` multi-select
type — usable today via API; polished UI ships in a follow-up
frontend PR.
146 lines
5.8 KiB
Python
146 lines
5.8 KiB
Python
"""Dependency-injection surface for automation handlers.
|
|
|
|
Each handler in ``core.automation.handlers`` is a top-level pure
|
|
function that accepts ``(config: dict, deps: AutomationDeps)`` instead
|
|
of reaching for module-level globals in ``web_server``. The deps
|
|
namespace bundles every callable, client, and mutable-state container
|
|
the handlers need.
|
|
|
|
Construction happens once at app startup in ``web_server.py``:
|
|
|
|
from core.automation.deps import AutomationDeps, AutomationState
|
|
state = AutomationState()
|
|
deps = AutomationDeps(
|
|
engine=automation_engine,
|
|
state=state,
|
|
get_database=get_database,
|
|
spotify_client=spotify_client,
|
|
...
|
|
)
|
|
register_all(deps)
|
|
|
|
Tests construct a fake ``AutomationDeps`` with stub callables — every
|
|
handler is then exercisable without spinning up Flask, the DB, or
|
|
the real media-server clients.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import threading
|
|
from dataclasses import dataclass, field
|
|
from typing import Any, Callable, Optional
|
|
|
|
|
|
@dataclass
|
|
class AutomationState:
|
|
"""Mutable flags shared across handler invocations.
|
|
|
|
Pre-refactor each was a ``global`` or ``nonlocal`` variable inside
|
|
the registration closure. Lifted here so handlers + their guards
|
|
can read/write a single object instead of importing globals.
|
|
|
|
All mutations should hold ``lock``; the helper methods below do
|
|
so for the common get/set patterns.
|
|
"""
|
|
|
|
scan_library_automation_id: Optional[str] = None
|
|
db_update_automation_id: Optional[str] = None
|
|
pipeline_running: bool = False
|
|
lock: threading.Lock = field(default_factory=threading.Lock)
|
|
|
|
def is_scan_library_active(self) -> bool:
|
|
with self.lock:
|
|
return self.scan_library_automation_id is not None
|
|
|
|
def is_pipeline_running(self) -> bool:
|
|
with self.lock:
|
|
return self.pipeline_running
|
|
|
|
def set_scan_library_id(self, automation_id: Optional[str]) -> None:
|
|
with self.lock:
|
|
self.scan_library_automation_id = automation_id
|
|
|
|
def set_pipeline_running(self, value: bool) -> None:
|
|
with self.lock:
|
|
self.pipeline_running = value
|
|
|
|
|
|
@dataclass
|
|
class AutomationDeps:
|
|
"""Bundle of every callable + client an automation handler may need.
|
|
|
|
Add fields as new handlers are extracted. Every field is required
|
|
at construction (no defaults) so a missing dep fails loudly at
|
|
startup, not silently mid-handler.
|
|
"""
|
|
|
|
# --- Engine + shared state ---
|
|
engine: Any # AutomationEngine instance
|
|
state: AutomationState
|
|
config_manager: Any # config.settings.ConfigManager singleton
|
|
update_progress: Callable[..., None] # _update_automation_progress
|
|
logger: Any # module-level logger from utils.logging_config
|
|
|
|
# --- Service clients (each may be None depending on user config) ---
|
|
get_database: Callable[[], Any] # late-binding so tests don't need DB
|
|
spotify_client: Any
|
|
tidal_client: Any
|
|
web_scan_manager: Any
|
|
|
|
# --- Background-task entry points ---
|
|
process_wishlist_automatically: Callable[..., Any]
|
|
process_watchlist_scan_automatically: Callable[..., Any]
|
|
is_wishlist_actually_processing: Callable[[], bool]
|
|
is_watchlist_actually_scanning: Callable[[], bool]
|
|
get_watchlist_scan_state: Callable[[], dict] # accessor returns the live mutable dict
|
|
|
|
# --- Playlist pipeline entry points ---
|
|
run_playlist_discovery_worker: Callable[..., Any]
|
|
run_sync_task: Callable[..., Any]
|
|
load_sync_status_file: Callable[[], dict]
|
|
get_deezer_client: Callable[[], Any]
|
|
parse_youtube_playlist: Callable[[str], Any]
|
|
get_sync_states: Callable[[], dict] # accessor returns the live dict shared with the sync UI
|
|
|
|
# --- Database update + quality scanner (shared state + executors) ---
|
|
set_db_update_automation_id: Callable[[Optional[str]], None] # syncs the legacy `_db_update_automation_id` global so the live DB-update progress callbacks (which still read the global directly) keep firing for the active automation
|
|
get_db_update_state: Callable[[], dict]
|
|
db_update_lock: Any # threading.Lock
|
|
db_update_executor: Any # ThreadPoolExecutor
|
|
run_db_update_task: Callable[..., Any]
|
|
run_deep_scan_task: Callable[..., Any]
|
|
get_duplicate_cleaner_state: Callable[[], dict]
|
|
duplicate_cleaner_lock: Any
|
|
duplicate_cleaner_executor: Any
|
|
run_duplicate_cleaner: Callable[..., Any]
|
|
get_quality_scanner_state: Callable[[], dict]
|
|
quality_scanner_lock: Any
|
|
quality_scanner_executor: Any
|
|
run_quality_scanner: Callable[..., Any]
|
|
|
|
# --- Download orchestrator + queue accessors ---
|
|
download_orchestrator: Any
|
|
run_async: Callable[..., Any]
|
|
tasks_lock: Any
|
|
get_download_batches: Callable[[], dict]
|
|
get_download_tasks: Callable[[], dict]
|
|
sweep_empty_download_directories: Callable[[], int]
|
|
get_staging_path: Callable[[], str]
|
|
|
|
# --- Maintenance helpers ---
|
|
docker_resolve_path: Callable[[str], str]
|
|
get_current_profile_id: Callable[[], int]
|
|
get_watchlist_scanner: Callable[[Any], Any]
|
|
get_app: Callable[[], Any] # Flask app for test_client (beatport refresh)
|
|
get_beatport_data_cache: Callable[[], dict]
|
|
|
|
# --- Progress + history callbacks (used by register_all to wire
|
|
# the engine's progress callback hooks). ---
|
|
init_automation_progress: Callable[..., Any]
|
|
record_progress_history: Callable[..., Any]
|
|
|
|
# --- Personalized playlist pipeline ---
|
|
# Lazy builder so the pipeline handler can construct a fresh
|
|
# PersonalizedPlaylistManager per run (cheap accessors inside,
|
|
# no caching needed yet).
|
|
build_personalized_manager: Callable[[], Any]
|