🐛 修复生命周期钩子函数的问题
This commit is contained in:
@ -1,3 +1,4 @@
|
||||
import threading
|
||||
import time
|
||||
import asyncio
|
||||
from typing import Any, Optional
|
||||
@ -29,7 +30,10 @@ class LiteyukiBot:
|
||||
|
||||
self.lifespan: Lifespan = Lifespan()
|
||||
self.chan = Channel() # 进程通信通道
|
||||
self.pm: Optional[ProcessManager] = None # 启动时实例化
|
||||
self.pm: ProcessManager = ProcessManager(bot=self, chan=self.chan)
|
||||
self.loop = asyncio.new_event_loop()
|
||||
asyncio.set_event_loop(self.loop)
|
||||
self.loop_thread = threading.Thread(target=self.loop.run_forever, daemon=True)
|
||||
|
||||
print("\033[34m" + r"""
|
||||
__ ______ ________ ________ __ __ __ __ __ __ ______
|
||||
@ -44,8 +48,10 @@ $$$$$$$$/ $$$$$$/ $$/ $$$$$$$$/ $$/ $$$$$$/ $$/ $$/ $$$$$$/
|
||||
""" + "\033[0m")
|
||||
|
||||
def run(self):
|
||||
# load_plugins("liteyuki/plugins") # 加载轻雪插件
|
||||
self.pm = ProcessManager(bot=self, chan=self.chan)
|
||||
load_plugins("liteyuki/plugins") # 加载轻雪插件
|
||||
|
||||
self.loop_thread.start() # 启动事件循环
|
||||
asyncio.run(self.lifespan.before_start()) # 启动前钩子
|
||||
|
||||
self.pm.add_target("nonebot", nb_run, **self.config)
|
||||
self.pm.start("nonebot")
|
||||
@ -53,7 +59,7 @@ $$$$$$$$/ $$$$$$/ $$/ $$$$$$$$/ $$/ $$$$$$/ $$/ $$/ $$$$$$/
|
||||
self.pm.add_target("melobot", mb_run, **self.config)
|
||||
self.pm.start("melobot")
|
||||
|
||||
run_coroutine(self.lifespan.after_start()) # 启动前
|
||||
asyncio.run(self.lifespan.after_start()) # 启动后钩子
|
||||
|
||||
def restart(self, name: Optional[str] = None):
|
||||
"""
|
||||
@ -64,14 +70,14 @@ $$$$$$$$/ $$$$$$/ $$/ $$$$$$$$/ $$/ $$$$$$/ $$/ $$/ $$$$$$/
|
||||
|
||||
"""
|
||||
logger.info("Stopping LiteyukiBot...")
|
||||
logger.debug("Running before_restart functions...")
|
||||
run_coroutine(self.lifespan.before_restart())
|
||||
logger.debug("Running before_shutdown functions...")
|
||||
run_coroutine(self.lifespan.before_shutdown())
|
||||
|
||||
self.loop.create_task(self.lifespan.before_shutdown()) # 重启前钩子
|
||||
self.loop.create_task(self.lifespan.before_shutdown()) # 停止前钩子
|
||||
|
||||
if name:
|
||||
self.chan.send(1, name)
|
||||
else:
|
||||
for name in self.pm.processes:
|
||||
for name in self.pm.targets:
|
||||
self.chan.send(1, name)
|
||||
|
||||
def init(self, *args, **kwargs):
|
||||
@ -114,7 +120,7 @@ $$$$$$$$/ $$$$$$/ $$/ $$$$$$$$/ $$/ $$$$$$/ $$/ $$/ $$$$$$/
|
||||
|
||||
def on_before_shutdown(self, func: LIFESPAN_FUNC):
|
||||
"""
|
||||
注册停止前的函数
|
||||
注册停止前的函数,为子进程停止时调用
|
||||
Args:
|
||||
func:
|
||||
|
||||
@ -136,7 +142,7 @@ $$$$$$$$/ $$$$$$/ $$/ $$$$$$$$/ $$/ $$$$$$/ $$/ $$/ $$$$$$/
|
||||
|
||||
def on_before_restart(self, func: LIFESPAN_FUNC):
|
||||
"""
|
||||
注册重启前的函数
|
||||
注册重启前的函数,为子进程重启时调用
|
||||
Args:
|
||||
func:
|
||||
|
||||
|
@ -10,6 +10,7 @@ Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved
|
||||
"""
|
||||
from typing import Any, Awaitable, Callable, TypeAlias
|
||||
|
||||
from liteyuki.log import logger
|
||||
from liteyuki.utils import is_coroutine_callable
|
||||
|
||||
SYNC_LIFESPAN_FUNC: TypeAlias = Callable[[], Any]
|
||||
@ -135,6 +136,7 @@ class Lifespan:
|
||||
启动前
|
||||
Returns:
|
||||
"""
|
||||
logger.debug("Running before_start functions")
|
||||
await self._run_funcs(self._before_start_funcs)
|
||||
|
||||
async def after_start(self) -> None:
|
||||
@ -142,6 +144,7 @@ class Lifespan:
|
||||
启动后
|
||||
Returns:
|
||||
"""
|
||||
logger.debug("Running after_start functions")
|
||||
await self._run_funcs(self._after_start_funcs)
|
||||
|
||||
async def before_shutdown(self) -> None:
|
||||
@ -149,6 +152,7 @@ class Lifespan:
|
||||
停止前
|
||||
Returns:
|
||||
"""
|
||||
logger.debug("Running before_shutdown functions")
|
||||
await self._run_funcs(self._before_shutdown_funcs)
|
||||
|
||||
async def after_shutdown(self) -> None:
|
||||
@ -156,6 +160,7 @@ class Lifespan:
|
||||
停止后
|
||||
Returns:
|
||||
"""
|
||||
logger.debug("Running after_shutdown functions")
|
||||
await self._run_funcs(self._after_shutdown_funcs)
|
||||
|
||||
async def before_restart(self) -> None:
|
||||
@ -163,6 +168,7 @@ class Lifespan:
|
||||
重启前
|
||||
Returns:
|
||||
"""
|
||||
logger.debug("Running before_restart functions")
|
||||
await self._run_funcs(self._before_restart_funcs)
|
||||
|
||||
async def after_restart(self) -> None:
|
||||
@ -171,6 +177,7 @@ class Lifespan:
|
||||
Returns:
|
||||
|
||||
"""
|
||||
logger.debug("Running after_restart functions")
|
||||
await self._run_funcs(self._after_restart_funcs)
|
||||
|
||||
async def after_nonebot_init(self) -> None:
|
||||
@ -178,4 +185,5 @@ class Lifespan:
|
||||
NoneBot 初始化后
|
||||
Returns:
|
||||
"""
|
||||
logger.debug("Running after_nonebot_init functions")
|
||||
await self._run_funcs(self._after_nonebot_init_funcs)
|
||||
|
Reference in New Issue
Block a user