This commit is contained in:
薇薇安 2026-01-13 23:29:55 +08:00
parent 65ca1d3d03
commit b41064f880

View File

@ -16,48 +16,100 @@ def _init_config_manager():
if _config_manager is not None:
return _config_manager
try:
# 使用基础日志(因为可能还没有配置好日志系统)
import sys
import logging
from pathlib import Path
logger = logging.getLogger(__name__)
try:
# 从trading_system目录向上两级到项目根目录然后找backend
project_root = Path(__file__).parent.parent
backend_path = project_root / 'backend'
logger.debug(f"尝试初始化配置管理器,项目根目录: {project_root}, backend路径: {backend_path}")
print(f"[配置管理器] 尝试初始化,项目根目录: {project_root}")
print(f"[配置管理器] backend路径: {backend_path}")
print(f"[配置管理器] backend目录存在: {backend_path.exists()}")
if backend_path.exists():
sys.path.insert(0, str(backend_path))
if not backend_path.exists():
print(f"[配置管理器] ❌ backend目录不存在: {backend_path}")
USE_DB_CONFIG = False
return None
# 检查backend目录内容
backend_files = list(backend_path.iterdir()) if backend_path.exists() else []
print(f"[配置管理器] backend目录内容: {[f.name for f in backend_files[:10]]}")
# 检查config_manager.py是否存在
config_manager_file = backend_path / 'config_manager.py'
print(f"[配置管理器] config_manager.py存在: {config_manager_file.exists()}")
if not config_manager_file.exists():
print(f"[配置管理器] ❌ config_manager.py不存在: {config_manager_file}")
USE_DB_CONFIG = False
return None
# 添加到sys.path
backend_str = str(backend_path)
if backend_str not in sys.path:
sys.path.insert(0, backend_str)
print(f"[配置管理器] 已添加路径到sys.path: {backend_str}")
# 尝试导入
try:
print(f"[配置管理器] 尝试导入config_manager...")
from config_manager import config_manager
print(f"[配置管理器] ✓ 导入成功")
# 测试数据库连接
try:
print(f"[配置管理器] 测试数据库连接...")
config_manager.reload()
print(f"[配置管理器] ✓ 数据库连接成功,已加载 {len(config_manager._cache)} 个配置项")
# 检查API密钥
api_key = config_manager.get('BINANCE_API_KEY')
api_secret = config_manager.get('BINANCE_API_SECRET')
print(f"[配置管理器] API密钥检查: KEY存在={bool(api_key)}, SECRET存在={bool(api_secret)}")
_config_manager = config_manager
USE_DB_CONFIG = True
logger.info("✓ 配置管理器初始化成功,将从数据库读取配置")
print(f"[配置管理器] ✓ 配置管理器初始化成功,将从数据库读取配置")
return config_manager
except ImportError as e:
logger.warning(f"无法导入config_manager: {e},将使用环境变量和默认配置")
USE_DB_CONFIG = False
return None
except Exception as e:
logger.warning(f"配置管理器初始化失败: {e},将使用环境变量和默认配置")
USE_DB_CONFIG = False
return None
else:
logger.warning(f"backend目录不存在: {backend_path},将使用环境变量和默认配置")
USE_DB_CONFIG = False
return None
except Exception as e:
import logging
logger = logging.getLogger(__name__)
logger.warning(f"配置管理器初始化异常: {e},将使用环境变量和默认配置")
except Exception as db_error:
print(f"[配置管理器] ⚠ 数据库连接失败: {db_error}")
print(f"[配置管理器] 错误类型: {type(db_error).__name__}")
import traceback
print(f"[配置管理器] 错误详情:\n{traceback.format_exc()}")
USE_DB_CONFIG = False
return None
# 初始化配置管理器
except ImportError as e:
print(f"[配置管理器] ❌ 无法导入config_manager: {e}")
import traceback
print(f"[配置管理器] 导入错误详情:\n{traceback.format_exc()}")
USE_DB_CONFIG = False
return None
except Exception as e:
print(f"[配置管理器] ❌ 配置管理器初始化失败: {e}")
import traceback
print(f"[配置管理器] 错误详情:\n{traceback.format_exc()}")
USE_DB_CONFIG = False
return None
except Exception as e:
print(f"[配置管理器] ❌ 初始化异常: {e}")
import traceback
print(f"[配置管理器] 异常详情:\n{traceback.format_exc()}")
USE_DB_CONFIG = False
return None
# 初始化配置管理器(在模块加载时执行)
# 注意此时日志系统可能还没初始化所以使用print输出
try:
_init_config_manager()
except Exception as e:
print(f"[config.py] 配置管理器初始化异常: {e}")
import traceback
print(traceback.format_exc())
def _get_config_value(key, default=None):
"""获取配置值(支持动态重载)"""