- Fix delete agent 500: clean up FK records (agent_llm_logs, permissions, schedules, executions, team_members) and unbind goals/tasks before delete - Remove hardcoded personality templates in Android, replace with dynamic system prompt generation from name + description - Set promptSectionsEnabled=false to bypass PromptComposer for personality - Add Tencent Cloud Linux deployment guide (Docker Compose) - Accumulated backend service updates, frontend UI fixes, Android app changes Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
57 lines
1.8 KiB
Python
57 lines
1.8 KiB
Python
"""
|
|
日志清理定时任务
|
|
"""
|
|
import logging
|
|
from datetime import datetime, timedelta
|
|
|
|
from app.core.celery_app import celery_app
|
|
from app.core.database import SessionLocal
|
|
from app.core.config import settings
|
|
from app.models.execution_log import ExecutionLog
|
|
from app.models.agent_execution_log import AgentExecutionLog
|
|
from app.models.agent_llm_log import AgentLLMLog
|
|
from app.models.audit_log import AuditLog
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# 需要清理的模型及其时间列
|
|
_CLEANUP_TARGETS: list[tuple[type, str]] = [
|
|
(ExecutionLog, "timestamp"),
|
|
(AgentExecutionLog, "created_at"),
|
|
(AgentLLMLog, "created_at"),
|
|
(AuditLog, "created_at"),
|
|
]
|
|
|
|
|
|
@celery_app.task(name="cleanup_old_logs")
|
|
def cleanup_old_logs() -> dict:
|
|
"""清理超过保留期的数据库日志记录"""
|
|
retention_days = settings.LOG_RETENTION_DAYS
|
|
cutoff = datetime.utcnow() - timedelta(days=retention_days)
|
|
|
|
db = SessionLocal()
|
|
result = {"status": "ok", "retention_days": retention_days, "deleted": {}}
|
|
|
|
try:
|
|
for model, time_col_name in _CLEANUP_TARGETS:
|
|
time_col = getattr(model, time_col_name)
|
|
count = db.query(model).filter(time_col < cutoff).delete()
|
|
result["deleted"][model.__tablename__] = count
|
|
if count > 0:
|
|
logger.info(
|
|
"[日志清理] %s: 删除 %d 条(%s 之前的记录)",
|
|
model.__tablename__, count, cutoff.isoformat()
|
|
)
|
|
|
|
db.commit()
|
|
logger.info("[日志清理] 完成,总计删除 %d 条记录", sum(result["deleted"].values()))
|
|
except Exception as e:
|
|
db.rollback()
|
|
logger.error("[日志清理] 失败: %s", e)
|
|
result["status"] = "failed"
|
|
result["error"] = str(e)
|
|
finally:
|
|
db.close()
|
|
|
|
return result
|