From 475ada22e7d64a04cbcad0576feca0c859b8d23c Mon Sep 17 00:00:00 2001 From: lanvent Date: Sun, 12 Mar 2023 22:49:07 +0800 Subject: [PATCH] catch thread exception --- bridge/bridge.py | 11 +++++++---- channel/wechat/wechat_channel.py | 13 ++++++++----- plugins/hello/hello.py | 1 - plugins/plugin_manager.py | 2 +- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/bridge/bridge.py b/bridge/bridge.py index e16d721..304046e 100644 --- a/bridge/bridge.py +++ b/bridge/bridge.py @@ -14,7 +14,7 @@ class Bridge(object): } self.bots={} - def getbot(self,typename): + def get_bot(self,typename): if self.bots.get(typename) is None: logger.info("create bot {} for {}".format(self.btype[typename],typename)) if typename == "text_to_voice": @@ -25,16 +25,19 @@ class Bridge(object): self.bots[typename] = bot_factory.create_bot(self.btype[typename]) return self.bots[typename] + def get_bot_type(self,typename): + return self.btype[typename] + # 以下所有函数需要得到一个reply字典,格式如下: # reply["type"] = "ERROR" / "TEXT" / "VOICE" / ... # reply["content"] = reply的内容 def fetch_reply_content(self, query, context): - return self.bots["chat"].reply(query, context) + return self.get_bot("chat").reply(query, context) def fetch_voice_to_text(self, voiceFile): - return self.bots["voice_to_text"].voiceToText(voiceFile) + return self.get_bot("voice_to_text").voiceToText(voiceFile) def fetch_text_to_voice(self, text): - return self.bots["text_to_voice"].textToVoice(text) + return self.get_bot("text_to_voice").textToVoice(text) diff --git a/channel/wechat/wechat_channel.py b/channel/wechat/wechat_channel.py index f436e48..73bb59a 100644 --- a/channel/wechat/wechat_channel.py +++ b/channel/wechat/wechat_channel.py @@ -19,7 +19,10 @@ import io thread_pool = ThreadPoolExecutor(max_workers=8) - +def thread_pool_callback(worker): + worker_exception = worker.exception() + if worker_exception: + logger.exception("Worker return exception: {}".format(worker_exception)) @itchat.msg_register(TEXT) def handler_single_msg(msg): @@ -69,7 +72,7 @@ class WechatChannel(Channel): context = {'isgroup': False, 'msg': msg, 'receiver': other_user_id} context['type'] = 'VOICE' context['session_id'] = other_user_id - thread_pool.submit(self.handle, context) + thread_pool.submit(self.handle, context).add_done_callback(thread_pool_callback) def handle_text(self, msg): logger.debug("[WX]receive text msg: " + json.dumps(msg, ensure_ascii=False)) @@ -96,7 +99,7 @@ class WechatChannel(Channel): context['type'] = 'TEXT' context['content'] = content - thread_pool.submit(self.handle, context) + thread_pool.submit(self.handle, context).add_done_callback(thread_pool_callback) def handle_group(self, msg): logger.debug("[WX]receive group msg: " + json.dumps(msg, ensure_ascii=False)) @@ -137,7 +140,7 @@ class WechatChannel(Channel): else: context['session_id'] = msg['ActualUserName'] - thread_pool.submit(self.handle, context) + thread_pool.submit(self.handle, context).add_done_callback(thread_pool_callback) # 统一的发送函数,每个Channel自行实现,根据reply的type字段发送不同类型的消息 def send(self, reply, receiver): @@ -208,7 +211,7 @@ class WechatChannel(Channel): reply_text = conf().get("single_chat_reply_prefix", "")+reply_text reply['content'] = reply_text elif reply['type'] == 'ERROR' or reply['type'] == 'INFO': - reply['content'] = reply['type']+": " + reply['content'] + reply['content'] = reply['type']+":\n" + reply['content'] elif reply['type'] == 'IMAGE_URL' or reply['type'] == 'VOICE' or reply['type'] == 'IMAGE': pass else: diff --git a/plugins/hello/hello.py b/plugins/hello/hello.py index c380b96..144906b 100644 --- a/plugins/hello/hello.py +++ b/plugins/hello/hello.py @@ -10,7 +10,6 @@ class Hello(Plugin): def __init__(self): super().__init__() self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context - # self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context logger.info("[Hello] inited") def on_handle_context(self, e_context: EventContext): diff --git a/plugins/plugin_manager.py b/plugins/plugin_manager.py index bf202f8..dc8e892 100644 --- a/plugins/plugin_manager.py +++ b/plugins/plugin_manager.py @@ -24,7 +24,7 @@ class PluginManager: plugincls.version = version plugincls.author = author plugincls.enabled = True - logger.info("Plugin %s registered" % name) + logger.info("Plugin %s_v%s registered" % (name, version)) return plugincls return wrapper