From 949838c5f2d37604128371d4368d30c1717e599b Mon Sep 17 00:00:00 2001 From: "Gan, Jimmy" Date: Thu, 2 Apr 2026 02:04:59 +0800 Subject: [PATCH] fix: use lifespan context manager instead of deprecated on_event for startup --- dashboard/backend/main.py | 55 +++++++++++++++++---------- dashboard/backend/routers/terminal.py | 3 +- 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/dashboard/backend/main.py b/dashboard/backend/main.py index 9a877da..49457bf 100644 --- a/dashboard/backend/main.py +++ b/dashboard/backend/main.py @@ -10,6 +10,7 @@ from routers import docker_router, gitea, files, terminal, system, chat_summary, import auth_service as auth_module import config from rbac import require_page +from contextlib import asynccontextmanager import asyncio import httpx @@ -21,8 +22,40 @@ from config import TELEGRAM_BOT_TOKEN, TELEGRAM_CHAT_ID, CORS_ORIGINS, SECRET_KE _tz_cst = timezone(timedelta(hours=8)) +@asynccontextmanager +async def lifespan(app: FastAPI): + # Startup + print("=== Application Startup ===") + + # Initialize OPC database + from db import opc_db + try: + await opc_db.init_db() + print("OPC database initialized") + except Exception as e: + print(f"Failed to initialize OPC database: {e}") + import traceback + traceback.print_exc() + + # Start agent executor service + from services import agent_executor + try: + asyncio.create_task(agent_executor.start_executor()) + print("Agent executor service started") + except Exception as e: + print(f"Failed to start agent executor: {e}") + import traceback + traceback.print_exc() + + asyncio.create_task(monitor_containers()) + + yield + + # Shutdown + print("=== Application Shutdown ===") + limiter = Limiter(key_func=get_remote_address) -app = FastAPI(title="NAS Dashboard") +app = FastAPI(title="NAS Dashboard", lifespan=lifespan) app.state.limiter = limiter app.add_middleware( @@ -111,26 +144,6 @@ async def monitor_containers(): await asyncio.sleep(60) -@app.on_event("startup") -async def startup_event(): - # Initialize OPC database - from db import opc_db - try: - await opc_db.init_db() - logging.getLogger(__name__).info("OPC database initialized") - except Exception as e: - logging.getLogger(__name__).error("Failed to initialize OPC database: %s", e) - - # Start agent executor service - from services import agent_executor - try: - asyncio.create_task(agent_executor.start_executor()) - logging.getLogger(__name__).info("Agent executor service started") - except Exception as e: - logging.getLogger(__name__).error("Failed to start agent executor: %s", e) - - asyncio.create_task(monitor_containers()) - @app.get("/api/health") async def health(): return {"status": "ok"} diff --git a/dashboard/backend/routers/terminal.py b/dashboard/backend/routers/terminal.py index da71cfb..c1d0ce1 100644 --- a/dashboard/backend/routers/terminal.py +++ b/dashboard/backend/routers/terminal.py @@ -55,9 +55,10 @@ def build_host_command(host_config: dict) -> str | None: quoted_container = shlex.quote(container) quoted_session = shlex.quote(session_name) quoted_marker = shlex.quote(PERSISTENCE_STATUS_PREFIX) + # Use login shell to ensure env vars are loaded from .bashrc attach_cmd = ( f"{docker_bin} exec -it {quoted_container} " - f"tmux new-session -A -s {quoted_session}" + f"bash -l -c 'cd /repos/nas-tools && tmux new-session -A -s {quoted_session}'" ) script = " ".join([