chore: add .gitignore, clean tracked junk files
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -108,6 +108,34 @@ class CasdoorAuth:
|
||||
pass # 如果无法记录日志,忽略错误
|
||||
return None
|
||||
|
||||
def get_account_info(self, access_token):
|
||||
"""
|
||||
使用访问令牌获取完整账户信息(包含标签等字段)
|
||||
"""
|
||||
account_url = f"{self.endpoint}/api/get-account"
|
||||
headers = {
|
||||
'Authorization': f'Bearer {access_token}'
|
||||
}
|
||||
|
||||
try:
|
||||
response = requests.get(account_url, headers=headers)
|
||||
response.raise_for_status()
|
||||
result = response.json()
|
||||
|
||||
# Casdoor 通常返回 {"status": "ok", "data": {...}}
|
||||
if isinstance(result, dict):
|
||||
if result.get('status') == 'ok' and isinstance(result.get('data'), dict):
|
||||
return result['data']
|
||||
if 'data' in result and isinstance(result.get('data'), dict):
|
||||
return result['data']
|
||||
return result if isinstance(result, dict) else None
|
||||
except requests.exceptions.RequestException as e:
|
||||
try:
|
||||
current_app.logger.error(f"获取 Casdoor 账户信息失败: {str(e)}")
|
||||
except:
|
||||
pass
|
||||
return None
|
||||
|
||||
def verify_state(self, state):
|
||||
"""
|
||||
验证 state 参数,防止 CSRF 攻击
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user