mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-27 07:54:19 +00:00
perf: 完善启动时的Troubleshooting信息 #84
This commit is contained in:
@@ -5,6 +5,10 @@ import time
|
|||||||
|
|
||||||
import logging
|
import logging
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
import mirai.exceptions
|
||||||
|
import websockets.exceptions
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import colorlog
|
import colorlog
|
||||||
except ImportError:
|
except ImportError:
|
||||||
@@ -29,74 +33,104 @@ def init_db():
|
|||||||
|
|
||||||
database.initialize_database()
|
database.initialize_database()
|
||||||
|
|
||||||
|
known_exception_caught = False
|
||||||
|
|
||||||
def main(first_time_init=False):
|
def main(first_time_init=False):
|
||||||
# 导入config.py
|
global known_exception_caught
|
||||||
assert os.path.exists('config.py')
|
|
||||||
|
|
||||||
# 检查是否设置了管理员
|
known_exception_caught = False
|
||||||
import config
|
try:
|
||||||
if not (hasattr(config, 'admin_qq') and config.admin_qq != 0):
|
# 导入config.py
|
||||||
logging.warning("未设置管理员QQ,管理员权限指令及运行告警将无法使用,如需设置请修改config.py中的admin_qq字段")
|
assert os.path.exists('config.py')
|
||||||
|
|
||||||
import pkg.utils.context
|
# 检查是否设置了管理员
|
||||||
if pkg.utils.context.context['logger_handler'] is not None:
|
import config
|
||||||
logging.getLogger().removeHandler(pkg.utils.context.context['logger_handler'])
|
if not (hasattr(config, 'admin_qq') and config.admin_qq != 0):
|
||||||
|
logging.warning("未设置管理员QQ,管理员权限指令及运行告警将无法使用,如需设置请修改config.py中的admin_qq字段")
|
||||||
|
|
||||||
logging.basicConfig(level=config.logging_level, # 设置日志输出格式
|
import pkg.utils.context
|
||||||
filename='qchatgpt.log', # log日志输出的文件位置和文件名
|
if pkg.utils.context.context['logger_handler'] is not None:
|
||||||
format="[%(asctime)s.%(msecs)03d] %(filename)s (%(lineno)d) - [%(levelname)s] : %(message)s",
|
logging.getLogger().removeHandler(pkg.utils.context.context['logger_handler'])
|
||||||
# 日志输出的格式
|
|
||||||
# -8表示占位符,让输出左对齐,输出长度都为8位
|
|
||||||
datefmt="%Y-%m-%d %H:%M:%S" # 时间输出的格式
|
|
||||||
)
|
|
||||||
sh = logging.StreamHandler()
|
|
||||||
sh.setLevel(config.logging_level)
|
|
||||||
sh.setFormatter(colorlog.ColoredFormatter(
|
|
||||||
fmt="%(log_color)s[%(asctime)s.%(msecs)03d] %(filename)s (%(lineno)d) - [%(levelname)s] : "
|
|
||||||
"%(message)s",
|
|
||||||
datefmt="%Y-%m-%d %H:%M:%S",
|
|
||||||
log_colors=log_colors_config
|
|
||||||
))
|
|
||||||
logging.getLogger().addHandler(sh)
|
|
||||||
|
|
||||||
import pkg.openai.manager
|
logging.basicConfig(level=config.logging_level, # 设置日志输出格式
|
||||||
import pkg.database.manager
|
filename='qchatgpt.log', # log日志输出的文件位置和文件名
|
||||||
import pkg.openai.session
|
format="[%(asctime)s.%(msecs)03d] %(filename)s (%(lineno)d) - [%(levelname)s] : %(message)s",
|
||||||
import pkg.qqbot.manager
|
# 日志输出的格式
|
||||||
|
# -8表示占位符,让输出左对齐,输出长度都为8位
|
||||||
|
datefmt="%Y-%m-%d %H:%M:%S" # 时间输出的格式
|
||||||
|
)
|
||||||
|
sh = logging.StreamHandler()
|
||||||
|
sh.setLevel(config.logging_level)
|
||||||
|
sh.setFormatter(colorlog.ColoredFormatter(
|
||||||
|
fmt="%(log_color)s[%(asctime)s.%(msecs)03d] %(filename)s (%(lineno)d) - [%(levelname)s] : "
|
||||||
|
"%(message)s",
|
||||||
|
datefmt="%Y-%m-%d %H:%M:%S",
|
||||||
|
log_colors=log_colors_config
|
||||||
|
))
|
||||||
|
logging.getLogger().addHandler(sh)
|
||||||
|
|
||||||
pkg.utils.context.context['logger_handler'] = sh
|
import pkg.openai.manager
|
||||||
# 主启动流程
|
import pkg.database.manager
|
||||||
database = pkg.database.manager.DatabaseManager()
|
import pkg.openai.session
|
||||||
|
import pkg.qqbot.manager
|
||||||
|
|
||||||
database.initialize_database()
|
pkg.utils.context.context['logger_handler'] = sh
|
||||||
|
# 主启动流程
|
||||||
|
database = pkg.database.manager.DatabaseManager()
|
||||||
|
|
||||||
openai_interact = pkg.openai.manager.OpenAIInteract(config.openai_config['api_key'])
|
database.initialize_database()
|
||||||
|
|
||||||
# 加载所有未超时的session
|
openai_interact = pkg.openai.manager.OpenAIInteract(config.openai_config['api_key'])
|
||||||
pkg.openai.session.load_sessions()
|
|
||||||
|
|
||||||
# 初始化qq机器人
|
# 加载所有未超时的session
|
||||||
qqbot = pkg.qqbot.manager.QQBotManager(mirai_http_api_config=config.mirai_http_api_config,
|
pkg.openai.session.load_sessions()
|
||||||
timeout=config.process_message_timeout, retry=config.retry_times,
|
|
||||||
first_time_init=first_time_init)
|
|
||||||
|
|
||||||
if first_time_init: # 不是热重载之后的启动,则不启动新的bot线程
|
# 初始化qq机器人
|
||||||
qq_bot_thread = threading.Thread(target=qqbot.bot.run, args=(), daemon=True)
|
qqbot = pkg.qqbot.manager.QQBotManager(mirai_http_api_config=config.mirai_http_api_config,
|
||||||
qq_bot_thread.start()
|
timeout=config.process_message_timeout, retry=config.retry_times,
|
||||||
|
first_time_init=first_time_init)
|
||||||
|
|
||||||
time.sleep(2)
|
if first_time_init: # 不是热重载之后的启动,则不启动新的bot线程
|
||||||
if first_time_init:
|
|
||||||
logging.info('程序启动完成,如长时间未显示 ”成功登录到账号xxxxx“ ,并且不回复消息,请查看 https://github.com/RockChinQ/QChatGPT/issues/37')
|
def run_bot_wrapper():
|
||||||
logging.info("如报错 \"TypeError: run() got an ... argument 'debug'\" ,"
|
global known_exception_caught
|
||||||
"请查看 https://github.com/RockChinQ/QChatGPT/issues/82")
|
try:
|
||||||
logging.info("如报错 \"TypeError: As of 3.10, the *loop* parameter ... it is no longer necessary\" ,"
|
qqbot.bot.run()
|
||||||
"请查看 https://github.com/RockChinQ/QChatGPT/issues/5")
|
except TypeError as e:
|
||||||
logging.info("如报错 \"server rejected WebSocket connection: HTTP 404\" ,"
|
if str(e).__contains__("argument 'debug'"):
|
||||||
"请查看 https://github.com/RockChinQ/QChatGPT/issues/22")
|
logging.error("连接bot失败:{}, 请查看 https://github.com/RockChinQ/QChatGPT/issues/82".format(e))
|
||||||
logging.info("其他异常请前往仓库issue搜索或提issue")
|
known_exception_caught = True
|
||||||
else:
|
elif str(e).__contains__("As of 3.10, the *loop*"):
|
||||||
logging.info('热重载完成')
|
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:
|
||||||
|
if not known_exception_caught:
|
||||||
|
logging.info('程序启动完成,如长时间未显示 ”成功登录到账号xxxxx“ ,并且不回复消息,请查看 '
|
||||||
|
'https://github.com/RockChinQ/QChatGPT/issues/37')
|
||||||
|
else:
|
||||||
|
sys.exit(1)
|
||||||
|
else:
|
||||||
|
logging.info('热重载完成')
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
@@ -123,6 +157,7 @@ def stop():
|
|||||||
for session in pkg.openai.session.sessions:
|
for session in pkg.openai.session.sessions:
|
||||||
logging.info('持久化session: %s', session)
|
logging.info('持久化session: %s', session)
|
||||||
pkg.openai.session.sessions[session].persistence()
|
pkg.openai.session.sessions[session].persistence()
|
||||||
|
pkg.utils.context.get_database_manager().close()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if not isinstance(e, KeyboardInterrupt):
|
if not isinstance(e, KeyboardInterrupt):
|
||||||
raise e
|
raise e
|
||||||
|
|||||||
@@ -28,6 +28,9 @@ class DatabaseManager:
|
|||||||
# self.conn.isolation_level = None
|
# self.conn.isolation_level = None
|
||||||
self.cursor = self.conn.cursor()
|
self.cursor = self.conn.cursor()
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
self.conn.close()
|
||||||
|
|
||||||
def execute(self, *args, **kwargs) -> Cursor:
|
def execute(self, *args, **kwargs) -> Cursor:
|
||||||
# logging.debug('SQL: {}'.format(sql))
|
# logging.debug('SQL: {}'.format(sql))
|
||||||
c = self.cursor.execute(*args, **kwargs)
|
c = self.cursor.execute(*args, **kwargs)
|
||||||
|
|||||||
Reference in New Issue
Block a user