Skip to content

armgit / peach

Public Imported from GitHub
0

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

perf(tasks): 任务中心只对仍有活动的任务问控制状态与资源占用

longmeidao393044f53f8b
diff --git a/src/peach/task_runs.py b/src/peach/task_runs.py
index ae91d02a5..1731ab77d 100644
--- a/src/peach/task_runs.py
+++ b/src/peach/task_runs.py
@@ -725,6 +725,18 @@ class TaskRunStore:
rows = connection.execute(sql, params).fetchall()
return [_row(row) for row in rows]
+ def live_ids(self) -> set[int]:
+ """自己或某个后代还在排或在跑的任务:在排在跑的那些,连同它们沿父链往上的每一层。
+
+ 任务中心一页两百来条,绝大多数早已结束、后代也都结束了;先一条查询圈出这一份,
+ 不在里面的就不必逐条再问控制状态与资源占用。
+ """
+ with self.database.read_connection() as connection:
+ return {int(row[0]) for row in connection.execute(
+ "WITH RECURSIVE live(id) AS (SELECT id FROM task_run WHERE status IN ('pending','running') "
+ "UNION SELECT t.parent_run_id FROM task_run t JOIN live l ON t.id=l.id "
+ "WHERE t.parent_run_id IS NOT NULL) SELECT id FROM live")}
+
def blocking(self, mutex_key: str) -> TaskRun | None:
"""当前占着这把锁的那一轮。冲突时用它回答「是谁挡的」。"""
marks = ",".join(f"'{name}'" for name in ACTIVE_STATUSES)
diff --git a/src/peach/web_tasks.py b/src/peach/web_tasks.py
index 66dbe8cb4..0b12ec70b 100644
--- a/src/peach/web_tasks.py
+++ b/src/peach/web_tasks.py
@@ -142,9 +142,13 @@ def _payload_reader(contract):
with contract.cache_lock:
if not hasattr(contract, 'task_resource_monitor'):
contract.task_resource_monitor = Monitor(contract.task_runs)
+ # 控制状态与资源占用只对自己或后代还在排在跑的那几条有内容;其余的不逐条再问。
+ live = contract.task_runs.live_ids()
def payload(row):
result = row.payload()
+ if row.id not in live:
+ return _with_checkpoint(row, result)
from .task_control import projection
control = projection(contract.task_runs, row)
if control is not None:
@@ -152,6 +156,9 @@ def _payload_reader(contract):
resources = contract.task_resource_monitor.for_run(row)
if resources is not None:
result['resources'] = resources
+ return _with_checkpoint(row, result)
+
+ def _with_checkpoint(row, result):
if row.task_key not in RESULT_FIELDS or row.status not in ('succeeded', 'failed'):
return result
if all(field in row.result_summary for field in RESULT_FIELDS[row.task_key]):
diff --git a/tests/test_task_center_integration.py b/tests/test_task_center_integration.py
index 5239f4b67..8d7dabbb9 100644
--- a/tests/test_task_center_integration.py
+++ b/tests/test_task_center_integration.py
@@ -363,6 +363,27 @@ class TasksEndpointTests(unittest.TestCase):
self.store.finish(run.id, 'succeeded')
self.assertFalse(web_tasks.q_result_updates(self.contract, {})['busy'])
+ def test_finished_runs_are_listed_without_a_query_per_row(self):
+ """最近完成那一页两百来条大多早已结束:控制状态与资源占用只问还有活动的那几条,连接数不随页长增长。"""
+ from unittest import mock
+ for _ in range(30):
+ self.store.finish(self.store.start('library-processing', trigger='manual').id, 'succeeded')
+ parent = self.store.start('parent', trigger='manual')
+ self.store.enqueue_followups(parent.id, [('leaf:1', 'leaf', '还在排的后继')])
+ self.store.finish(parent.id, 'succeeded')
+ original = self.store.database.read_connection
+ opened = []
+
+ def counted():
+ opened.append(1)
+ return original()
+
+ with mock.patch.object(self.store.database, 'read_connection', side_effect=counted):
+ payload = web_tasks.q_tasks(self.contract, {'limit': '100'})
+ controlled = [row['id'] for row in (*payload['finished'], *payload['working_parents']) if 'control' in row]
+ self.assertEqual(controlled, [parent.id])
+ self.assertLess(len(opened), 20, f'连接数:{len(opened)}')
+
def test_ingest_notices_ride_along_with_the_task_list(self):
from unittest import mock