Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

fix(metadata): 定向采集忽略无关离线来源

longmeidaoab8973a95cb7
diff --git a/src/peach/library_processing.py b/src/peach/library_processing.py
index 0f18fe221..91d335663 100644
--- a/src/peach/library_processing.py
+++ b/src/peach/library_processing.py
@@ -1520,6 +1520,37 @@ def _ffprobe_path(config):
return str(choice.path) if choice else None
+def _chosen_asset_paths(db_path, asset_ids):
+ """定向重试只关心这批资产所在的来源根。
+
+ 配置里其他来源离线不能让一条已落地的云下载后处理失败;
+ 如果离线的正是目标资产所在的根,仍保留原有的离线问题。
+ """
+ if not asset_ids:
+ return {}
+ placeholders = ','.join('?' * len(asset_ids))
+ with closing(sqlite3.connect(db_path, timeout=30)) as connection:
+ rows = connection.execute(
+ f"SELECT location,path FROM asset WHERE id IN ({placeholders}) "
+ "AND medium='video' AND disposal IS NULL", asset_ids).fetchall()
+ chosen = {}
+ for source, path in rows:
+ chosen.setdefault(str(source), []).append(translate_ledger_path(path))
+ return {source: tuple(paths) for source, paths in chosen.items()}
+
+
+def _online_source_root(source, root, chosen_paths, *, retrying, issue):
+ translated_root = translate_ledger_path(root)
+ if root_online(translated_root):
+ return translated_root
+ relevant = not retrying or any(
+ path.is_relative_to(translated_root)
+ for path in chosen_paths.get(source, ()))
+ if relevant:
+ issue(None, f'{source} 来源离线', action='reading_local')
+ return None
+
+
def process_library(config, db_path, candidate_root, cover_root, *, location='configured',
report=lambda state: None, provider_factory=None, job_id=None,
retry_ids=None, active=lambda: True, stage=ALL_STAGES,
@@ -1546,6 +1577,7 @@ def process_library(config, db_path, candidate_root, cover_root, *, location='co
path.parent.mkdir(parents=True, exist_ok=True)
retrying = retry_ids is not None
chosen_ids = list(dict.fromkeys(int(value) for value in (retry_ids or [])))
+ chosen_paths = _chosen_asset_paths(db_path, chosen_ids)
with FileLock(str(path) + '.lock', timeout=0):
state = dict(job_id=job_id or uuid.uuid4().hex, status='running',
stage='读取本地资料' if retrying else '扫描文件',
@@ -1620,10 +1652,11 @@ def process_library(config, db_path, candidate_root, cover_root, *, location='co
for source, roots in locations.items():
for root in roots:
guard.check()
- if not root_online(translate_ledger_path(root)):
- issue(None, f'{source} 来源离线', action='reading_local')
+ translated_root = _online_source_root(
+ source, root, chosen_paths, retrying=retrying, issue=issue)
+ if translated_root is None:
continue
- online_roots.append(translate_ledger_path(root))
+ online_roots.append(translated_root)
if not retrying and stage != COLLECT_STAGE:
result = scan_location(db_path, source, root, declared_roots=config.locations,
mounts=mounts, report=lambda line: update(stage='扫描文件'))
diff --git a/tests/test_metadata_library.py b/tests/test_metadata_library.py
index c49b958aa..cdd3ee431 100644
--- a/tests/test_metadata_library.py
+++ b/tests/test_metadata_library.py
@@ -1968,6 +1968,53 @@ class LibraryWatchdogTests(unittest.TestCase):
self.assertEqual(retried['checked'], 1)
self.assertEqual(retried['status'], 'complete')
+ @windows_ledger_roots
+ def test_retry_ignores_an_unrelated_offline_source(self):
+ missing = self.root / 'missing-local'
+ cloud = self.root / 'cloud'
+ cloud.mkdir()
+ (cloud / 'ABW-203.mp4').write_bytes(b'video')
+ db = fresh_ledger(self.root)
+ config = PeachConfig(
+ self.root, self.root / 'config.toml', present=True,
+ locations={'local': (str(missing),), '115': (str(cloud),)})
+ first = process_library(
+ config, db, self.root / 'generated', self.root / 'covers', stage='scan',
+ provider_factory=lambda: self._provider())
+ self.assertEqual(first['status'], 'failed')
+ self.assertEqual(first['issue_preview'][0]['message'], 'local 来源离线')
+ with closing(sqlite3.connect(db)) as connection:
+ asset_id = connection.execute(
+ "SELECT id FROM asset WHERE name='ABW-203.mp4'").fetchone()[0]
+ retried = process_library(
+ config, db, self.root / 'generated', self.root / 'covers',
+ retry_ids=[asset_id], stage='collect', provider_factory=lambda: self._provider())
+ self.assertEqual(
+ (retried['status'], retried['total'], retried['checked'], retried['issue_count']),
+ ('complete', 1, 1, 0))
+
+ @windows_ledger_roots
+ def test_retry_still_reports_the_target_source_offline(self):
+ cloud = self.root / 'cloud'
+ cloud.mkdir()
+ (cloud / 'ABW-204.mp4').write_bytes(b'video')
+ db = fresh_ledger(self.root)
+ config = PeachConfig(
+ self.root, self.root / 'config.toml', present=True,
+ locations={'115': (str(cloud),)})
+ process_library(
+ config, db, self.root / 'generated', self.root / 'covers', stage='scan',
+ provider_factory=lambda: self._provider())
+ with closing(sqlite3.connect(db)) as connection:
+ asset_id = connection.execute(
+ "SELECT id FROM asset WHERE name='ABW-204.mp4'").fetchone()[0]
+ cloud.rename(self.root / 'offline-cloud')
+ retried = process_library(
+ config, db, self.root / 'generated', self.root / 'covers',
+ retry_ids=[asset_id], stage='collect', provider_factory=lambda: self._provider())
+ self.assertEqual((retried['status'], retried['issue_count']), ('failed', 1))
+ self.assertEqual(retried['issue_preview'][0]['message'], '115 来源离线')
+
@windows_ledger_roots
def test_scanning_alone_registers_the_files_and_asks_no_source(self):
"""只扫描那一段登记完文件就收工,不读本地资料也不联网。