fix: content quality, image format, task monitor, calendar data source, search UI & sort

This commit is contained in:
Yuzhiran Dev
2026-05-26 11:26:10 +08:00
parent b2d043b231
commit ac8644d752
36 changed files with 2294 additions and 873 deletions
+44 -34
View File
@@ -1,4 +1,5 @@
import uuid
import threading
from fastapi import APIRouter, Depends, HTTPException, Query
from sqlalchemy.orm import Session
from typing import List, Optional
@@ -8,6 +9,8 @@ from ..models import ContentTask, Topic
from ..schemas import ContentTaskCreate, ContentTaskResponse
from .auth import get_current_user, org_filter
_creator_semaphore = threading.Semaphore(3)
router = APIRouter(prefix="/api/tasks", tags=["tasks"])
@@ -240,6 +243,7 @@ def _get_module_detail_data(module_id: str, db, ROOT, DATA_DIR, LOGS_DIR, today_
"scheduled_optimize": {"name": "🔍 合规审查", "description": "LLM 审查已创作文章,检查合规、打分数、优化建议"},
"scheduled_optimize_sources": {"name": "📡 信息源优化", "description": "AI 分析当前类别和信息源的市场匹配度,给出调整建议"},
"scheduled_metrics_sync": {"name": "📊 指标同步", "description": "从各平台公开 API 获取已发布文章的互动数据(点赞、阅读、评论等)"},
"scheduled_task_monitor": {"name": "⏰ 任务监控", "description": "每小时自动检查卡死/中断任务,标记为失败以便重新执行"},
}
meta = MODULE_META.get(module_id, {"name": module_id, "description": ""})
@@ -430,7 +434,6 @@ def run_creator_task(
db: Session = Depends(get_db),
current_user=Depends(get_current_user)
):
import threading
from datetime import datetime, timezone
now = datetime.now(timezone.utc)
@@ -448,44 +451,51 @@ def run_creator_task(
db.commit()
db.refresh(task)
from ..core.generator import run_creator
def _run():
from ..database import SessionLocal
from ..core.generator import run_creator_blocking
from datetime import datetime, timezone
new_db = SessionLocal()
import traceback
_creator_semaphore.acquire()
try:
new_task = new_db.query(ContentTask).filter(ContentTask.task_id == task_id).first()
if new_task:
new_task.message = "创作脚本运行中..."
new_task.progress = 30
new_db.commit()
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)
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)
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()
new_db = SessionLocal()
try:
new_task = new_db.query(ContentTask).filter(ContentTask.task_id == task_id).first()
if new_task:
new_task.message = "创作脚本运行中..."
new_task.progress = 30
new_db.commit()
thread = threading.Thread(target=_run)
result = run_creator_blocking(topic_id)
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 = {"stdout": (result or {}).get("stdout", "")[:2000]} if isinstance(result, dict) else {"raw": str(result)[:2000]}
if new_task.started_at:
new_task.duration = int((finished - new_task.started_at).total_seconds())
new_db.commit()
except Exception as e:
new_db.rollback()
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 = f"{type(e).__name__}: {e}\n{traceback.format_exc()}"
if new_task.started_at:
new_task.duration = int((finished - new_task.started_at).total_seconds())
new_db.commit()
finally:
new_db.close()
finally:
_creator_semaphore.release()
thread = threading.Thread(target=_run, daemon=True)
thread.start()
return task