Skip to content

armgit / peach

Public Imported from GitHub
0

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

perf(api): 处理任务状态只回页面读的字段

longmeidaoaab4d4dc78d1
diff --git a/src/peach/web_library_processing.py b/src/peach/web_library_processing.py
index a3cc1a35e..88716f18f 100644
--- a/src/peach/web_library_processing.py
+++ b/src/peach/web_library_processing.py
@@ -9,6 +9,26 @@ from .processing_control import control_for, initial_estimate, valid_mode, mode_
ACTIVE = ('running', 'pausing', 'paused')
+#: 回给页面的状态字段:`frontend/src/react/library-processing/library-processing.ts` 的
+#: `LibraryProcessingData`,加上对应任务中心那一行的 `run_id`。状态文件里另有续跑用的 `local_metadata`(带完整媒体路径)、
+#: `asset_ids`、`completed_metadata` 等大字段,页面一个都不读;照发的话每页每 10 秒轮询一次
+#: 就是两兆多,还把媒体路径交给了浏览器。续跑与重试读的是 `_current()`,不经过这里。
+PUBLIC_FIELDS = frozenset({
+ 'run_id', 'metadata_only', 'status', 'speed_mode', 'eta_seconds_min', 'eta_seconds_max', 'eta_kind',
+ 'eta_scope', 'eta_initial', 'job_id', 'stage', 'requested_stage', 'steps', 'checked', 'total',
+ 'scanned', 'scanned_bytes', 'identified', 'candidates', 'auto_applied', 'covers', 'previews',
+ 'performer_aliases', 'performer_avatars', 'performer_profile_conflicts',
+ 'performer_profile_failed', 'error', 'issue_count', 'paused_count', 'issue_preview',
+ 'issues_log', 'issues_truncated', 'retryable_asset_ids', 'notes', 'current_asset_id',
+ 'current_asset_name', 'current_action', 'current_started_at', 'last_progress_at', 'stalled',
+ 'waited_seconds', 'completed_at',
+})
+
+
+def public_state(state):
+ """任务状态的页面投影:只留 `PUBLIC_FIELDS`。"""
+ return {key: value for key, value in state.items() if key in PUBLIC_FIELDS}
+
def _current(contract):
live = contract.library_processing_job.snapshot()
@@ -40,7 +60,7 @@ def q_library_processing(contract, _args):
path = issues_path(settings_file.active(), str(state['job_id']))
if path.is_file():
state['issues_log'] = str(path)
- return state
+ return public_state(state)
def _control_action(contract, config, body):
@@ -183,10 +203,10 @@ def w_library_processing(contract, body, *, resume=False):
return _control_action(contract, config, body)
previous = _current(contract)
if previous.get('status') in ACTIVE and not resume:
- return previous
+ return public_state(previous)
retry_ids, asset_ids, stage = _processing_request(contract, config, previous, body)
if retry_ids is not None and not retry_ids:
- return previous
+ return public_state(previous)
mode = valid_mode(control_for(config).mode if resume else
(body or {}).get('speed_mode') or control_for(config).mode)
control_for(config).request('set_speed', mode=mode)
@@ -209,8 +229,8 @@ def w_library_processing(contract, body, *, resume=False):
# 人会以为点错了。
labels = {ALL_STAGES: '准备处理', SCAN_STAGE: '准备扫描文件', COLLECT_STAGE: '准备采集资料',
METADATA_STAGE: '准备采集资料', PREVIEWS_STAGE: '准备生成预览图'}
- return contract.library_processing_job.start(
+ return public_state(contract.library_processing_job.start(
work, restart=True, initial={'stage': labels[stage], 'requested_stage': stage,
**initial_estimate(config, contract.db_path, stage, asset_ids or retry_ids, mode)},
recovery={'stage': stage, 'speed_mode': mode, **({'asset_ids': asset_ids or retry_ids}
- if asset_ids is not None or retry_ids is not None else {})})
+ if asset_ids is not None or retry_ids is not None else {})}))
diff --git a/tests/test_metadata_library.py b/tests/test_metadata_library.py
index 3f96c8723..323a06649 100644
--- a/tests/test_metadata_library.py
+++ b/tests/test_metadata_library.py
@@ -2423,6 +2423,30 @@ class LibraryWatchdogTests(unittest.TestCase):
with patch('peach.web_library_processing.settings_file.active', return_value=config):
self.assertEqual(q_library_processing(contract, {})['issues_log'], str(log))
+ def test_status_responses_leave_resume_fields_and_media_paths_on_the_server(self):
+ """状态文件里续跑用的大字段(含完整媒体路径)不随每 10 秒一次的轮询发给页面。"""
+ from peach.web_library_processing import q_library_processing, w_library_processing
+ config = self._config(self.root / 'media')
+ state = {'status': 'failed', 'job_id': 'old', 'requested_stage': 'all', 'checked': 2,
+ 'retryable_asset_ids': [7], 'asset_ids': [7, 8], 'completed_metadata': [8],
+ 'local_metadata': {'7': {'path': 'R:\\media\\secret\\a.mp4'}}, 'progress_seq': 3}
+ state_path(config).parent.mkdir(parents=True, exist_ok=True)
+ state_path(config).write_text(json.dumps(state), encoding='utf-8')
+ contract = Mock()
+ contract.db_path = self.root / 'database/ledger.db'
+ contract.library_processing_job.snapshot.return_value = None
+ with patch('peach.web_library_processing.settings_file.active', return_value=config):
+ read = q_library_processing(contract, {})
+ # 正在跑时再点一次开始,回的是当前任务,同样只回投影。
+ contract.library_processing_job.snapshot.return_value = {**state, 'status': 'running'}
+ again = w_library_processing(contract, {})
+ self.assertEqual((read['status'], read['checked'], read['retryable_asset_ids']), ('failed', 2, [7]))
+ self.assertEqual((again['status'], again['requested_stage']), ('running', 'all'))
+ for response in (read, again):
+ for field in ('asset_ids', 'completed_metadata', 'local_metadata', 'progress_seq'):
+ self.assertNotIn(field, response)
+ self.assertNotIn('secret', json.dumps(response))
+
@windows_ledger_roots
def test_each_issue_names_the_item_its_path_and_where_the_full_log_is(self):
"""一句「NFO 无法解析」加一个链接,是哪个文件得逐个点开才知道;改名或去磁盘上