1
0
forked from bot/app

🐛 fix: Channel的接收者过滤器的问题,优化重启部分

This commit is contained in:
2024-07-31 02:28:25 +08:00
parent 0fb5b84392
commit ca34f9c2a1
21 changed files with 386 additions and 329 deletions

View File

@ -1,26 +1,17 @@
import asyncio
import multiprocessing
import time
from typing import Any, Coroutine, Optional
import asyncio
from typing import Any, Optional
from multiprocessing import freeze_support
import nonebot
import liteyuki
from liteyuki.plugin.load import load_plugin, load_plugins
from liteyuki.bot.lifespan import (LIFESPAN_FUNC, Lifespan)
from liteyuki.comm.channel import Channel
from liteyuki.core import IS_MAIN_PROCESS
from liteyuki.core.manager import ProcessManager
from liteyuki.core.spawn_process import mb_run, nb_run
from liteyuki.log import init_log, logger
from liteyuki.plugin import load_plugins
from liteyuki.utils import run_coroutine
from liteyuki.log import logger, init_log
from src.utils import (
adapter_manager,
driver_manager,
)
from liteyuki.bot.lifespan import (
Lifespan,
LIFESPAN_FUNC,
)
from liteyuki.core.spawn_process import nb_run, ProcessingManager
__all__ = [
"LiteyukiBot",
@ -28,19 +19,21 @@ __all__ = [
]
"""是否为主进程"""
IS_MAIN_PROCESS = multiprocessing.current_process().name == "MainProcess"
class LiteyukiBot:
def __init__(self, *args, **kwargs):
global _BOT_INSTANCE
_BOT_INSTANCE = self # 引用
if not IS_MAIN_PROCESS:
self.config: dict[str, Any] = kwargs
self.lifespan: Lifespan = Lifespan()
self.init(**self.config) # 初始化
else:
print("\033[34m" + r"""
self.config: dict[str, Any] = kwargs
self.init(**self.config) # 初始化
self.lifespan: Lifespan = Lifespan()
self.chan = Channel() # 进程通信通道
self.pm: Optional[ProcessManager] = None # 启动时实例化
print("\033[34m" + r"""
__ ______ ________ ________ __ __ __ __ __ __ ______
/ | / |/ |/ |/ \ / |/ | / |/ | / |/ |
$$ | $$$$$$/ $$$$$$$$/ $$$$$$$$/ $$ \ /$$/ $$ | $$ |$$ | /$$/ $$$$$$/
@ -52,62 +45,23 @@ $$ |/ $$ | $$ | $$ | $$ | $$ $$/ $$ | $$ |/ $$ |
$$$$$$$$/ $$$$$$/ $$/ $$$$$$$$/ $$/ $$$$$$/ $$/ $$/ $$$$$$/
""" + "\033[0m")
def run(self, *args, **kwargs):
if IS_MAIN_PROCESS:
self._run_nb_in_spawn_process(*args, **kwargs)
else:
# 子进程启动
load_plugins("liteyuki/plugins") # 加载轻雪插件
driver_manager.init(config=self.config)
adapter_manager.init(self.config)
adapter_manager.register()
nonebot.load_plugin("src.liteyuki_main")
run_coroutine(self.lifespan.after_start()) # 启动前
def run(self):
# load_plugins("liteyuki/plugins") # 加载轻雪插件
self.pm = ProcessManager(bot=self, chan=self.chan)
def _run_nb_in_spawn_process(self, *args, **kwargs):
"""
在新的进程中运行nonebot.run方法该函数在主进程中被调用
Args:
*args:
**kwargs:
self.pm.add_target("melobot", mb_run, **self.config)
self.pm.start("melobot")
Returns:
"""
if IS_MAIN_PROCESS:
timeout_limit: int = 20
should_exit = False
self.pm.add_target("nonebot", nb_run, **self.config)
self.pm.start("nonebot")
while not should_exit:
ctx = multiprocessing.get_context("spawn")
event = ctx.Event()
ProcessingManager.event = event
process = ctx.Process(
target=nb_run,
args=(event,) + args,
kwargs=kwargs,
)
process.start() # 启动进程
run_coroutine(self.lifespan.after_start()) # 启动前
while not should_exit:
if ProcessingManager.event.wait(1):
logger.info("Receive reboot event")
process.terminate()
process.join(timeout_limit)
if process.is_alive():
logger.warning(
f"Process {process.pid} is still alive after {timeout_limit} seconds, force kill it."
)
process.kill()
break
elif process.is_alive():
liteyuki.chan.send("轻雪进程正常运行", "sub")
continue
else:
should_exit = True
def restart(self):
def restart(self, name: Optional[str] = None):
"""
停止轻雪
Args:
name: 进程名称, 默认为None, 所有进程
Returns:
"""
@ -116,8 +70,11 @@ $$$$$$$$/ $$$$$$/ $$/ $$$$$$$$/ $$/ $$$$$$/ $$/ $$/ $$$$$$/
run_coroutine(self.lifespan.before_restart())
logger.debug("Running before_shutdown functions...")
run_coroutine(self.lifespan.before_shutdown())
ProcessingManager.restart()
if name:
self.chan.send(1, name)
else:
for name in self.pm.processes:
self.chan.send(1, name)
def init(self, *args, **kwargs):
"""
@ -127,11 +84,9 @@ $$$$$$$$/ $$$$$$/ $$/ $$$$$$$$/ $$/ $$$$$$/ $$/ $$/ $$$$$$/
"""
self.init_config()
self.init_logger()
if not IS_MAIN_PROCESS:
nonebot.init(**kwargs)
asyncio.run(self.lifespan.after_nonebot_init())
def init_logger(self):
# 修改nonebot的日志配置
init_log(config=self.config)
def init_config(self):
@ -225,4 +180,8 @@ def get_bot() -> Optional[LiteyukiBot]:
Returns:
LiteyukiBot: 当前的轻雪实例
"""
return _BOT_INSTANCE
if IS_MAIN_PROCESS:
return _BOT_INSTANCE
else:
# 从多进程上下文中获取
pass