fix: 修复关闭报错
This commit is contained in:
2
bot.py
2
bot.py
@@ -9,6 +9,7 @@ import platform
|
||||
from dotenv import load_dotenv
|
||||
from src.common.logger import get_module_logger
|
||||
from src.main import MainSystem
|
||||
from src.plugins.message import global_api
|
||||
|
||||
logger = get_module_logger("main_bot")
|
||||
|
||||
@@ -252,6 +253,7 @@ if __name__ == "__main__":
|
||||
loop.run_until_complete(main_system.initialize())
|
||||
loop.run_until_complete(main_system.schedule_tasks())
|
||||
except KeyboardInterrupt:
|
||||
# loop.run_until_complete(global_api.stop())
|
||||
logger.warning("收到中断信号,正在优雅关闭...")
|
||||
loop.run_until_complete(graceful_shutdown())
|
||||
finally:
|
||||
|
||||
@@ -33,9 +33,6 @@ class MainSystem:
|
||||
"""初始化系统组件"""
|
||||
logger.debug(f"正在唤醒{global_config.BOT_NICKNAME}......")
|
||||
|
||||
# 启动API服务器(改为异步启动)
|
||||
self.api_task = asyncio.create_task(self.app.run())
|
||||
|
||||
# 其他初始化任务
|
||||
await asyncio.gather(
|
||||
self._init_components(), # 将原有的初始化代码移到这个新方法中
|
||||
@@ -91,6 +88,7 @@ class MainSystem:
|
||||
self.generate_schedule_task(),
|
||||
self.remove_recalled_message_task(),
|
||||
emoji_manager.start_periodic_check(interval_MINS=global_config.EMOJI_CHECK_INTERVAL),
|
||||
self.app.run(),
|
||||
]
|
||||
await asyncio.gather(*tasks)
|
||||
|
||||
@@ -143,7 +141,10 @@ class MainSystem:
|
||||
async def main():
|
||||
"""主函数"""
|
||||
system = MainSystem()
|
||||
await asyncio.gather(system.initialize(), system.schedule_tasks(), system.api_task)
|
||||
await asyncio.gather(
|
||||
system.initialize(),
|
||||
system.schedule_tasks(),
|
||||
)
|
||||
# await system.initialize()
|
||||
# await system.schedule_tasks()
|
||||
|
||||
|
||||
@@ -49,8 +49,11 @@ class BaseMessageAPI:
|
||||
"""异步方式运行服务器"""
|
||||
config = uvicorn.Config(self.app, host=self.host, port=self.port, loop="asyncio")
|
||||
self.server = uvicorn.Server(config)
|
||||
|
||||
await self.server.serve()
|
||||
try:
|
||||
await self.server.serve()
|
||||
except KeyboardInterrupt as e:
|
||||
await self.stop()
|
||||
raise KeyboardInterrupt from e
|
||||
|
||||
async def start_server(self):
|
||||
"""启动服务器的异步方法"""
|
||||
@@ -83,4 +86,4 @@ class BaseMessageAPI:
|
||||
loop.close()
|
||||
|
||||
|
||||
global_api = BaseMessageAPI(host=os.environ["HOST"], port=os.environ["PORT"])
|
||||
global_api = BaseMessageAPI(host=os.environ["HOST"], port=int(os.environ["PORT"]))
|
||||
|
||||
Reference in New Issue
Block a user