剥离无效事件系统并收口插件统计链路

- 删除未被实际消费的事件系统实现与相关发布逻辑
- 将插件调用统计改为在机器人主链路中直接埋点记录
- 重构统计收集插件初始化与记录方式,移除事件总线依赖
- 同步更新工程优化文档中的性能与链路治理描述
This commit is contained in:
liuwei
2026-04-30 14:54:22 +08:00
parent 78e4f50b7e
commit 0878f0d4ea
7 changed files with 174 additions and 284 deletions

View File

@@ -1,85 +0,0 @@
from enum import Enum, auto
from typing import Dict, Any, Callable, List
import threading
class EventType(Enum):
"""事件类型枚举"""
SYSTEM_STARTUP = auto()
SYSTEM_SHUTDOWN = auto()
PLUGIN_LOADED = auto()
PLUGIN_UNLOADED = auto()
MESSAGE_RECEIVED = auto()
MESSAGE_PROCESSED = auto()
CUSTOM_EVENT = auto()
class EventSystem:
"""事件系统,用于插件间通信"""
_instance = None
_lock = threading.Lock()
def __new__(cls):
with cls._lock:
if cls._instance is None:
cls._instance = super(EventSystem, cls).__new__(cls)
cls._instance._subscribers = {}
cls._instance._initialized = False
return cls._instance
def __init__(self):
if not self._initialized:
self._subscribers = {}
self._initialized = True
def subscribe(self, event_type: EventType, callback: Callable[[Dict[str, Any]], None]) -> None:
"""
订阅事件
Args:
event_type: 事件类型
callback: 回调函数,接收事件数据
"""
if event_type not in self._subscribers:
self._subscribers[event_type] = []
if callback not in self._subscribers[event_type]:
self._subscribers[event_type].append(callback)
def unsubscribe(self, event_type: EventType, callback: Callable[[Dict[str, Any]], None]) -> None:
"""
取消订阅事件
Args:
event_type: 事件类型
callback: 回调函数
"""
if event_type in self._subscribers and callback in self._subscribers[event_type]:
self._subscribers[event_type].remove(callback)
def publish(self, event_type: EventType, data: Dict[str, Any]) -> None:
"""
发布事件
Args:
event_type: 事件类型
data: 事件数据
"""
if event_type in self._subscribers:
for callback in self._subscribers[event_type]:
try:
callback(data)
except Exception as e:
print(f"事件处理错误: {e}")
def get_subscribers(self, event_type: EventType) -> List[Callable]:
"""
获取事件订阅者
Args:
event_type: 事件类型
Returns:
订阅者列表
"""
return self._subscribers.get(event_type, [])

View File

@@ -12,7 +12,6 @@ from base.plugin_common.plugin_interface import PluginInterface, PluginStatus
from base.plugin_common.message_plugin_interface import MessagePluginInterface
from base.plugin_common.scheduled_plugin_interface import ScheduledPluginInterface
from base.plugin_common.plugin_registry import PluginRegistry
from base.plugin_common.event_system import EventSystem, EventType
from utils.decorator.async_job import async_job
from wechat_ipad import WechatAPIClient
@@ -431,9 +430,6 @@ class PluginManager:
self._refresh_module_file_state(module_name)
# self.LOG.info(f"PluginManager添加模块映射 {module_name} -> {display_name}")
# 发布插件加载事件
EventSystem().publish(EventType.PLUGIN_LOADED, {"plugin": plugin})
return plugin
else:
self.LOG.error(f"PluginManager插件模块 {module_name} 的 get_plugin() 返回的不是有效的插件实例")
@@ -483,9 +479,6 @@ class PluginManager:
self._refresh_module_file_state(module_name)
# self.LOG.info(f"PluginManager添加模块映射 {module_name} -> {display_name}")
# 发布插件加载事件
EventSystem().publish(EventType.PLUGIN_LOADED, {"plugin": plugin})
return plugin
except Exception as e:
@@ -544,9 +537,6 @@ class PluginManager:
# 移除插件实例
del self.plugins[display_name]
# 发布插件卸载事件
EventSystem().publish(EventType.PLUGIN_UNLOADED, {"plugin_name": display_name})
return True
def reload_plugin(self, name: str) -> Optional[PluginInterface]: