Peach: Local-first video metadata scraper and media manager.
fix(tasks): 补齐冻结运行依赖与暂停后的进程身份核对
zouyonghe10034bc9cb2f
diff --git a/docs/REUSE.md b/docs/REUSE.mdindex da4ca2250..38deea2e5 100644--- a/docs/REUSE.md+++ b/docs/REUSE.md@@ -1105,6 +1105,10 @@ Python 源文件纳入 `src/peach/library_sharing.py` 与 `src/peach/library_pro普通 Toast 与扫描任务卡共用 Agent Progress 外框,16px 半径由通知栈内的既有 token 解析,细边框与接触阴影分别复用 `border-button-default`、`elevation-xs`,不改变内容排版。浏览器回归核对桌面、手机和明暗主题下的外框一致性,并以同一任务、同一内容回放外框基线生成对比图。+## 任务恢复的进程身份++任务恢复复用 psutil 7.2.2(BSD-3-Clause,Windows/macOS/Linux)查询进程创建时间,并将既有开发依赖提升为固定运行依赖;不自行解析系统进程表。PID 存活且创建时间不晚于任务最近心跳时保留所有者,已复用 PID 允许恢复原任务;暂停后的 PID 为零时仍按恢复记录核对。创建时间因权限未取得时保守保留。临时账本回归覆盖失效 PID、复用 PID、当前心跳、安装暂停及任务身份保留,不直接清理真实任务记录。+## 本地附属媒体识别附属媒体识别复用 `catalog_rules.local_extra_type`、`is_local_extra` 与现有 `scan.is_sidecar`,兼容 Plex、Kodi、Emby、Jellyfin 的显式文件标记和专用目录。规则来源、完整标记及边界见[附属媒体命名证据](reference-snapshots/local-trailer-evidence.md)。diff --git a/pyproject.toml b/pyproject.tomlindex 3ca77977b..7dc92b0e2 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -44,6 +44,7 @@ dependencies = ["m3u8==6.0.0","pillow==13.0.0","protobuf==7.36.2",+ "psutil==7.2.2","pystray==0.19.5","pydantic==2.14.2","resvg-py==0.5.0",@@ -68,11 +69,10 @@ peach = "peach.error_reporting:cli_main"peach-tray = "peach.error_reporting:tray_main"[project.optional-dependencies]-# psutil 给 `scripts/agent_worktree.py` 查工作树里的残留进程:当前目录、父进程、CPU 与内存。+# 开发工具复用运行依赖 psutil 查询工作树进程。dev = ["filelock==4.2.4","httpx2==2.16.0",- "psutil==7.2.2","ruff==0.17.3","seiso==0.4.0",]diff --git a/src/peach/job_recovery.py b/src/peach/job_recovery.pyindex 3ed1a99e9..867c9c91b 100644--- a/src/peach/job_recovery.py+++ b/src/peach/job_recovery.py@@ -30,8 +30,8 @@ def _worker_alive(db: Path, data: dict) -> bool:if isinstance(run_id, int):from .task_runs import TaskRunStorerun = TaskRunStore(db).get(run_id)- if run is not None and run.pid == pid:- return process_matches(pid, run.started_at)+ if run is not None and (run.pid == pid or (run.pid == 0 and run.status == 'pending')):+ return process_matches(pid, run.heartbeat_at or run.started_at)return process_alive(pid)diff --git a/src/peach/task_runs.py b/src/peach/task_runs.pyindex ef8c027ee..322ed3f6b 100644--- a/src/peach/task_runs.py+++ b/src/peach/task_runs.py@@ -641,7 +641,7 @@ class TaskRunStore:continuelocal = run.host == self.hostalive = (local and run.pid is not None and run.pid != os.getpid()- and process_matches(run.pid, run.started_at, alive=process_alive))+ and process_matches(run.pid, run.heartbeat_at or run.started_at, alive=process_alive))beat = _parse(run.heartbeat_at) or _parse(run.started_at)expired = beat is None or beat < deadlineif alive or (not local and not expired):diff --git a/tests/test_task_runs.py b/tests/test_task_runs.pyindex c658643f5..dd5ff0207 100644--- a/tests/test_task_runs.py+++ b/tests/test_task_runs.py@@ -9,6 +9,7 @@ from pathlib import Pathfrom unittest import mockfrom peach.migrations import upgrade+from peach.jobs import process_matchesfrom peach.repository import LedgerDatabasefrom peach.task_runs import (TaskRunConflict,@@ -22,6 +23,24 @@ MIGRATIONS = Path(__file__).resolve().parents[1] / "migrations"class TaskRunStoreTests(unittest.TestCase):+ def test_process_identity_preserves_owners_when_creation_time_is_unknown(self):+ started = stamp(datetime.now(timezone.utc))+ with mock.patch('peach.jobs.process_creation_time', return_value=None):+ self.assertTrue(process_matches(os.getpid(), started, alive=lambda _: True))+ self.assertFalse(process_matches(os.getpid(), started, alive=lambda _: False))++ def test_a_resumed_worker_with_current_heartbeat_is_not_reclaimed(self):+ run = self.store.start('library-processing', trigger='manual')+ earlier = datetime.now(timezone.utc) - timedelta(days=1)+ created = datetime.now(timezone.utc).timestamp() - 30+ with self.database.write_transaction(notify=False) as connection:+ connection.execute('UPDATE task_run SET pid=?,started_at=? WHERE id=?',+ (os.getpid() + 1, stamp(earlier), run.id))+ with mock.patch('peach.task_runs.process_alive', return_value=True), \+ mock.patch('peach.jobs.process_creation_time', return_value=created):+ self.assertEqual(self.store.recover_interrupted(), [])+ self.assertEqual(self.store.get(run.id).status, 'running')+def setUp(self):self.tmp = tempfile.TemporaryDirectory()self.addCleanup(self.tmp.cleanup)diff --git a/tests/test_update_tasks.py b/tests/test_update_tasks.pyindex e471512b6..bc47645fc 100644--- a/tests/test_update_tasks.py+++ b/tests/test_update_tasks.py@@ -78,12 +78,13 @@ class UpdateTaskTests(unittest.TestCase):data = json.loads(path.read_text(encoding='utf-8'))data['pid'] = os.getpid()path.write_text(json.dumps(data), encoding='utf-8')+ self.store.suspend(run.id)observed = []handler = {'library-processing': lambda request: (observed.append(request), {'resumed': True})[1]}with patch('peach.jobs.process_creation_time', return_value=datetime.now(timezone.utc).timestamp()):self.assertEqual(job_recovery.resume(self.db, handler), ['library-processing'])self.assertEqual(observed, [{'stage': 'collect'}])- self.assertEqual(self.store.get(run.id).status, 'running')+ self.assertEqual(self.store.get(run.id).status, 'pending')def test_terminal_checkpoint_is_readable_by_a_new_job_instance(self):first = BackgroundJob('resume-test', runs=self.store, task_key='link-check')diff --git a/uv.lock b/uv.lockindex 08a1219e0..df2bff827 100644--- a/uv.lock+++ b/uv.lock@@ -1249,6 +1249,7 @@ dependencies = [{ name = "m3u8" },{ name = "pillow" },{ name = "protobuf" },+ { name = "psutil" },{ name = "pyahocorasick" },{ name = "pydantic" },{ name = "pystray" },@@ -1273,7 +1274,6 @@ build = [dev = [{ name = "filelock" },{ name = "httpx2" },- { name = "psutil" },{ name = "ruff" },{ name = "seiso" },]@@ -1314,7 +1314,7 @@ requires-dist = [{ name = "p115client", marker = "extra == 'maintenance-115'", specifier = "==0.0.9.7.2" },{ name = "pillow", specifier = "==13.0.0" },{ name = "protobuf", specifier = "==7.36.2" },- { name = "psutil", marker = "extra == 'dev'", specifier = "==7.2.2" },+ { name = "psutil", specifier = "==7.2.2" },{ name = "pyahocorasick", specifier = "==2.3.1" },{ name = "pydantic", specifier = "==2.14.2" },{ name = "pyinstaller", marker = "extra == 'build'", specifier = "==6.22.3" },