Skip to content

armgit / peach

Public Imported from GitHub
0

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

fix(tasks): 父任务控制状态一次查询取回后代,任务中心不再逐条开连接

longmeidao3877779b4699
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f142f09f1..fc1a8fb58 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -21,6 +21,10 @@ Removed / Fixed / Security。
## [未发布]
+### 修复
+
+- **界面**:一轮自动修复或补全排着上千条后续任务时,任务中心不再要等十几秒才显示;任务越多越慢的问题一并消除。
+
## [0.55.0] - 2026-11-05
### 新增
diff --git a/src/peach/task_control.py b/src/peach/task_control.py
index a4b4997e9..0bb95d45c 100644
--- a/src/peach/task_control.py
+++ b/src/peach/task_control.py
@@ -64,17 +64,9 @@ def requested(db, run_id, ancestors=()):
def projection(store, run):
if not store.enabled or run.host != store.host:
return None
- children = []
- if not run.active:
- with store.database.read_connection() as connection:
- rows = connection.execute(
- 'WITH RECURSIVE family(id) AS (SELECT id FROM task_run WHERE parent_run_id=? '
- 'UNION ALL SELECT t.id FROM task_run t JOIN family f ON t.parent_run_id=f.id) '
- "SELECT id FROM task_run WHERE id IN (SELECT id FROM family) AND status IN ('pending','running')",
- (run.id,)).fetchall()
- children = [store.get(row[0]) for row in rows]
- if not children:
- return None
+ # 自己已结束的父任务,有后代还在排或在跑才算可控;这里只问有没有。
+ if not run.active and not store.active_descendants(run.id, limit=1):
+ return None
command = intent(store.database.db_path, run.id, (run.parent_run_id, run.root_run_id))
action = command.get('action', 'resume')
observed = _read(_root(store.database.db_path) / f'{run.id}.observed.json')
@@ -83,6 +75,8 @@ def projection(store, run):
elif action == 'pause':
acknowledged = observed.get('command') == command.get('id') and observed.get('state') == 'paused'
if not run.active:
+ # 暂停是否已落到每个后代:只有这里要整行,一次查询取回。
+ children = store.active_descendants(run.id)
acknowledged = bool(children) and all(
(projection(store, child) or {}).get('state') == 'paused' for child in children)
state = 'paused' if run.status == 'pending' or acknowledged else 'pausing'
diff --git a/src/peach/task_runs.py b/src/peach/task_runs.py
index 366e7e19f..ae91d02a5 100644
--- a/src/peach/task_runs.py
+++ b/src/peach/task_runs.py
@@ -707,6 +707,24 @@ class TaskRunStore:
f"SELECT {_COLUMNS} FROM task_run WHERE id=?", (run_id,)).fetchone()
return _row(row) if row else None
+ def active_descendants(self, run_id: int, *, limit: int | None = None) -> list[TaskRun]:
+ """还在排或在跑的全部后代(不含自己),一条查询取回整行。
+
+ 一轮补全能派出上千条后继;逐条 `get` 每条都开一次连接,任务中心每次轮询都要等好几秒。
+ 只想知道有没有时给 `limit=1`。
+ """
+ sql = ("WITH RECURSIVE family(id) AS (SELECT id FROM task_run WHERE parent_run_id=? "
+ "UNION ALL SELECT t.id FROM task_run t JOIN family f ON t.parent_run_id=f.id) "
+ f"SELECT {_COLUMNS} FROM task_run WHERE id IN (SELECT id FROM family) "
+ "AND status IN ('pending','running') ORDER BY id")
+ params: tuple = (run_id,)
+ if limit is not None:
+ sql += " LIMIT ?"
+ params += (limit,)
+ with self.database.read_connection() as connection:
+ rows = connection.execute(sql, params).fetchall()
+ return [_row(row) for row in rows]
+
def blocking(self, mutex_key: str) -> TaskRun | None:
"""当前占着这把锁的那一轮。冲突时用它回答「是谁挡的」。"""
marks = ",".join(f"'{name}'" for name in ACTIVE_STATUSES)
diff --git a/tests/test_task_control.py b/tests/test_task_control.py
index 7f31f29fc..da1b501b0 100644
--- a/tests/test_task_control.py
+++ b/tests/test_task_control.py
@@ -177,6 +177,28 @@ class TaskControlTests(unittest.TestCase):
self.store.finish(late_id, 'succeeded')
self.assertIsNone(self.state(parent.id))
+ def test_a_parent_with_many_queued_followups_is_projected_without_a_query_per_followup(self):
+ """一轮补全派出上千条后继:任务中心每次轮询算父卡的控制状态,连接数不随后继条数增长。"""
+ parent = self.store.start('parent', trigger='manual')
+ self.store.enqueue_followups(parent.id, [(f'leaf:{n}', 'leaf', f'后继 {n}') for n in range(50)])
+ self.store.finish(parent.id, 'succeeded')
+ original = self.store.database.read_connection
+ opened = []
+
+ def counted():
+ opened.append(1)
+ return original()
+
+ run = self.store.get(parent.id)
+ with patch.object(self.store.database, 'read_connection', side_effect=counted):
+ self.assertEqual(task_control.projection(self.store, run)['state'], 'running')
+ running = len(opened)
+ task_control.request(self.store, parent.id, 'pause')
+ opened.clear()
+ self.assertEqual(task_control.projection(self.store, run)['state'], 'paused')
+ self.assertLessEqual(running, 2)
+ self.assertLessEqual(len(opened), 3)
+
def test_cancelled_parent_does_not_run_followups_enqueued_after_the_request(self):
parent = self.store.start('parent', trigger='manual')
first = self.store.enqueue_followups(parent.id, [('leaf:1', 'leaf', '原任务')])['queued'][0]