perf: 完善启动时的Troubleshooting信息 #84

This commit is contained in:
Rock Chin
2023-01-04 12:01:26 +08:00
parent 6da11eded1
commit b318f6d4f0
2 changed files with 93 additions and 55 deletions

57
main.py
View File

@@ -5,6 +5,10 @@ import time
import logging
import sys
import mirai.exceptions
import websockets.exceptions
try:
import colorlog
except ImportError:
@@ -29,8 +33,13 @@ def init_db():
database.initialize_database()
known_exception_caught = False
def main(first_time_init=False):
global known_exception_caught
known_exception_caught = False
try:
# 导入config.py
assert os.path.exists('config.py')
@@ -82,19 +91,44 @@ def main(first_time_init=False):
first_time_init=first_time_init)
if first_time_init: # 不是热重载之后的启动,则不启动新的bot线程
qq_bot_thread = threading.Thread(target=qqbot.bot.run, args=(), daemon=True)
qq_bot_thread.start()
time.sleep(2)
def run_bot_wrapper():
global known_exception_caught
try:
qqbot.bot.run()
except TypeError as e:
if str(e).__contains__("argument 'debug'"):
logging.error("连接bot失败:{}, 请查看 https://github.com/RockChinQ/QChatGPT/issues/82".format(e))
known_exception_caught = True
elif str(e).__contains__("As of 3.10, the *loop*"):
logging.error("Websockets版本过低:{}, 请查看 https://github.com/RockChinQ/QChatGPT/issues/5".format(e))
known_exception_caught = True
except websockets.exceptions.InvalidStatus as e:
logging.error("mirai-api-http端口无法使用:{}, 请查看 https://github.com/RockChinQ/QChatGPT/issues/22".format(e))
known_exception_caught = True
except mirai.exceptions.NetworkError as e:
logging.error("连接mirai-api-http失败:{}, 请检查是否已按照文档启动mirai".format(e))
known_exception_caught = True
except Exception as e:
if str(e).__contains__("HTTP 404"):
logging.error("mirai-api-http端口无法使用:{}, 请查看 https://github.com/RockChinQ/QChatGPT/issues/22".format(e))
known_exception_caught = True
else:
logging.error("捕捉到未知异常:{}, 请前往 https://github.com/RockChinQ/issues 查找或提issue".format(e))
known_exception_caught = True
raise e
qq_bot_thread = threading.Thread(target=run_bot_wrapper, args=(), daemon=True)
qq_bot_thread.start()
finally:
time.sleep(10)
if first_time_init:
logging.info('程序启动完成,如长时间未显示 ”成功登录到账号xxxxx“ ,并且不回复消息,请查看 https://github.com/RockChinQ/QChatGPT/issues/37')
logging.info("如报错 \"TypeError: run() got an ... argument 'debug'\" ,"
"请查看 https://github.com/RockChinQ/QChatGPT/issues/82")
logging.info("如报错 \"TypeError: As of 3.10, the *loop* parameter ... it is no longer necessary\" ,"
"请查看 https://github.com/RockChinQ/QChatGPT/issues/5")
logging.info("如报错 \"server rejected WebSocket connection: HTTP 404\" ,"
"请查看 https://github.com/RockChinQ/QChatGPT/issues/22")
logging.info("其他异常请前往仓库issue搜索或提issue")
if not known_exception_caught:
logging.info('程序启动完成,如长时间未显示 ”成功登录到账号xxxxx“ ,并且不回复消息,请查看 '
'https://github.com/RockChinQ/QChatGPT/issues/37')
else:
sys.exit(1)
else:
logging.info('热重载完成')
@@ -123,6 +157,7 @@ def stop():
for session in pkg.openai.session.sessions:
logging.info('持久化session: %s', session)
pkg.openai.session.sessions[session].persistence()
pkg.utils.context.get_database_manager().close()
except Exception as e:
if not isinstance(e, KeyboardInterrupt):
raise e

View File

@@ -28,6 +28,9 @@ class DatabaseManager:
# self.conn.isolation_level = None
self.cursor = self.conn.cursor()
def close(self):
self.conn.close()
def execute(self, *args, **kwargs) -> Cursor:
# logging.debug('SQL: {}'.format(sql))
c = self.cursor.execute(*args, **kwargs)