mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-05 03:39:59 +08:00
性能优化 (#2126)
* 性能优化 * 代码改进 * 优化浏览器代际切换逻辑 * 统一缓存与生命周期 * 添加aiomysql依赖 * 优化插件路径处理逻辑,简化条件判断;在虚拟环境包管理器中添加编码和错误处理参数以增强稳定性 * 🚨 auto fix by pre-commit hooks * 优化Windows下的关闭逻辑 * 代码优化 * bugfix:修复配置重载问题 * bugfix:修复插件加载启动竞态问题 * 收敛事件入口和权限上下文 * 优化 Windows launcher 关闭重启兜底 --------- Co-authored-by: HibiKier <775757368@qq.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
HibiKier
pre-commit-ci[bot]
parent
24c316cd2c
commit
5d92ccd3b0
@@ -1,3 +1,5 @@
|
||||
import contextlib
|
||||
|
||||
from nonebot.permission import SUPERUSER
|
||||
from nonebot.plugin import PluginMetadata
|
||||
from nonebot.rule import to_me
|
||||
@@ -11,8 +13,11 @@ from zhenxun.services.llm.config.providers import get_llm_config
|
||||
from zhenxun.services.llm.manager import clear_model_cache
|
||||
from zhenxun.services.log import logger
|
||||
from zhenxun.utils.enum import PluginType
|
||||
from zhenxun.utils.manager.priority_manager import PriorityLifecycle
|
||||
from zhenxun.utils.message import MessageUtils
|
||||
|
||||
AUTO_RELOAD_JOB_ID = "zhenxun.reload_setting.auto_reload"
|
||||
|
||||
__plugin_meta__ = PluginMetadata(
|
||||
name="重载配置",
|
||||
description="重新加载config.yaml",
|
||||
@@ -53,22 +58,75 @@ _matcher = on_alconna(
|
||||
)
|
||||
|
||||
|
||||
@_matcher.handle()
|
||||
async def _(session: EventSession, arparma: Arparma):
|
||||
def _get_auto_reload_interval() -> int:
|
||||
value = Config.get_config("reload_setting", "AUTO_RELOAD_TIME", 180)
|
||||
try:
|
||||
seconds = int(value)
|
||||
except (TypeError, ValueError):
|
||||
logger.warning(
|
||||
f"AUTO_RELOAD_TIME 配置无效: {value!r},已使用默认值 180 秒",
|
||||
"重载配置",
|
||||
)
|
||||
return 180
|
||||
if seconds <= 0:
|
||||
logger.warning(
|
||||
f"AUTO_RELOAD_TIME 配置小于等于 0: {seconds},已使用默认值 180 秒",
|
||||
"重载配置",
|
||||
)
|
||||
return 180
|
||||
return seconds
|
||||
|
||||
|
||||
def _reschedule_auto_reload_job() -> None:
|
||||
seconds = _get_auto_reload_interval()
|
||||
if scheduler.get_job(AUTO_RELOAD_JOB_ID):
|
||||
scheduler.reschedule_job(
|
||||
AUTO_RELOAD_JOB_ID,
|
||||
trigger="interval",
|
||||
seconds=seconds,
|
||||
)
|
||||
else:
|
||||
scheduler.add_job(
|
||||
_auto_reload_config,
|
||||
"interval",
|
||||
seconds=seconds,
|
||||
id=AUTO_RELOAD_JOB_ID,
|
||||
replace_existing=True,
|
||||
)
|
||||
logger.debug(f"自动重载配置任务间隔已设置为 {seconds} 秒", "重载配置")
|
||||
|
||||
|
||||
async def _reload_plugin_limit_config() -> None:
|
||||
from zhenxun.builtin_plugins.hooks.auth.auth_limit import LimitManager
|
||||
from zhenxun.builtin_plugins.init.manager import manager
|
||||
|
||||
manager.init()
|
||||
await manager.load_to_db()
|
||||
await LimitManager.update_limits()
|
||||
|
||||
|
||||
async def _reload_runtime_config() -> None:
|
||||
Config.reload()
|
||||
get_llm_config.cache_clear()
|
||||
clear_model_cache()
|
||||
await _reload_plugin_limit_config()
|
||||
with contextlib.suppress(Exception):
|
||||
_reschedule_auto_reload_job()
|
||||
|
||||
|
||||
@PriorityLifecycle.on_startup(priority=1)
|
||||
def _init_auto_reload_job() -> None:
|
||||
_reschedule_auto_reload_job()
|
||||
|
||||
|
||||
@_matcher.handle()
|
||||
async def _(session: EventSession, arparma: Arparma):
|
||||
await _reload_runtime_config()
|
||||
logger.debug("自动重载配置文件", arparma.header_result, session=session)
|
||||
await MessageUtils.build_message("重载完成!").send(reply_to=True)
|
||||
|
||||
|
||||
@scheduler.scheduled_job(
|
||||
"interval",
|
||||
seconds=Config.get_config("reload_setting", "AUTO_RELOAD_TIME", 180),
|
||||
)
|
||||
async def _():
|
||||
async def _auto_reload_config() -> None:
|
||||
if Config.get_config("reload_setting", "AUTO_RELOAD"):
|
||||
Config.reload()
|
||||
get_llm_config.cache_clear()
|
||||
clear_model_cache()
|
||||
await _reload_runtime_config()
|
||||
logger.debug("已自动重载配置文件...")
|
||||
|
||||
Reference in New Issue
Block a user