checkpoint: 架构治理四件套——统一 async / 测试真 PG / Redis 降级可选 / DPB 扶正
- A1 业务层统一 async:gencode 表内省改 run_sync 异步(不再阻塞事件循环)、 check_db 改 async_engine、dict_util 启动预热异步载入;psycopg 降为 APScheduler SQLAlchemyJobStore 专用同步孤岛(注释标注) - A2 测试真 PG 化:conftest 弃 SQLite + mock Redis,改连真实 PG16(dpb_test)+ Redis(number_gen advisory lock 路径真被测);新增 docker/test-compose.yaml 测试依赖栈(postgres:16:5433 + redis:7:6380);run_ci 前置探测 + 容器兜底, pytest -m pg 10 套正式 tc 全过(真实 PG16 + Redis) - B1 Redis 启动不强依赖:redis_connect 失败降级启动,缓存类回源 DB、存储类 (会话/AI 配置/调度)经 require_redis 守卫返回 503;真机 3 阶段降级测试 PASS=11 FAIL=0(独立 Redis 6390 + 后端 8091,不动共享 dev 服务) - C 扶正 DPB:后端横幅/日志(dpb.log)/README/pyproject/ai_factory agent 名、 前端 package.json/署名注释/链接文案/deploy.sh/docker 注释全部去 fastapiadmin; grep backend/app + frontend/web/src 零残留 - 验证:全量回归 pass=72 fail=0
This commit is contained in:
@@ -20,12 +20,44 @@ from app.core.exceptions import CustomException
|
||||
_value2label_cache: dict[str, dict[str, str]] | None = None
|
||||
|
||||
|
||||
def _rows_to_mapping(rows) -> dict[str, dict[str, str]]:
|
||||
mapping: dict[str, dict[str, str]] = {}
|
||||
for dt, dv, dl in rows:
|
||||
mapping.setdefault(dt, {})[dv] = dl
|
||||
return mapping
|
||||
|
||||
|
||||
async def preload_value2label_cache() -> None:
|
||||
"""应用启动时用异步引擎预载全量字典映射(value→label)。
|
||||
|
||||
batch_export 均为同步 staticmethod(控制器同步调用、bytes 序列化),无法 await,
|
||||
故启动时预热一次进程内缓存,导出请求路径只做内存查找、不触达同步引擎。
|
||||
"""
|
||||
global _value2label_cache
|
||||
if _value2label_cache is not None:
|
||||
return
|
||||
from sqlalchemy import select
|
||||
|
||||
from app.api.v1.module_system.dict.model import DictDataModel
|
||||
from app.core.database import async_db_session
|
||||
|
||||
async with async_db_session() as session:
|
||||
rows = (
|
||||
await session.execute(
|
||||
select(DictDataModel.dict_type, DictDataModel.dict_value, DictDataModel.dict_label)
|
||||
.where(DictDataModel.is_deleted.is_(False))
|
||||
)
|
||||
).all()
|
||||
_value2label_cache = _rows_to_mapping(rows)
|
||||
|
||||
|
||||
def dict_value_to_label(dict_type: str, value: Any) -> Any:
|
||||
"""把英码 dict_value 翻译为中文 dict_label(导出用)。
|
||||
|
||||
与 DictLabelResolver 方向相反:导出时把库内的英码还原为中文,便于用户阅读;
|
||||
值空或无法识别时原样返回,保证"导出的文件再导入"仍可解析。
|
||||
使用同步引擎读取 sys_dict_data 并进程内缓存,避免每次导出都查库。
|
||||
应用启动经 preload_value2label_cache 预热后纯内存查找;脚本等未走 lifespan 的
|
||||
上下文兜底用同步会话载入(见 create_engine_and_session 的调度器孤岛注释)。
|
||||
"""
|
||||
if value is None:
|
||||
return value
|
||||
@@ -39,15 +71,12 @@ def dict_value_to_label(dict_type: str, value: Any) -> Any:
|
||||
from app.api.v1.module_system.dict.model import DictDataModel
|
||||
from app.core.database import db_session
|
||||
|
||||
mapping: dict[str, dict[str, str]] = {}
|
||||
with db_session() as session:
|
||||
rows = session.execute(
|
||||
select(DictDataModel.dict_type, DictDataModel.dict_value, DictDataModel.dict_label)
|
||||
.where(DictDataModel.is_deleted.is_(False))
|
||||
).all()
|
||||
for dt, dv, dl in rows:
|
||||
mapping.setdefault(dt, {})[dv] = dl
|
||||
_value2label_cache = mapping
|
||||
_value2label_cache = _rows_to_mapping(rows)
|
||||
return _value2label_cache.get(dict_type, {}).get(text, value)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user