From f427aea1b355b49eb5c376a674089ad1ec04e48f Mon Sep 17 00:00:00 2001 From: Yuzhiran Dev Date: Mon, 18 May 2026 22:37:59 +0800 Subject: [PATCH] Fix task status stuck at 'running': use new DB session in thread, add frontend auto-polling --- platform/backend/app/api/tasks.py | 41 +++++++++++++++++++------------ platform/frontend/tasks.html | 13 +++++++++- 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/platform/backend/app/api/tasks.py b/platform/backend/app/api/tasks.py index 937b40d..f148e09 100644 --- a/platform/backend/app/api/tasks.py +++ b/platform/backend/app/api/tasks.py @@ -227,25 +227,34 @@ def run_creator_task( from ..core.generator import run_creator def _run(): + from ..database import SessionLocal + from datetime import datetime, timezone + new_db = SessionLocal() try: result = run_creator(topic_id) - finished = datetime.now(timezone.utc) - task.status = "completed" - task.finished_at = finished - task.progress = 100 - task.message = "创作完成" - task.result_data = result or {} - if task.started_at: - task.duration = int((finished - task.started_at).total_seconds()) - db.commit() + new_task = new_db.query(ContentTask).filter(ContentTask.task_id == task_id).first() + if new_task: + finished = datetime.now(timezone.utc) + new_task.status = "completed" + new_task.finished_at = finished + new_task.progress = 100 + new_task.message = "创作完成" + new_task.result_data = result or {} + if new_task.started_at: + new_task.duration = int((finished - new_task.started_at).total_seconds()) + new_db.commit() except Exception as e: - finished = datetime.now(timezone.utc) - task.status = "failed" - task.finished_at = finished - task.error_msg = str(e) - if task.started_at: - task.duration = int((finished - task.started_at).total_seconds()) - db.commit() + new_task = new_db.query(ContentTask).filter(ContentTask.task_id == task_id).first() + if new_task: + finished = datetime.now(timezone.utc) + new_task.status = "failed" + new_task.finished_at = finished + new_task.error_msg = str(e) + if new_task.started_at: + new_task.duration = int((finished - new_task.started_at).total_seconds()) + new_db.commit() + finally: + new_db.close() thread = threading.Thread(target=_run) thread.start() diff --git a/platform/frontend/tasks.html b/platform/frontend/tasks.html index 207134a..3a445e6 100644 --- a/platform/frontend/tasks.html +++ b/platform/frontend/tasks.html @@ -187,6 +187,7 @@ const TasksApp = { showDetailDialog: false, detailTask: null, schedulerJobs: [], schedulerLoading: false, totalTasks: 0, currentPage: 1, pageSize: 20, + pollTimer: null, SCHEDULER_JOBS, } }, @@ -236,6 +237,13 @@ const TasksApp = { if (!dateStr) return '-'; try { return new Date(dateStr).toLocaleString('zh-CN'); } catch (e) { return dateStr; } }, + startPolling() { + this.stopPolling(); + this.pollTimer = setInterval(() => this.loadTasks(), 5000); + }, + stopPolling() { + if (this.pollTimer) { clearInterval(this.pollTimer); this.pollTimer = null; } + }, async loadTasks() { this.loading = true; try { @@ -244,6 +252,8 @@ const TasksApp = { const data = await this.api(url) || []; this.tasks = data; this.totalTasks = data.length < this.pageSize ? (this.currentPage - 1) * this.pageSize + data.length : this.currentPage * this.pageSize + 1; + const hasRunning = data.some(t => t.status === 'running'); + if (hasRunning) { this.startPolling(); } else { this.stopPolling(); } } catch (e) { console.error(e); this.$message.error('加载任务列表失败: ' + e.message); } finally { this.loading = false; } }, @@ -265,7 +275,8 @@ const TasksApp = { } catch (e) { this.$message.error('重试失败: ' + e.message); } } }, - mounted() { this.checkAuth(); } + mounted() { this.checkAuth(); }, + beforeUnmount() { this.stopPolling(); } }; const app = Vue.createApp(TasksApp); app.use(ElementPlus);