强化定时任务日志查询的错误处理

添加多层错误保护:

1. 在整个查询函数外层添加try-except
2. 在每行数据处理时也添加try-except
3. 出错时返回空数组而不是抛出异常
4. 打印详细的错误信息和堆栈跟踪

这样即使数据有问题,也不会导致500错误,
而是会正常返回空列表并在日志中显示具体错误。

位置: database.py 第1661-1695行

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
root
2025-12-10 16:47:00 +08:00
parent cc4b69e631
commit fb7a75e76e

View File

@@ -1660,27 +1660,39 @@ def update_schedule_execution_log(log_id, **kwargs):
def get_schedule_execution_logs(schedule_id, limit=10):
"""获取定时任务执行日志"""
with db_pool.get_db() as conn:
cursor = conn.cursor()
cursor.execute('''
SELECT * FROM schedule_execution_logs
WHERE schedule_id = ?
ORDER BY execute_time DESC
LIMIT ?
''', (schedule_id, limit))
try:
with db_pool.get_db() as conn:
cursor = conn.cursor()
cursor.execute('''
SELECT * FROM schedule_execution_logs
WHERE schedule_id = ?
ORDER BY execute_time DESC
LIMIT ?
''', (schedule_id, limit))
# 将数据库字段映射到前端期望的字段名
logs = []
for row in cursor.fetchall():
log = dict(row)
# 字段映射
log['created_at'] = log.get('execute_time')
log['success_count'] = log.get('success_accounts', 0)
log['failed_count'] = log.get('failed_accounts', 0)
log['duration'] = log.get('duration_seconds', 0)
logs.append(log)
# 将数据库字段映射到前端期望的字段名
logs = []
rows = cursor.fetchall()
return logs
for row in rows:
try:
log = dict(row)
# 字段映射
log['created_at'] = log.get('execute_time')
log['success_count'] = log.get('success_accounts', 0)
log['failed_count'] = log.get('failed_accounts', 0)
log['duration'] = log.get('duration_seconds', 0)
logs.append(log)
except Exception as e:
print(f"[数据库] 处理日志行时出错: {e}")
continue
return logs
except Exception as e:
print(f"[数据库] 查询定时任务日志时出错: {e}")
import traceback
traceback.print_exc()
return [] # 出错时返回空列表,而不是抛出异常
def get_user_all_schedule_logs(user_id, limit=50):