fix(startup): move all blocking calls to lifespan event

This commit is contained in:
Chris Christiansen 2026-06-23 11:54:44 +00:00
parent 4610521e73
commit 4dfcffd0b3
2 changed files with 3 additions and 6 deletions

View File

@ -13,7 +13,7 @@ import google.auth
import google.auth.transport.requests import google.auth.transport.requests
from typing import Literal from typing import Literal
import vertexai
from google.adk.agents import Agent from google.adk.agents import Agent
from google.adk.tools import FunctionTool from google.adk.tools import FunctionTool
from google.adk.runners import Runner from google.adk.runners import Runner
@ -25,9 +25,6 @@ logger = logging.getLogger(__name__)
PROJECT_ID = os.environ.get("GOOGLE_CLOUD_PROJECT", "propane-will-491900-m5") PROJECT_ID = os.environ.get("GOOGLE_CLOUD_PROJECT", "propane-will-491900-m5")
LOCATION = os.environ.get("GOOGLE_CLOUD_LOCATION", "us-central1") LOCATION = os.environ.get("GOOGLE_CLOUD_LOCATION", "us-central1")
# Initialiser Vertex AI eksplisitt — ADK krev dette for Cloud Run
vertexai.init(project=PROJECT_ID, location=LOCATION)
ORCHESTRATOR_MODEL = os.environ.get("ORCHESTRATOR_MODEL", "gemini-2.5-flash") ORCHESTRATOR_MODEL = os.environ.get("ORCHESTRATOR_MODEL", "gemini-2.5-flash")
SUBAGENT_MODEL = os.environ.get("SUBAGENT_MODEL", "gemini-2.5-flash") SUBAGENT_MODEL = os.environ.get("SUBAGENT_MODEL", "gemini-2.5-flash")
REASONING_MODEL = os.environ.get("REASONING_MODEL", "gemini-2.5-flash") REASONING_MODEL = os.environ.get("REASONING_MODEL", "gemini-2.5-flash")
@ -63,8 +60,6 @@ except ImportError as _e:
return flash_model if mode != "heavy" else pro_model return flash_model if mode != "heavy" else pro_model
class TokenBudgetExceeded(Exception): pass class TokenBudgetExceeded(Exception): pass
create_bq_table_if_not_exists()
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# OPAX MCP Client (inlina) # OPAX MCP Client (inlina)

View File

@ -10,6 +10,7 @@ from pathlib import Path
from fastapi import FastAPI, HTTPException, Request from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import JSONResponse, FileResponse from fastapi.responses import JSONResponse, FileResponse
from pydantic import BaseModel from pydantic import BaseModel
import vertexai
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -42,6 +43,7 @@ _budget_store: dict = {}
@asynccontextmanager @asynccontextmanager
async def lifespan(app: FastAPI): async def lifespan(app: FastAPI):
logger.info(f"OSVauco agent '{APP_NAME}' starting up") logger.info(f"OSVauco agent '{APP_NAME}' starting up")
vertexai.init(project=PROJECT_ID, location=os.environ.get("GOOGLE_CLOUD_LOCATION", "us-central1"))
create_bq_table_if_not_exists() create_bq_table_if_not_exists()
logger.info(f"Static dir: {STATIC_DIR} (exists={STATIC_DIR.exists()})") logger.info(f"Static dir: {STATIC_DIR} (exists={STATIC_DIR.exists()})")
yield yield