From 671cec58fa09ce353ae8955b3908b29897bb5bf8 Mon Sep 17 00:00:00 2001 From: Yuzhiran Dev Date: Mon, 18 May 2026 23:38:30 +0800 Subject: [PATCH] Fix preview platform switching, add stale task cleanup on startup --- platform/backend/app/main.py | 25 +++++++++++++++++++++++++ platform/frontend/topics.html | 4 ++++ 2 files changed, 29 insertions(+) diff --git a/platform/backend/app/main.py b/platform/backend/app/main.py index deb55f8..eef8e38 100644 --- a/platform/backend/app/main.py +++ b/platform/backend/app/main.py @@ -48,6 +48,31 @@ Base.metadata.create_all(bind=engine) init_db() import_initial_data() +# 清理超时任务(运行超过 1 小时的任务视为故障) +from datetime import datetime, timezone, timedelta +from .database import SessionLocal +from .models import ContentTask +try: + cleanup_db = SessionLocal() + cutoff = datetime.now(timezone.utc) - timedelta(hours=1) + stale = cleanup_db.query(ContentTask).filter( + ContentTask.status == "running", + ContentTask.started_at.isnot(None), + ContentTask.started_at < cutoff + ).all() + for t in stale: + t.status = "failed" + t.error_msg = "任务超时(服务重启导致状态丢失)" + if t.started_at: + t.duration = int((datetime.now(timezone.utc) - t.started_at).total_seconds()) + logger.warning(f"标记超时任务: {t.task_id}") + if stale: + cleanup_db.commit() + logger.info(f"已清理 {len(stale)} 个超时任务") + cleanup_db.close() +except Exception as e: + logger.warning(f"任务清理失败: {e}") + # 注册 API 路由 app.include_router(topics.router) app.include_router(system.router) diff --git a/platform/frontend/topics.html b/platform/frontend/topics.html index b82e5c2..f888053 100644 --- a/platform/frontend/topics.html +++ b/platform/frontend/topics.html @@ -394,6 +394,10 @@ const TopicsApp = { await this.fetchTodayCount(); } catch (e) { if (e !== 'cancel') this.$message.error('删除失败'); } }, + switchPlatform(platform) { + if (this.editing) this.cancelEdit(); + this.previewPlatform = platform; + }, startEdit() { this.editContent = this.currentPreviewBody; this.editing = true;