fix: ignore empty env vars so API health check can start

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-08-02 09:19:04 +08:00
parent 458cc42dd5
commit 667cee33fa
3 changed files with 27 additions and 4 deletions
+22 -2
View File
@@ -1,16 +1,28 @@
from __future__ import annotations
import logging
from fastapi import APIRouter
from packages.config import get_settings
from packages.db import Repository
router = APIRouter(tags=["health"])
log = logging.getLogger("health")
@router.get("/health")
def health() -> dict:
s = get_settings()
try:
s = get_settings()
except Exception as e: # noqa: BLE001
log.exception("settings load failed: %s", e)
return {
"ok": False,
"service": "market_intel",
"product": "比特骆驼行情采集分析",
"error": f"settings: {e}",
}
repo = Repository(s.db_path)
try:
hb = repo.get_heartbeat()
@@ -29,5 +41,13 @@ def health() -> dict:
"option_quotes": repo.count_option_quotes(),
"index_ticks": repo.count_index_ticks(),
}
except Exception as e: # noqa: BLE001
log.exception("health db failed: %s", e)
return {
"ok": False,
"service": "market_intel",
"product": "比特骆驼行情采集分析",
"error": f"db: {e}",
}
finally:
repo.close()
repo.close()
+4 -2
View File
@@ -223,8 +223,10 @@ ensure_dotenv() {
ensure_env_key "${envf}" "AUTH_SECRET" "鉴权密钥(disabled 关闭)" "change-me"
ensure_env_key "${envf}" "ADMIN_USERNAME" "管理员用户名" "admin"
ensure_env_key "${envf}" "ADMIN_PASSWORD" "管理员密码" "admin123"
ensure_env_key "${envf}" "BACKUP_DIR" "" "/root/market_intel_backups"
ensure_env_key "${envf}" "BACKUP_AUTO_ENABLED" "自动备份 1/0" "1"
ensure_env_key "${envf}" "BACKUP_INTERVAL_HOURS" "自动备份间隔小时" "24"
ensure_env_key "${envf}" "BACKUP_KEEP_COUNT" "" "30"
ensure_env_key "${envf}" "OKX_API_KEY" "OKX API Key(可空)" ""
ensure_env_key "${envf}" "OKX_API_SECRET" "OKX API Secret(可空)" ""
ensure_env_key "${envf}" "OKX_API_PASSPHRASE" "OKX Passphrase(可空)" ""
@@ -323,7 +325,7 @@ verify_health() {
local url="http://127.0.0.1:${port}/health"
step "健康检查 ${url}"
local i
for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do
for i in $(seq 1 30); do
if curl -fsS "${url}" >/dev/null 2>&1; then
log "health OK"
curl -fsS "${url}" || true
@@ -332,7 +334,7 @@ verify_health() {
fi
sleep 2
done
log "警告: health 暂未就绪,请检查: docker compose -f ${REPO_ROOT}/docker-compose.yml logs"
log "警告: health 暂未就绪,请检查: docker compose -f ${REPO_ROOT}/docker-compose.yml logs api --tail 80"
return 1
}
+1
View File
@@ -13,6 +13,7 @@ class Settings(BaseSettings):
model_config = SettingsConfigDict(
env_file=".env",
env_file_encoding="utf-8",
env_ignore_empty=True,
extra="ignore",
)