소스 검색

catch thread exception

lanvent 3 년 전
부모
커밋
475ada22e7
4개의 변경된 파일16개의 추가작업 그리고 11개의 파일을 삭제
  1. 7 4
      bridge/bridge.py
  2. 8 5
      channel/wechat/wechat_channel.py
  3. 0 1
      plugins/hello/hello.py
  4. 1 1
      plugins/plugin_manager.py

+ 7 - 4
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)
 

+ 8 - 5
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:

+ 0 - 1
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):

+ 1 - 1
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