From 169df9b50369463f420adc3946441640a5522f0a Mon Sep 17 00:00:00 2001 From: xiaoxia-saas-bot Date: Sun, 4 Oct 2026 15:51:39 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20loader=E5=9C=A8api=E8=BF=9B=E7=A8=8B?= =?UTF-8?q?=E5=86=85=E6=87=92=E5=BB=BA=E5=90=8C=E6=AD=A5=E5=BC=95=E6=93=8E?= =?UTF-8?q?=EF=BC=8C=E6=94=AF=E6=8C=81DB=E7=83=AD=E5=8A=A0=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/viral_video/prompt_loader.py | 33 +++++++++++++++++-- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/packages/application/viral_video/prompt_loader.py b/packages/application/viral_video/prompt_loader.py index 32af8898c..611f5f709 100644 --- a/packages/application/viral_video/prompt_loader.py +++ b/packages/application/viral_video/prompt_loader.py @@ -50,12 +50,39 @@ def _fallback(prompt_type: str) -> Optional[PromptTemplate]: return None -def _load_from_db(prompt_type: str) -> Optional[PromptTemplate]: - if _session_mod.SessionLocal is None: +_lazy_session = None + + +def _get_session(): + """优先用全局 SessionLocal(worker);否则按应用配置懒建同步引擎(api)。""" + global _lazy_session + if _session_mod.SessionLocal is not None: + return _session_mod.SessionLocal() + if _lazy_session is not None: + return _lazy_session() + try: + from packages.config import get_shared_settings + + url = str(get_shared_settings().database_url) + except Exception: # noqa: BLE001 return None + if not url: + return None + url = url.replace("postgresql+asyncpg://", "postgresql+psycopg://") + url = url.replace("postgresql://", "postgresql+psycopg://") if url.startswith("postgresql://") else url + engine = sa.create_engine(url, pool_pre_ping=True, pool_size=2, max_overflow=2) + from sqlalchemy.orm import sessionmaker + + _lazy_session = sessionmaker(bind=engine) + return _lazy_session() + + +def _load_from_db(prompt_type: str) -> Optional[PromptTemplate]: session = None try: - session = _session_mod.SessionLocal() + session = _get_session() + if session is None: + return None sql = sa.text(""" SELECT name, prompt_type, version, system_prompt, user_prompt_template, COALESCE(example_output, '') AS example_output, -- 2.54.0