Peach: Local-first video metadata scraper and media manager.
fix(runtime): 安装关停期间保留任务续跑状态
longmeidao3f34fe563c39
diff --git a/docs/STATUS.md b/docs/STATUS.mdindex b8eceb7b9..7b2cbb22c 100644--- a/docs/STATUS.md+++ b/docs/STATUS.md@@ -22,8 +22,7 @@- 封面2001,v11。- 收藏、续播、播放墙续页;[路由](ROUTES.md)、[JAV](JAV_DISCOVERY.md)。- `/healthz` 报 `configurable=true`;配置、选目录共用连接判据;托盘管理 HTTPS 地址、端口及重载。-- 首启记住目录,支持接回已有配置;更新沿用数据根;见 [运行与配置](OPERATIONS.md)。-- 安装与更新会暂停刮削、抽帧等后台任务,完成或取消后由运行中的 Peach 续跑,已退出则下次启动时恢复;期间新发起的采集等待安装结束,已完成的馆藏处理步骤保留进度。+- 首启恢复配置;更新保留数据,安装暂停采集,运行后续跑;见 [运行与配置](OPERATIONS.md)。- 密码未开,局域网匿名可读;首启可跳过密码,配置页可改可关,登录可记住设备。- macOS 是 reader,代码与 `peach-data` 在内置盘;`peach.local` 经 8900/8443 和 pf 提供 80/443,GET 正常、写入回 409。- 两端用本机 CA,私钥、凭据不跨机;代码 Git、账本单写者复制、图片 Syncthing;坐标 `<数据根>/config.toml`。diff --git a/scripts/smoke_update_tasks.py b/scripts/smoke_update_tasks.pyindex 148667842..a842cc950 100644--- a/scripts/smoke_update_tasks.py+++ b/scripts/smoke_update_tasks.py@@ -47,7 +47,6 @@ def main():with smoke_directory(args.evidence_dir.resolve()) as root:application = root / '中文 程序目录/Peach'old = (args.old_executable or executable).resolve(strict=True)- shutil.copytree(old.parent, application)target = application / 'Peach.exe'data, media = root / 'data', root / 'media'media.mkdir()@@ -62,6 +61,14 @@ def main():process = Nonewith log_path.open('wb') as log:try:+ installer_command = None+ if args.installer:+ installer_command = [str(args.installer.resolve()), '/VERYSILENT', '/SUPPRESSMSGBOXES',+ '/NORESTART', f'/DIR={application}']+ subprocess.run([*installer_command, f'/LOG={root / "initial-install.log"}'],+ env=environment, timeout=240, check=True)+ else:+ shutil.copytree(old.parent, application)process = subprocess.Popen([*command, '--setup'], cwd=root, env=environment, stdout=log, stderr=log)wait_health(opener, base, process, log_path)complete_first_run(opener, base, media, port, 'update-task-smoke-password')@@ -81,11 +88,9 @@ def main():state = data / 'state/library-processing.json'state.write_text(json.dumps({'job_id': 'interrupted-fixture', 'status': 'running','requested_stage': 'scan', 'last_progress_at': 1}), encoding='utf-8')- installer_command = None- if args.installer:- installer_command = [str(args.installer.resolve()), '/VERYSILENT', '/SUPPRESSMSGBOXES',- '/NORESTART', f'/DIR={application}', f'/LOG={root / "install.log"}']- subprocess.run(installer_command, env=environment, timeout=240, check=True)+ if installer_command:+ subprocess.run([*installer_command, f'/LOG={root / "install.log"}'],+ env=environment, timeout=240, check=True)else:error = root / 'preflight-error.txt'helper = subprocess.run([str(executable), '--installer-prepare', str(application),diff --git a/src/peach/jobs.py b/src/peach/jobs.pyindex 028bb2ebc..197b0a7b3 100644--- a/src/peach/jobs.py+++ b/src/peach/jobs.py@@ -297,6 +297,7 @@ class BackgroundJob:self._run_ids: dict[str, int] = {}self._activity_files: dict[str, Path] = {}self._recoverable: set[str] = set()+ self._paused_stops: set[str] = set()#: 摘 run id 与把那一行写成结束是同一件事:只摘不写完就放手的话,另一个线程#: 看见 id 没了以为已经收过尾,紧接着开的下一轮会撞上那一行还没释放的互斥键。self._close_lock = threading.Lock()@@ -511,23 +512,37 @@ class BackgroundJob:往一个没人读的状态里写进度。等不到就不等——一次外部 HTTP 探测可以是十几秒,关停不该被它拖住。- 在途那一轮在表里记成 `cancelled`:进程是有序退出的,不是被打断,下次启动的- 租约恢复也就不该再把它算成一次故障。+ 安装暂停期间保留可恢复任务与原运行记录,线程退出后交回恢复入口。+ 普通关停的在途任务记成 `cancelled`,租约恢复不把有序退出计为故障。"""- if not resume and (db := self._database_path()) is not None:- from .job_recovery import cancel- cancel(db, self.task_key)- with self.lock:- state = self.state- job_id = state[self.id_key] if state is not None else ""- self.state = None- thread = self.thread- self.thread = None- if job_id and state is not None and state["status"] == "running":- self._close_run(job_id, "cancelled", error="服务关停,这一轮没有跑完")+ from .update_tasks import _mutex, state_for+ db = self._database_path()+ with _mutex(state_for(db)) if db is not None else nullcontext():+ if not resume and db is not None:+ from .job_recovery import cancel+ cancel(db, self.task_key)+ with self.lock:+ state = self.state+ job_id = state[self.id_key] if state is not None else ""+ self.state = None+ thread = self.thread+ self.thread = None+ if job_id and state is not None and state["status"] == "running":+ self._stop_run(job_id, resume=resume)if thread is not None and thread.is_alive():thread.join(timeout)+ def _stop_run(self, job_id, *, resume):+ if resume and job_id in self._recoverable:+ from .job_recovery import read+ if read(self._database_path(), self.task_key).get('pause_requested'):+ self._paused_stops.add(job_id)+ run_id = self._run_ids.pop(job_id, None)+ if self.runs is not None and run_id is not None:+ self.runs.suspend(run_id)+ return+ self._close_run(job_id, 'cancelled', error='服务关停,这一轮没有跑完')+def start_result(self, fn: Callable[[], dict], *, recovery: dict | None = None) -> dict:"""执行一次操作并保存终态回执;重复提交只返回进行中的同一任务。"""def work(job_id):@@ -572,6 +587,9 @@ class BackgroundJob:with _mutex(state_for(db)) if db is not None else nullcontext():try:self._settle(job_id)+ if job_id in self._paused_stops:+ from .job_recovery import waiting+ waiting(db, self.task_key, job_id)state = self.snapshot() or {}if (db is not None and job_id in self._recoverableand state.get(self.id_key) == job_id and not state.get('waiting_for_update')):@@ -583,6 +601,7 @@ class BackgroundJob:if activity is not None:activity.unlink(missing_ok=True)self._recoverable.discard(job_id)+ self._paused_stops.discard(job_id)def _settle(self, job_id: str) -> None:"""按域自己落下的终态结算 `task_run`。diff --git a/tests/test_update_tasks.py b/tests/test_update_tasks.pyindex cfbbf3c55..c5a7804c4 100644--- a/tests/test_update_tasks.py+++ b/tests/test_update_tasks.py@@ -92,6 +92,34 @@ class UpdateTaskTests(unittest.TestCase):self.assertEqual(self.store.recover_interrupted(), [])update_tasks.release(self.db, token)+ def test_service_shutdown_during_installation_preserves_the_run_until_its_worker_exits(self):+ entered, release = threading.Event(), threading.Event()+ job = BackgroundJob('test', runs=self.store, task_key='library-processing')+ def work(job_id):+ entered.set()+ release.wait(10)+ job.update(job_id, status='complete')+ state = job.start(work, recovery={'stage': 'previews'})+ thread = job.thread+ token = update_tasks.reserve(self.db)+ try:+ self.assertTrue(entered.wait(5))+ job.stop(timeout=0)+ self.assertEqual(self.store.get(state['run_id']).status, 'pending')+ update_tasks.release(self.db, token)+ handler = {'library-processing': lambda body: job.start(work, restart=True, recovery=body)}+ self.assertEqual(job_recovery.resume(self.db, handler), [])+ finally:+ release.set()+ thread.join(5)+ update_tasks.release(self.db, token)+ self.assertFalse(thread.is_alive())+ self.assertEqual(job_recovery.resume(self.db, handler), ['library-processing'])+ job.thread.join(5)+ self.assertEqual(job.snapshot()['job_id'], state['job_id'])+ self.assertEqual(job.snapshot()['run_id'], state['run_id'])+ self.assertEqual(self.store.get(state['run_id']).status, 'succeeded')+def test_another_service_cannot_replace_the_scope_already_waiting_for_installation(self):token = update_tasks.reserve(self.db)first = BackgroundJob('one', runs=self.store, task_key='library-processing')