fix: patch sqlor.dbpools BEFORE any other imports to prevent stale references
This commit is contained in:
parent
c34abef54a
commit
cc7ab76377
45
app/pccs.py
45
app/pccs.py
@ -4,11 +4,26 @@ pccs — 算力中心集群系统 (Pooled Computing Cluster System)
|
||||
"""
|
||||
import os, sys
|
||||
|
||||
from ahserver.webapp import webapp
|
||||
# ═══ 最早导入并 patch sqlor, 确保所有后续 import 拿到 patched 版本 ═══
|
||||
import sqlor.dbpools as _dbp
|
||||
from appPublic.jsonConfig import getConfig as _getConfig
|
||||
|
||||
# ═══ 模块级 Monkey Patches (必须在任何 rbac/sqlor 使用前执行) ═══
|
||||
_workdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
_config = _getConfig(_workdir, NS={'workdir': _workdir})
|
||||
_global_db = _dbp.DBPools(_config.databases)
|
||||
|
||||
# 1. DictObject.__getattr__ — 所有 ServerEnv 实例默认 get_module_dbname
|
||||
_orig_sor_ctx = _dbp.get_sor_context
|
||||
|
||||
|
||||
async def _patched_sor_ctx(env, modulename, errorback=None):
|
||||
dbname = env.get_module_dbname(modulename)
|
||||
async with _global_db.sqlorContext(dbname) as sor:
|
||||
yield sor
|
||||
|
||||
|
||||
_dbp.get_sor_context = _patched_sor_ctx
|
||||
|
||||
# ═══ DictObject patch =════
|
||||
from appPublic.dictObject import DictObject
|
||||
|
||||
_orig_getattr = DictObject.__getattr__
|
||||
@ -24,28 +39,8 @@ def _patched_getattr(self, key):
|
||||
|
||||
DictObject.__getattr__ = _patched_getattr
|
||||
|
||||
# 2. get_sor_context — 复用全局 DBPools 而非创建空实例
|
||||
import os as _os
|
||||
from appPublic.jsonConfig import getConfig as _getConfig
|
||||
import sqlor.dbpools as _dbp
|
||||
from sqlor.dbpools import DBPools
|
||||
|
||||
_workdir = _os.path.dirname(_os.path.dirname(_os.path.abspath(__file__)))
|
||||
_config = _getConfig(_workdir, NS={'workdir': _workdir})
|
||||
_global_db = DBPools(_config.databases)
|
||||
|
||||
_orig_sor_ctx = _dbp.get_sor_context
|
||||
|
||||
|
||||
async def _patched_sor_ctx(env, modulename, errorback=None):
|
||||
dbname = env.get_module_dbname(modulename)
|
||||
print('[pccs] get_sor_context called, dbname=' + str(dbname), flush=True)
|
||||
async with _global_db.sqlorContext(dbname) as sor:
|
||||
yield sor
|
||||
|
||||
|
||||
_dbp.get_sor_context = _patched_sor_ctx
|
||||
print('[pccs] patched get_sor_context', flush=True)
|
||||
# ═══ 现在才 import 其他模块 ═══
|
||||
from ahserver.webapp import webapp
|
||||
|
||||
|
||||
def init():
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user