Fix task status stuck at 'running': use new DB session in thread, add frontend auto-polling
This commit is contained in:
@@ -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)
|
||||
new_task = new_db.query(ContentTask).filter(ContentTask.task_id == task_id).first()
|
||||
if new_task:
|
||||
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.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:
|
||||
new_task = new_db.query(ContentTask).filter(ContentTask.task_id == task_id).first()
|
||||
if new_task:
|
||||
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.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()
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user