diff --git a/app/static/app.js b/app/static/app.js index e515c97..97f6824 100644 --- a/app/static/app.js +++ b/app/static/app.js @@ -71,6 +71,26 @@ document.addEventListener("DOMContentLoaded", () => { updateScaleHint(); }); +function pollTaskDetailStatus(taskId) { + if (!taskId) return; + const intervalMs = 3000; + const poll = async () => { + try { + const resp = await fetch(`/api/tasks/${taskId}`, { cache: "no-store" }); + if (!resp.ok) return; + const task = await resp.json(); + if (task.status !== "running") { + window.location.reload(); + return; + } + } catch (_error) { + return; + } + window.setTimeout(poll, intervalMs); + }; + window.setTimeout(poll, intervalMs); +} + async function downloadExport(url, defaultFilename, event) { if (event) event.preventDefault(); const resp = await fetch(url); diff --git a/app/templates/tasks/detail.html b/app/templates/tasks/detail.html index 32f2e9a..65ea078 100644 --- a/app/templates/tasks/detail.html +++ b/app/templates/tasks/detail.html @@ -9,9 +9,9 @@ {% block content %}

任务 #{{ task.id }}

- +
-
+
{% set label, cls = status_badge_config(task.status) %}

平台:{{ task.platform | platform_label }} {{ label }}

成功 {{ task.successful_items_count }} / 共 {{ task.total_items_count }} 条内容

@@ -19,6 +19,7 @@
{% if task.status == "running" and not hotspots %}

正在抓取热点数据,请稍候...

+ {% endif %}
{% for hotspot in hotspots %} diff --git a/tests/integration/test_routes.py b/tests/integration/test_routes.py index 6883455..d92c652 100644 --- a/tests/integration/test_routes.py +++ b/tests/integration/test_routes.py @@ -42,6 +42,31 @@ def test_index_page_lists_existing_tasks(): assert "运行中" in response.text +def test_running_task_detail_page_auto_polls_current_task(): + with make_test_client() as (client, engine): + from sqlalchemy.orm import Session + + with Session(engine) as session: + session.add( + Task( + id="task-running", + platform="xiaohongshu", + status="running", + hotspot_limit=1, + item_limit_per_hotspot=1, + comment_limit_per_item=10, + ) + ) + session.commit() + + response = client.get("/tasks/task-running") + + assert response.status_code == 200 + assert 'data-task-id="task-running"' in response.text + assert "pollTaskDetailStatus" in response.text + assert 'onclick="window.location.reload()"' in response.text + + def seed_result_data(engine): from sqlalchemy.orm import Session @@ -119,7 +144,7 @@ def test_result_pages_render_seeded_data(): assert task_response.status_code == 200 assert "热点标题" in task_response.text - assert 'onclick="window.location.href=\'/\'"' in task_response.text + assert 'onclick="window.location.reload()"' in task_response.text assert hotspot_response.status_code == 200 assert "热点总结" in hotspot_response.text assert item_response.status_code == 200