a
This commit is contained in:
parent
fb3d1a0dda
commit
6504efbf15
|
|
@ -8,6 +8,8 @@ from typing import Optional
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
# 加载 .env 文件(优先从 trading_system/.env,其次从 backend/.env,再到项目根目录/.env)
|
# 加载 .env 文件(优先从 trading_system/.env,其次从 backend/.env,再到项目根目录/.env)
|
||||||
|
# 重要:对于 ATS_ACCOUNT_ID,如果 supervisor 已设置(交易进程),则不要从 .env 覆盖
|
||||||
|
# .env 中的 ATS_ACCOUNT_ID 只用于 recommendations_main.py 的默认值
|
||||||
try:
|
try:
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
trading_system_dir = Path(__file__).parent
|
trading_system_dir = Path(__file__).parent
|
||||||
|
|
@ -18,14 +20,26 @@ try:
|
||||||
backend_dir / '.env', # backend/.env(线上常见放这里:DB/REDIS/ATS_MASTER_KEY)
|
backend_dir / '.env', # backend/.env(线上常见放这里:DB/REDIS/ATS_MASTER_KEY)
|
||||||
project_root / '.env', # 项目根目录/.env
|
project_root / '.env', # 项目根目录/.env
|
||||||
]
|
]
|
||||||
|
# 先保存 supervisor 设置的 ATS_ACCOUNT_ID(如果存在)
|
||||||
|
# supervisor 在启动交易进程时会设置这个环境变量,优先级应该高于 .env
|
||||||
|
supervisor_account_id = os.getenv("ATS_ACCOUNT_ID")
|
||||||
|
|
||||||
for env_file in env_files:
|
for env_file in env_files:
|
||||||
if env_file.exists():
|
if env_file.exists():
|
||||||
|
# 加载 .env 文件(会覆盖环境变量)
|
||||||
load_dotenv(env_file, override=True)
|
load_dotenv(env_file, override=True)
|
||||||
print(f"[config.py] 已加载 .env 文件: {env_file}")
|
print(f"[config.py] 已加载 .env 文件: {env_file}")
|
||||||
|
# 如果 supervisor 设置了 ATS_ACCOUNT_ID,恢复它(交易进程使用 supervisor 的值,不依赖 .env)
|
||||||
|
if supervisor_account_id:
|
||||||
|
os.environ["ATS_ACCOUNT_ID"] = supervisor_account_id
|
||||||
|
print(f"[config.py] 恢复 supervisor 设置的 ATS_ACCOUNT_ID={supervisor_account_id}(交易进程使用此值,不依赖 .env)")
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
# 如果都不存在,尝试加载但不报错
|
# 如果都不存在,尝试加载但不报错
|
||||||
load_dotenv(project_root / '.env', override=False)
|
load_dotenv(project_root / '.env', override=False)
|
||||||
|
# 同样恢复 supervisor 设置的值
|
||||||
|
if supervisor_account_id:
|
||||||
|
os.environ["ATS_ACCOUNT_ID"] = supervisor_account_id
|
||||||
except ImportError:
|
except ImportError:
|
||||||
# python-dotenv 未安装时忽略
|
# python-dotenv 未安装时忽略
|
||||||
pass
|
pass
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user