This commit is contained in:
薇薇安 2026-01-13 23:32:23 +08:00
parent b41064f880
commit f81e4fc04e
2 changed files with 23 additions and 1 deletions

View File

@ -29,7 +29,15 @@ except Exception:
project_root = Path(__file__).parent.parent project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root)) sys.path.insert(0, str(project_root))
# 延迟导入避免在trading_system中导入时因为缺少依赖而失败
try:
from database.models import TradingConfig from database.models import TradingConfig
except ImportError as e:
TradingConfig = None
import logging
logger = logging.getLogger(__name__)
logger.warning(f"无法导入TradingConfig: {e},配置管理器将无法使用数据库")
import logging import logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -44,6 +52,11 @@ class ConfigManager:
def _load_from_db(self): def _load_from_db(self):
"""从数据库加载配置""" """从数据库加载配置"""
if TradingConfig is None:
logger.warning("TradingConfig未导入无法从数据库加载配置")
self._cache = {}
return
try: try:
configs = TradingConfig.get_all() configs = TradingConfig.get_all()
for config in configs: for config in configs:
@ -74,6 +87,11 @@ class ConfigManager:
def set(self, key, value, config_type='string', category='general', description=None): def set(self, key, value, config_type='string', category='general', description=None):
"""设置配置(同时更新数据库和缓存)""" """设置配置(同时更新数据库和缓存)"""
if TradingConfig is None:
logger.warning("TradingConfig未导入无法更新数据库配置")
self._cache[key] = value
return
try: try:
TradingConfig.set(key, value, config_type, category, description) TradingConfig.set(key, value, config_type, category, description)
self._cache[key] = value self._cache[key] = value

View File

@ -3,3 +3,7 @@ python-binance==1.0.19
websocket-client==1.6.1 websocket-client==1.6.1
aiohttp==3.9.1 aiohttp==3.9.1
unicorn-binance-websocket-api==2.4.0 unicorn-binance-websocket-api==2.4.0
# 数据库依赖(用于从数据库读取配置)
pymysql==1.1.0
python-dotenv==1.0.0