fix: Pipeline 处理添加耗时日志和异常捕获
- message_pipeline.handle_message 添加每步耗时日志(租户解析/会话管理/消息处理) - feishu_longconn_service Pipeline 调用包裹 try/catch,异常时记录完整堆栈 - feishu_bot.py 同样添加 Pipeline 异常捕获 - 防止 Pipeline 内部异常被静默吞掉导致消息无回复
This commit is contained in:
Binary file not shown.
@@ -63,10 +63,8 @@ class DatabaseManager:
|
|||||||
autoflush=False,
|
autoflush=False,
|
||||||
bind=self.engine
|
bind=self.engine
|
||||||
)
|
)
|
||||||
|
|
||||||
# 创建所有表
|
# 创建所有表
|
||||||
Base.metadata.create_all(bind=self.engine)
|
Base.metadata.create_all(bind=self.engine)
|
||||||
logger.info("数据库初始化成功")
|
|
||||||
|
|
||||||
# 运行 schema 迁移(处理字段变更)
|
# 运行 schema 迁移(处理字段变更)
|
||||||
self._run_migrations()
|
self._run_migrations()
|
||||||
@@ -169,7 +167,6 @@ class DatabaseManager:
|
|||||||
if self.engine:
|
if self.engine:
|
||||||
self.engine.dispose()
|
self.engine.dispose()
|
||||||
self._initialize_database()
|
self._initialize_database()
|
||||||
logger.info("数据库重新连接成功")
|
|
||||||
return True
|
return True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"数据库重新连接失败: {e}")
|
logger.error(f"数据库重新连接失败: {e}")
|
||||||
|
|||||||
@@ -191,7 +191,6 @@ def create_session_store() -> SessionStore:
|
|||||||
socket_timeout=2
|
socket_timeout=2
|
||||||
)
|
)
|
||||||
client.ping()
|
client.ping()
|
||||||
logger.info("会话存储使用 Redis")
|
|
||||||
return RedisSessionStore(client)
|
return RedisSessionStore(client)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.info(f"Redis 不可用({e}),会话存储使用内存")
|
logger.info(f"Redis 不可用({e}),会话存储使用内存")
|
||||||
|
|||||||
@@ -73,9 +73,20 @@ class MessagePipeline:
|
|||||||
完整的消息处理流程(一步到位)。
|
完整的消息处理流程(一步到位)。
|
||||||
各入口可以直接调用此方法,不需要自己管理会话。
|
各入口可以直接调用此方法,不需要自己管理会话。
|
||||||
"""
|
"""
|
||||||
|
import time as _time
|
||||||
|
t0 = _time.time()
|
||||||
|
|
||||||
resolved_tenant = self.resolve_tenant(chat_id=chat_id, tenant_id=tenant_id)
|
resolved_tenant = self.resolve_tenant(chat_id=chat_id, tenant_id=tenant_id)
|
||||||
|
t1 = _time.time()
|
||||||
|
|
||||||
session_id = self.get_or_create_session(user_id, resolved_tenant, work_order_id)
|
session_id = self.get_or_create_session(user_id, resolved_tenant, work_order_id)
|
||||||
|
t2 = _time.time()
|
||||||
|
|
||||||
result = self.process(session_id, message, ip_address, invocation_method)
|
result = self.process(session_id, message, ip_address, invocation_method)
|
||||||
|
t3 = _time.time()
|
||||||
|
|
||||||
|
logger.info(f"Pipeline 耗时: 租户解析={t1-t0:.2f}s, 会话管理={t2-t1:.2f}s, 消息处理={t3-t2:.2f}s, 总计={t3-t0:.2f}s")
|
||||||
|
|
||||||
result['tenant_id'] = resolved_tenant
|
result['tenant_id'] = resolved_tenant
|
||||||
result['session_id'] = session_id
|
result['session_id'] = session_id
|
||||||
return result
|
return result
|
||||||
|
|||||||
@@ -165,14 +165,19 @@ class FeishuLongConnService:
|
|||||||
|
|
||||||
# 使用 Pipeline 统一处理消息
|
# 使用 Pipeline 统一处理消息
|
||||||
session_user_id = f"feishu_{chat_id}_{sender_id}"
|
session_user_id = f"feishu_{chat_id}_{sender_id}"
|
||||||
pipeline = service_manager.get_pipeline()
|
try:
|
||||||
response_data = pipeline.handle_message(
|
pipeline = service_manager.get_pipeline()
|
||||||
user_id=session_user_id,
|
logger.info(f"Pipeline 开始处理: user={session_user_id}, chat_id={chat_id}")
|
||||||
message=text_content,
|
response_data = pipeline.handle_message(
|
||||||
chat_id=chat_id,
|
user_id=session_user_id,
|
||||||
ip_address=f"feishu:{sender_id}:{sender_name}",
|
message=text_content,
|
||||||
invocation_method=f"feishu_longconn({chat_type})"
|
chat_id=chat_id,
|
||||||
)
|
ip_address=f"feishu:{sender_id}:{sender_name}",
|
||||||
|
invocation_method=f"feishu_longconn({chat_type})"
|
||||||
|
)
|
||||||
|
except Exception as pipe_err:
|
||||||
|
logger.error(f"Pipeline 处理异常: {pipe_err}", exc_info=True)
|
||||||
|
response_data = {"success": False, "error": str(pipe_err)}
|
||||||
tenant_id = response_data.get('tenant_id', 'default')
|
tenant_id = response_data.get('tenant_id', 'default')
|
||||||
logger.info(f"处理结果: success={response_data.get('success')}, 租户={tenant_id}")
|
logger.info(f"处理结果: success={response_data.get('success')}, 租户={tenant_id}")
|
||||||
|
|
||||||
|
|||||||
@@ -119,15 +119,20 @@ def _process_message_in_background(app, event_data: dict):
|
|||||||
logger.info(f"[Feishu Bot] 发送者={sender_name}({sender_id}), 群={chat_id}, 租户={tenant_id}")
|
logger.info(f"[Feishu Bot] 发送者={sender_name}({sender_id}), 群={chat_id}, 租户={tenant_id}")
|
||||||
|
|
||||||
# 4. 使用 Pipeline 统一处理消息
|
# 4. 使用 Pipeline 统一处理消息
|
||||||
pipeline = service_manager.get_pipeline()
|
try:
|
||||||
response_data = pipeline.handle_message(
|
pipeline = service_manager.get_pipeline()
|
||||||
user_id=user_id,
|
logger.info(f"[Feishu Bot] Pipeline 开始处理: user={user_id}")
|
||||||
message=text_content,
|
response_data = pipeline.handle_message(
|
||||||
tenant_id=tenant_id,
|
user_id=user_id,
|
||||||
chat_id=chat_id,
|
message=text_content,
|
||||||
ip_address=f"feishu:{sender_id}:{sender_name}",
|
tenant_id=tenant_id,
|
||||||
invocation_method=f"feishu_bot({chat_type})"
|
chat_id=chat_id,
|
||||||
)
|
ip_address=f"feishu:{sender_id}:{sender_name}",
|
||||||
|
invocation_method=f"feishu_bot({chat_type})"
|
||||||
|
)
|
||||||
|
except Exception as pipe_err:
|
||||||
|
logger.error(f"[Feishu Bot] Pipeline 处理异常: {pipe_err}", exc_info=True)
|
||||||
|
response_data = {"success": False, "error": str(pipe_err)}
|
||||||
logger.info(f"[Feishu Bot] 处理结果: success={response_data.get('success')}")
|
logger.info(f"[Feishu Bot] 处理结果: success={response_data.get('success')}")
|
||||||
|
|
||||||
# 5. 提取回复并发送
|
# 5. 提取回复并发送
|
||||||
|
|||||||
@@ -48,9 +48,6 @@ def start_feishu_longconn_service():
|
|||||||
try:
|
try:
|
||||||
import logging
|
import logging
|
||||||
logger = logging.getLogger("feishu_longconn")
|
logger = logging.getLogger("feishu_longconn")
|
||||||
logger.info("=" * 80)
|
|
||||||
logger.info("🚀 启动飞书长连接服务(后台线程)")
|
|
||||||
logger.info("=" * 80)
|
|
||||||
|
|
||||||
from src.integrations.feishu_longconn_service import get_feishu_longconn_service
|
from src.integrations.feishu_longconn_service import get_feishu_longconn_service
|
||||||
service = get_feishu_longconn_service()
|
service = get_feishu_longconn_service()
|
||||||
@@ -76,9 +73,6 @@ def check_database_connection():
|
|||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""主函数"""
|
"""主函数"""
|
||||||
print("=" * 60)
|
|
||||||
print("TSP智能助手 - 综合管理平台")
|
|
||||||
print("=" * 60)
|
|
||||||
start_time = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
start_time = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
||||||
print(f"启动时间: {start_time}")
|
print(f"启动时间: {start_time}")
|
||||||
print()
|
print()
|
||||||
@@ -91,9 +85,6 @@ def main():
|
|||||||
# 检查必要目录
|
# 检查必要目录
|
||||||
os.makedirs("logs", exist_ok=True)
|
os.makedirs("logs", exist_ok=True)
|
||||||
os.makedirs("data", exist_ok=True)
|
os.makedirs("data", exist_ok=True)
|
||||||
|
|
||||||
logger.info("正在启动TSP智能助手综合管理平台...")
|
|
||||||
|
|
||||||
# 检查数据库连接
|
# 检查数据库连接
|
||||||
if not check_database_connection():
|
if not check_database_connection():
|
||||||
logger.error("数据库连接失败,退出启动")
|
logger.error("数据库连接失败,退出启动")
|
||||||
|
|||||||
Reference in New Issue
Block a user