Threads the rename_only flag from the apply endpoint to the executor, additively (default False everywhere → existing full-flow behaviour byte-for-byte unchanged): - /api/library/album/<id>/reorganize-files reads `rename_only` from the body → enqueue. - QueueItem gains rename_only (+ surfaced in to_dict for the status panel). - reorganize_runner.build_runner takes build_final_path_fn and branches: a rename_only item routes to reorganize_album_rename_only (no staging dir, no copy, no post-process); everything else falls through to the full reorganize_album. Staging is only created for the full path now. - web_server injects build_final_path_fn (= _build_final_path_for_track, the same builder the preview uses) so apply matches the preview exactly. Fixed a test landmine: _make_item returns a MagicMock, whose .rename_only is a truthy mock that wrongly took the new branch — set it to False to match the real QueueItem default. +2 runner tests (rename_only routes to the rename executor + creates no staging; missing path-builder → clean setup_failed). 209 reorganize tests green, ruff clean. Left: the modal (Full vs Rename-only) + optional post-rename server scan + the issue reply.
150 lines
6.4 KiB
Python
150 lines
6.4 KiB
Python
"""Builds the per-item runner closure that the reorganize queue worker
|
|
invokes. Lives outside ``web_server`` so the wiring is unit-testable
|
|
and the monolith stays small.
|
|
|
|
The runner ties three subsystems together:
|
|
|
|
* :func:`core.library_reorganize.reorganize_album` — the orchestrator
|
|
that copies files to staging, matches them against the metadata
|
|
source, and routes each through the post-process pipeline.
|
|
* :func:`core.reorganize_queue.get_queue` — the queue this runner is
|
|
registered with; we forward live progress updates back into the
|
|
active queue item so the status panel can show per-track state.
|
|
* The dependency callbacks injected by ``web_server`` (DB accessor,
|
|
resolve-file-path, post-process function, empty-dir cleanup,
|
|
shutdown signal). These are passed in rather than imported so the
|
|
module stays testable in isolation.
|
|
|
|
Config (download path / transfer path) is read **per run**, not at
|
|
module load. That way a user changing their download path in settings
|
|
takes effect on the next reorganize without needing a server restart.
|
|
"""
|
|
|
|
import os
|
|
from typing import Callable, Optional
|
|
|
|
from utils.logging_config import get_logger
|
|
|
|
logger = get_logger("reorganize_runner")
|
|
|
|
|
|
def build_runner(
|
|
*,
|
|
get_database: Callable[[], object],
|
|
resolve_file_path_fn: Callable[[Optional[str]], Optional[str]],
|
|
post_process_fn: Callable[[str, dict, str], None],
|
|
cleanup_empty_directories_fn: Callable[[str, str], None],
|
|
is_shutting_down_fn: Callable[[], bool],
|
|
get_download_path: Callable[[], str],
|
|
get_transfer_path: Callable[[], str],
|
|
build_final_path_fn: Optional[Callable] = None,
|
|
) -> Callable[[object], dict]:
|
|
"""Return the closure the queue worker invokes per item.
|
|
|
|
Args:
|
|
get_database: Returns the live MusicDatabase singleton.
|
|
resolve_file_path_fn: Resolves a DB-stored file path to the
|
|
actual on-disk path (or ``None`` if missing).
|
|
post_process_fn: ``_post_process_matched_download``. Must set
|
|
``context['_final_processed_path']`` on success.
|
|
cleanup_empty_directories_fn: Called as
|
|
``cleanup_empty_directories_fn(transfer_dir, marker_path)``
|
|
to prune empty source dirs after a track is moved.
|
|
is_shutting_down_fn: Returns True when the server is shutting
|
|
down so the orchestrator can abort early.
|
|
get_download_path: Resolves the user's configured download
|
|
path *at call time* (so config changes apply live).
|
|
get_transfer_path: Same, for the transfer path.
|
|
|
|
Returns:
|
|
A callable ``runner(item)`` suitable for
|
|
:meth:`core.reorganize_queue.ReorganizeQueue.set_runner`.
|
|
"""
|
|
from core.library_reorganize import reorganize_album, reorganize_album_rename_only
|
|
from core.reorganize_queue import get_queue
|
|
|
|
def _update_track_path(track_id, new_path):
|
|
try:
|
|
db = get_database()
|
|
with db._get_connection() as conn:
|
|
conn.execute(
|
|
"UPDATE tracks SET file_path = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?",
|
|
(new_path, str(track_id)),
|
|
)
|
|
conn.commit()
|
|
except Exception as db_err:
|
|
logger.warning(f"[Reorganize] DB path update failed for {track_id}: {db_err}")
|
|
|
|
def runner(item):
|
|
# Read config per-run so the user changing their download path
|
|
# in Settings takes effect on the next reorganize without a
|
|
# server restart.
|
|
download_dir = get_download_path()
|
|
transfer_dir = get_transfer_path()
|
|
|
|
def _cleanup_empty(src_dir):
|
|
try:
|
|
cleanup_empty_directories_fn(transfer_dir, os.path.join(src_dir, '_'))
|
|
except Exception as e:
|
|
logger.debug("cleanup empty dirs failed: %s", e)
|
|
|
|
def _on_progress(updates):
|
|
try:
|
|
get_queue().update_active_progress(queue_id=item.queue_id, **updates)
|
|
except Exception as e:
|
|
# Progress fan-out failures must never break a run.
|
|
logger.debug("reorganize progress fan-out: %s", e)
|
|
|
|
# Rename-only mode (#875): just move files to the current scheme — no staging,
|
|
# no copy, no post-processing. Falls through to the full pipeline otherwise.
|
|
if getattr(item, 'rename_only', False):
|
|
if build_final_path_fn is None:
|
|
return {
|
|
'status': 'setup_failed', 'source': None,
|
|
'total': 0, 'moved': 0, 'skipped': 0, 'failed': 0,
|
|
'errors': [{'error': 'Rename-only mode unavailable (no path builder)'}],
|
|
}
|
|
return reorganize_album_rename_only(
|
|
album_id=item.album_id,
|
|
db=get_database(),
|
|
transfer_dir=transfer_dir,
|
|
resolve_file_path_fn=resolve_file_path_fn,
|
|
build_final_path_fn=build_final_path_fn,
|
|
update_track_path_fn=_update_track_path,
|
|
cleanup_empty_dir_fn=_cleanup_empty,
|
|
on_progress=_on_progress,
|
|
primary_source=item.source,
|
|
strict_source=bool(item.source),
|
|
metadata_source=getattr(item, 'metadata_source', 'api') or 'api',
|
|
stop_check=is_shutting_down_fn,
|
|
)
|
|
|
|
staging_root = os.path.join(download_dir, 'ssync_staging')
|
|
try:
|
|
os.makedirs(staging_root, exist_ok=True)
|
|
except OSError as mk_err:
|
|
logger.error(f"[Reorganize] Cannot create staging dir {staging_root}: {mk_err}")
|
|
return {
|
|
'status': 'setup_failed',
|
|
'source': None,
|
|
'total': 0, 'moved': 0, 'skipped': 0, 'failed': 0,
|
|
'errors': [{'error': f'Could not create staging dir: {mk_err}'}],
|
|
}
|
|
|
|
return reorganize_album(
|
|
album_id=item.album_id,
|
|
db=get_database(),
|
|
staging_root=staging_root,
|
|
resolve_file_path_fn=resolve_file_path_fn,
|
|
post_process_fn=post_process_fn,
|
|
update_track_path_fn=_update_track_path,
|
|
cleanup_empty_dir_fn=_cleanup_empty,
|
|
transfer_dir=transfer_dir,
|
|
on_progress=_on_progress,
|
|
primary_source=item.source,
|
|
strict_source=bool(item.source),
|
|
stop_check=is_shutting_down_fn,
|
|
metadata_source=getattr(item, 'metadata_source', 'api') or 'api',
|
|
)
|
|
|
|
return runner
|