Files
HSAP/scripts/wait_for_db.py
Chengfang Lu 7c43b44c57 feat: initial HSAP platform
Huaxu Sentinel Active Safety Platform with embedded algorithm code,
Docker Compose setup, and vendored dataset scaffolds for clone-and-run.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-25 16:59:59 +08:00

62 lines
1.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""等待 PostgreSQL + Redis 就绪。"""
from __future__ import annotations
import os
import sys
import time
from as_platform.config import IS_POSTGRES, JOB_EXECUTOR, REDIS_URL
from as_platform.db.engine import check_connection
from as_platform.redis.bus import ping_redis
def main() -> None:
ok_db = True
if IS_POSTGRES:
ok_db = False
for i in range(30):
if check_connection():
ok_db = True
print("PostgreSQL 已就绪")
break
print(f"等待 PostgreSQL… ({i + 1}/30)")
time.sleep(2)
else:
if check_connection():
print("数据库已就绪")
else:
print("数据库连接失败", file=sys.stderr)
sys.exit(1)
redis_required = JOB_EXECUTOR == "worker" or os.environ.get("AS_REQUIRE_REDIS", "").lower() in (
"1",
"true",
"yes",
)
if not REDIS_URL:
print("Redis 未配置,跳过")
if not ok_db:
sys.exit(1)
return
for i in range(30):
if ping_redis():
print("Redis 已就绪")
break
print(f"等待 Redis… ({i + 1}/30)")
time.sleep(2)
else:
if redis_required:
print("Redis 连接超时", file=sys.stderr)
sys.exit(1)
print("警告: Redis 不可用thread 模式仍可启动")
if not ok_db:
print("PostgreSQL 连接超时", file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()