From 93f2b94dd2c8bfef5b1db93b5ede72cfe3750aaf Mon Sep 17 00:00:00 2001 From: mr-forust Date: Sat, 27 Jun 2026 15:21:02 +0200 Subject: [PATCH] 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 --- health.py | 37 +++++++++++++++++++++++++++++++------ main.py | 15 +++++++++++++++ 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/health.py b/health.py index f7a1ba8..2bdaed8 100644 --- a/health.py +++ b/health.py @@ -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) + }, } diff --git a/main.py b/main.py index e622697..7bc8875 100644 --- a/main.py +++ b/main.py @@ -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()