Skip to content

armgit / peach

Public Imported from GitHub
0

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

refactor(tasks): 任务接口的进行中取数与后继计数拆成独立函数

longmeidaoff8e35080b9c
diff --git a/src/peach/web_tasks.py b/src/peach/web_tasks.py
index 379ad3e94..d1d75cddd 100644
--- a/src/peach/web_tasks.py
+++ b/src/peach/web_tasks.py
@@ -54,16 +54,11 @@ def _tasks(contract, args):
"runs": [payload(row) for row in rows]}
finished, has_more = contract.task_runs.finished_page(before=before, limit=limit)
if before is not None:
- counts = contract.task_runs.followup_counts(row.id for row in finished if not row.followup)
return {"ok": True, "filtered": False, "available": True,
"finished": [payload(row) for row in finished],
"finished_has_more": has_more,
- "followup_counts": {str(parent): states for parent, states in counts.items()}}
- # 正在跑的一条不落,排队的取最近几条:一轮派出上千条后继时,按 id 取最近的只剩排队那些,
- # 真正在跑的那条反而被挤掉。
- started = contract.task_runs.query(status="running", limit=500)
- queued = contract.task_runs.query(status="pending", limit=limit)
- running = sorted({row.id: row for row in (*started, *queued)}.values(), key=lambda row: row.id, reverse=True)
+ "followup_counts": _parent_counts(contract, finished)}
+ running = _active(contract, limit)
# 还在跑的那一轮已经跑完的后继:它们挂在「正在进行」那张卡下面,不在哪一页里。
finished += contract.task_runs.settled_followups(
[row.id for row in running if not row.followup])
@@ -71,9 +66,6 @@ def _tasks(contract, args):
# 它派出的后继在页面上找不到父卡,会一条条散开。它不进 `finished`,翻页游标才不会跳过中间那段。
shown = {row.id for row in (*running, *finished)}
working = [row for row in contract.task_runs.active_parents() if row.id not in shown]
- # 后继只下发了一部分,父任务做到哪一步、还算不算在进行,按计数回答。
- counts = contract.task_runs.followup_counts(
- row.id for row in (*running, *finished, *working) if not row.followup)
return {
"ok": True,
"filtered": False,
@@ -87,11 +79,25 @@ def _tasks(contract, args):
"finished": [payload(row) for row in finished],
"finished_has_more": has_more,
"working_parents": [payload(row) for row in working],
- "followup_counts": {str(parent): states for parent, states in counts.items()},
+ "followup_counts": _parent_counts(contract, (*running, *finished, *working)),
"statuses": {"active": list(ACTIVE_STATUSES), "finished": list(TERMINAL_STATUSES)},
}
+def _active(contract, limit: int) -> list:
+ """正在跑的一条不落,排队的取最近几条:一轮派出上千条后继时,按 id 取最近的只剩排队那些,
+ 真正在跑的那条反而被挤掉。"""
+ started = contract.task_runs.query(status="running", limit=500)
+ queued = contract.task_runs.query(status="pending", limit=limit)
+ return sorted({row.id: row for row in (*started, *queued)}.values(), key=lambda row: row.id, reverse=True)
+
+
+def _parent_counts(contract, rows) -> dict[str, dict[str, int]]:
+ """后继只下发了一部分,父任务做到哪一步、还算不算在进行,按这份计数回答。"""
+ counts = contract.task_runs.followup_counts(row.id for row in rows if not row.followup)
+ return {str(parent): states for parent, states in counts.items()}
+
+
def q_task(contract, run_id):
payload = _payload_reader(contract)
run = contract.task_runs.get(int(run_id))