chore: add .gitignore, clean tracked junk files

This commit is contained in:
2026-05-29 08:33:35 +08:00
parent 2d5c5d4c0d
commit 1ccb116f80
53 changed files with 189 additions and 2445 deletions
+39
View File
@@ -3,6 +3,7 @@ import time
from flask import current_app
import logging
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy import text
from scripts.models import db
from collections import defaultdict
@@ -121,6 +122,44 @@ def clear_table_cache(table_name):
if key in query_cache.access_times:
del query_cache.access_times[key]
# 数据库连接重试机制
def retry_database_connection(db_instance, app, max_retries=10, retry_delay=5):
"""
尝试连接数据库,在连接失败时进行重试
:param db_instance: SQLAlchemy数据库实例
:param app: Flask应用实例
:param max_retries: 最大重试次数
:param retry_delay: 重试间隔(秒)
:return: 连接成功返回True,失败返回False
"""
import sqlalchemy.exc as sa_exc
for attempt in range(max_retries):
try:
app.logger.info(f"尝试连接数据库... (尝试 {attempt + 1}/{max_retries})")
# 使用with语句确保应用上下文正确
with app.app_context():
# 尝试建立连接 - SQLAlchemy 2.0 兼容方式
with db_instance.engine.connect() as connection:
connection.execute(text("SELECT 1"))
connection.commit() # 确保事务提交
app.logger.info("数据库连接成功!")
return True
except (sa_exc.OperationalError, sa_exc.DatabaseError, Exception) as e:
app.logger.warning(f"数据库连接失败: {str(e)}")
if attempt < max_retries - 1:
app.logger.info(f"等待 {retry_delay} 秒后重试...")
time.sleep(retry_delay)
else:
app.logger.error(f"数据库连接失败,已重试 {max_retries} 次,放弃连接")
return False
return False
# 通用查询函数,包含异常处理和缓存
@db_operation()
@cache_query()