Skip to content

armgit / peach

Public Imported from GitHub
0

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

fix(processing): 清理完成前阻止重试并保留当前失败

longmeidao8a96c5afd8a7
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e675aff87..0c776e2b2 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -21,6 +21,10 @@ Removed / Fixed / Security。
## [未发布]
+### 修复
+
+- **馆藏**:处理失败后的线程清理完成前保留停止中状态,避免立即重试撞上仍持有的处理锁;起步失败时显示本次错误,不再退回上一轮结果。
+
## [0.51.0] - 2026-10-28
作者从 2026 年 10 月 29 日开始游玩《影之刃零》,通关前会暂缓更新和维护。
diff --git a/src/peach/web_library_processing.py b/src/peach/web_library_processing.py
index 678462363..fbb898484 100644
--- a/src/peach/web_library_processing.py
+++ b/src/peach/web_library_processing.py
@@ -34,9 +34,17 @@ def public_state(state):
def _current(contract):
live = contract.library_processing_job.snapshot()
thread = getattr(contract.library_processing_job, 'thread', None)
- if live and live.get('status') == 'cancelled' and thread is not None and thread.is_alive():
+ if live and live.get('status') in ('cancelled', 'failed', 'complete') and thread is not None and thread.is_alive():
return {**live, 'status':'stopping'}
- return live if live and live.get('status') in ACTIVE else snapshot(settings_file.active())
+ if live and live.get('status') in ACTIVE:
+ return live
+ stored = snapshot(settings_file.active())
+ if live and live.get('job_id') == stored.get('job_id'):
+ return {**stored, **live}
+ if live and live.get('started_at', 0) > stored.get('started_at', 0):
+ # 抢处理锁之前失败时还未写进度文件,不能再用上一轮终态遮住这次错误。
+ return live
+ return stored
def q_library_processing(contract, _args):
diff --git a/tests/test_processing_control.py b/tests/test_processing_control.py
index ca95cccf0..240601763 100644
--- a/tests/test_processing_control.py
+++ b/tests/test_processing_control.py
@@ -17,6 +17,48 @@ from support.ledger import fresh_ledger
class ProcessingControlTests(unittest.TestCase):
+ def test_failed_worker_keeps_retries_out_until_its_real_lock_is_released(self):
+ from filelock import FileLock, Timeout
+ from peach.jobs import BackgroundJob
+ from peach.library_processing import _save, state_path
+ from peach.web_library_processing import _current
+ job = BackgroundJob('library-processing')
+ contract = SimpleNamespace(db_path=self.db, library_processing_job=job)
+ path = state_path(self.config)
+ path.parent.mkdir(parents=True, exist_ok=True)
+ _save(path, {'job_id': 'old', 'status': 'failed', 'started_at': 1, 'error': '旧错误'})
+ failed, release = threading.Event(), threading.Event()
+ def work(job_id):
+ with FileLock(str(path) + '.lock', timeout=0):
+ job.update(job_id, status='failed', error='当前处理失败')
+ failed.set()
+ release.wait(10)
+ job.start(work)
+ self.addCleanup(release.set)
+ self.assertTrue(failed.wait(5))
+ identity = job.snapshot()['job_id']
+ try:
+ with patch('peach.web_library_processing.settings_file.active', return_value=self.config):
+ with self.assertRaises(Timeout), FileLock(str(path) + '.lock', timeout=0):
+ pass
+ state = _current(contract)
+ self.assertEqual((state['job_id'], state['status']), (identity, 'stopping'))
+ retried = w_library_processing(contract, {'retry': True})
+ self.assertEqual((retried['job_id'], retried['status']), (identity, 'stopping'))
+ release.set()
+ job.thread.join(5)
+ self.assertFalse(job.thread.is_alive())
+ with FileLock(str(path) + '.lock', timeout=0):
+ pass
+ current = _current(contract)
+ self.assertEqual((current['job_id'], current['error']), (identity, '当前处理失败'))
+ _save(path, {'job_id': 'external', 'status': 'complete',
+ 'started_at': job.snapshot()['started_at'] + 1})
+ self.assertEqual(_current(contract)['job_id'], 'external')
+ finally:
+ release.set()
+ job.thread.join(5)
+
def test_skip_intent_is_shared_scoped_and_cleared_before_next_asset(self):
from peach.processing_control import ItemSkipped, processing_item
control = ProcessingControl(self.root / 'skip-control.json')