chore: update entry points for multi-account
- main.py calls migrate_legacy_state() before starting server - health.py accepts account_ids parameter for per-account health checks - _continuous_check returns per-account status instead of single snapshot
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import sqlite3
|
||||
from pathlib import Path
|
||||
from typing import Any, Dict
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from app_state import StateStore
|
||||
|
||||
@@ -11,6 +11,7 @@ def health_payload(
|
||||
state_store: StateStore,
|
||||
continuous_snapshot: Dict[str, Any],
|
||||
job_queue_size: int,
|
||||
account_ids: Optional[List[str]] = None,
|
||||
) -> Dict[str, Any]:
|
||||
checks = {
|
||||
"data_dir": _dir_check(data_dir, writable=True),
|
||||
@@ -20,6 +21,22 @@ def health_payload(
|
||||
"continuous": _continuous_check(continuous_snapshot),
|
||||
"job_queue": {"ok": True, "size": job_queue_size},
|
||||
}
|
||||
|
||||
# Per-account health
|
||||
if account_ids:
|
||||
account_checks: Dict[str, Any] = {}
|
||||
for acc_id in account_ids:
|
||||
acc_dir = data_dir / "accounts" / acc_id
|
||||
session_file = session_dir / f"{acc_id}.session"
|
||||
account_checks[acc_id] = {
|
||||
"data_dir": _dir_check(acc_dir),
|
||||
"session_file": {
|
||||
"ok": session_file.exists(),
|
||||
"path": str(session_file),
|
||||
},
|
||||
}
|
||||
checks["accounts"] = account_checks
|
||||
|
||||
ok = all(item.get("ok", False) for item in checks.values())
|
||||
return {"ok": ok, "status": "ok" if ok else "degraded", "checks": checks}
|
||||
|
||||
@@ -63,11 +80,19 @@ def _sqlite_check() -> Dict[str, Any]:
|
||||
|
||||
|
||||
def _continuous_check(snapshot: Dict[str, Any]) -> Dict[str, Any]:
|
||||
status = snapshot.get("status", {})
|
||||
config = snapshot.get("config", {})
|
||||
running_accounts = snapshot.get("running_accounts")
|
||||
if running_accounts is None:
|
||||
running_accounts = snapshot
|
||||
return {
|
||||
"ok": True,
|
||||
"enabled": bool(config.get("enabled")),
|
||||
"running": bool(status.get("running")),
|
||||
"last_error": status.get("last_error"),
|
||||
"account_count": len(running_accounts),
|
||||
"accounts": {
|
||||
aid: {
|
||||
"running": bool((info.get("status") or info).get("running")),
|
||||
"enabled": bool((info.get("config") or {}).get("enabled", False)),
|
||||
"last_error": (info.get("status") or info).get("last_error"),
|
||||
}
|
||||
for aid, info in running_accounts.items()
|
||||
if isinstance(info, dict)
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import logging
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from webui_server import run_server
|
||||
|
||||
@@ -11,6 +12,20 @@ def main():
|
||||
datefmt="%Y-%m-%d %H:%M:%S",
|
||||
stream=sys.stdout,
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Run legacy migration before starting the server
|
||||
from app_state import migrate_legacy_state
|
||||
|
||||
data_dir = Path("data")
|
||||
session_dir = Path("session")
|
||||
try:
|
||||
if migrate_legacy_state(data_dir, session_dir):
|
||||
logger.info("Legacy migration completed successfully.")
|
||||
except Exception as exc:
|
||||
logger.error("Migration failed: %s", exc)
|
||||
|
||||
run_server()
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user