dev
This commit is contained in:
113
domain-api/app/services/runtime_status_service.py
Normal file
113
domain-api/app/services/runtime_status_service.py
Normal file
@@ -0,0 +1,113 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
from app.core.config import settings
|
||||
from app.core.db import get_db
|
||||
from app.core.redis_client import get_redis
|
||||
from app.services.runtime_settings_service import get_runtime_settings
|
||||
from app.services.worker_control_service import detect_worker_runtime
|
||||
|
||||
|
||||
def _runtime_log_path(filename: str) -> str:
|
||||
path = Path(__file__).resolve().parents[2] / "runtime" / "logs" / filename
|
||||
return str(path)
|
||||
|
||||
|
||||
def get_runtime_status() -> dict:
|
||||
runtime_settings = get_runtime_settings()
|
||||
worker_runtime = detect_worker_runtime()
|
||||
api_pid = os.getpid()
|
||||
|
||||
return {
|
||||
"api": {
|
||||
"service": "domain-api",
|
||||
"version": "0.1.0",
|
||||
"api_prefix": settings.api_prefix,
|
||||
"pid": api_pid,
|
||||
"host": settings.api_host,
|
||||
"port": settings.api_port,
|
||||
"mode": runtime_settings.get("worker_mode", "windows-local"),
|
||||
"service_name": runtime_settings.get("api_service_name", settings.api_service_name),
|
||||
"health_url": f"http://127.0.0.1:{settings.api_port}/health",
|
||||
"stdout_log": _runtime_log_path("domain-api.stdout.log"),
|
||||
"stderr_log": _runtime_log_path("domain-api.stderr.log"),
|
||||
},
|
||||
"worker": {
|
||||
"mode": worker_runtime.get("mode", runtime_settings.get("worker_mode", "windows-local")),
|
||||
"service_name": runtime_settings.get("worker_service_name", settings.worker_service_name),
|
||||
"running": worker_runtime.get("running", False),
|
||||
"process_count": worker_runtime.get("process_count", 0),
|
||||
"latest_start_time": worker_runtime.get("latest_start_time", ""),
|
||||
"message": worker_runtime.get("message", ""),
|
||||
"log_path": str(Path(settings.domain_root) / "detect_worker.log"),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def get_runtime_preflight() -> dict:
|
||||
runtime_settings = get_runtime_settings()
|
||||
checks: list[dict[str, object]] = []
|
||||
|
||||
domain_root = Path(settings.domain_root)
|
||||
checks.append(
|
||||
{
|
||||
"key": "domain_root",
|
||||
"label": "domainCheck 目录",
|
||||
"ok": domain_root.exists(),
|
||||
"message": str(domain_root),
|
||||
}
|
||||
)
|
||||
|
||||
try:
|
||||
with get_db() as conn:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute("select 1")
|
||||
cur.fetchone()
|
||||
checks.append({"key": "database", "label": "PostgreSQL", "ok": True, "message": f"{settings.db_host}:{settings.db_port}/{settings.db_database}"})
|
||||
except Exception as exc:
|
||||
checks.append({"key": "database", "label": "PostgreSQL", "ok": False, "message": str(exc)})
|
||||
|
||||
try:
|
||||
redis_client = get_redis()
|
||||
redis_client.ping()
|
||||
checks.append({"key": "redis", "label": "Redis", "ok": True, "message": f"{settings.redis_host}:{settings.redis_port}/{settings.redis_db}"})
|
||||
except Exception as exc:
|
||||
checks.append({"key": "redis", "label": "Redis", "ok": False, "message": str(exc)})
|
||||
|
||||
worker_mode = runtime_settings.get("worker_mode", "windows-local")
|
||||
checks.append({"key": "worker_mode", "label": "运行模式", "ok": True, "message": worker_mode})
|
||||
|
||||
if worker_mode == "linux-systemd":
|
||||
checks.append(
|
||||
{
|
||||
"key": "worker_service_name",
|
||||
"label": "Worker service 名",
|
||||
"ok": bool(runtime_settings.get("worker_service_name")),
|
||||
"message": runtime_settings.get("worker_service_name", ""),
|
||||
}
|
||||
)
|
||||
checks.append(
|
||||
{
|
||||
"key": "api_service_name",
|
||||
"label": "API service 名",
|
||||
"ok": bool(runtime_settings.get("api_service_name")),
|
||||
"message": runtime_settings.get("api_service_name", ""),
|
||||
}
|
||||
)
|
||||
else:
|
||||
checks.append(
|
||||
{
|
||||
"key": "windows_scripts",
|
||||
"label": "Windows 启停脚本",
|
||||
"ok": (Path(settings.domain_root).parent / "start_domain_api.ps1").exists() and (Path(settings.domain_root).parent / "stop_domain_api.ps1").exists(),
|
||||
"message": "start_domain_api.ps1 / stop_domain_api.ps1",
|
||||
}
|
||||
)
|
||||
|
||||
overall_ok = all(bool(item["ok"]) for item in checks)
|
||||
return {
|
||||
"ok": overall_ok,
|
||||
"checks": checks,
|
||||
}
|
||||
Reference in New Issue
Block a user