|
|
|
@ -13,25 +13,50 @@ from app.core.logger import info, error
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AdapterService:
|
|
|
|
class AdapterService:
|
|
|
|
"""适配器管理服务"""
|
|
|
|
"""适配器管理服务(单例模式)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
确保整个应用中只有一个 AdapterService 实例,
|
|
|
|
|
|
|
|
避免重复创建导致适配器连接状态丢失。
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_instance: Optional['AdapterService'] = None
|
|
|
|
|
|
|
|
_lock = RLock()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __new__(cls) -> 'AdapterService':
|
|
|
|
|
|
|
|
if cls._instance is None:
|
|
|
|
|
|
|
|
with cls._lock:
|
|
|
|
|
|
|
|
if cls._instance is None:
|
|
|
|
|
|
|
|
cls._instance = super().__new__(cls)
|
|
|
|
|
|
|
|
cls._instance._initialized = False
|
|
|
|
|
|
|
|
return cls._instance
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
def __init__(self):
|
|
|
|
self.lock = RLock()
|
|
|
|
# 避免重复初始化
|
|
|
|
|
|
|
|
if self._initialized:
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
with self._lock:
|
|
|
|
|
|
|
|
if self._initialized:
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.lock = RLock()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 已注册的适配器工厂
|
|
|
|
|
|
|
|
self.factories: Dict[str, Callable[[], DataSourceAdapter]] = {}
|
|
|
|
|
|
|
|
|
|
|
|
# 已注册的适配器工厂
|
|
|
|
# 适配器配置
|
|
|
|
self.factories: Dict[str, Callable[[], DataSourceAdapter]] = {}
|
|
|
|
self.configs: Dict[str, dict] = {}
|
|
|
|
|
|
|
|
|
|
|
|
# 适配器配置
|
|
|
|
# 当前激活的适配器实例
|
|
|
|
self.configs: Dict[str, dict] = {}
|
|
|
|
self.active_adapters: Dict[str, DataSourceAdapter] = {}
|
|
|
|
|
|
|
|
|
|
|
|
# 当前激活的适配器实例
|
|
|
|
# 适配器元数据
|
|
|
|
self.active_adapters: Dict[str, DataSourceAdapter] = {}
|
|
|
|
self.metadata: Dict[str, dict] = {}
|
|
|
|
|
|
|
|
|
|
|
|
# 适配器元数据
|
|
|
|
# 注册内置适配器
|
|
|
|
self.metadata: Dict[str, dict] = {}
|
|
|
|
self._register_builtin_adapters()
|
|
|
|
|
|
|
|
|
|
|
|
# 注册内置适配器
|
|
|
|
self._initialized = True
|
|
|
|
self._register_builtin_adapters()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _register_builtin_adapters(self):
|
|
|
|
def _register_builtin_adapters(self):
|
|
|
|
"""注册内置适配器"""
|
|
|
|
"""注册内置适配器"""
|
|
|
|
@ -155,12 +180,15 @@ class AdapterService:
|
|
|
|
print(f"Using adapter: {active_name}")
|
|
|
|
print(f"Using adapter: {active_name}")
|
|
|
|
# 返回已激活的适配器实例
|
|
|
|
# 返回已激活的适配器实例
|
|
|
|
if active_name in self.active_adapters:
|
|
|
|
if active_name in self.active_adapters:
|
|
|
|
|
|
|
|
print(f"Adapter {active_name} already connected, reusing...")
|
|
|
|
return self.active_adapters[active_name]
|
|
|
|
return self.active_adapters[active_name]
|
|
|
|
|
|
|
|
|
|
|
|
# 如果配置为启用但未激活,尝试连接
|
|
|
|
# 如果适配器已注册但未连接,尝试连接
|
|
|
|
if active_name in self.configs and self.configs[active_name].get("enabled"):
|
|
|
|
# 检查适配器是否已注册(在 factories 中)
|
|
|
|
# 启动异步连接(不等待)
|
|
|
|
if active_name in self.factories:
|
|
|
|
asyncio.create_task(self._connect_adapter(active_name))
|
|
|
|
print(f"Adapter {active_name} registered but not connected, connecting...")
|
|
|
|
|
|
|
|
# 注意:这里不直接调用异步方法,让调用方处理连接
|
|
|
|
|
|
|
|
# 返回 None 表示需要连接,由服务层调用 _connect_adapter
|
|
|
|
|
|
|
|
|
|
|
|
# 如果没有激活的适配器,返回None
|
|
|
|
# 如果没有激活的适配器,返回None
|
|
|
|
return None
|
|
|
|
return None
|
|
|
|
|