mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-06 04:10:02 +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
@@ -34,6 +34,31 @@ run_time = time.time()
|
||||
|
||||
ws_router = APIRouter()
|
||||
router = APIRouter(prefix="/main")
|
||||
_SYSTEM_STATUS_CONNECTIONS: set[WebSocket] = set()
|
||||
_SYSTEM_STATUS_STOPPING = False
|
||||
|
||||
|
||||
async def _close_system_status_websocket(websocket: WebSocket) -> None:
|
||||
with contextlib.suppress(Exception):
|
||||
if websocket.client_state == WebSocketState.CONNECTED:
|
||||
await asyncio.wait_for(
|
||||
websocket.close(code=1001, reason="server shutdown"),
|
||||
timeout=2,
|
||||
)
|
||||
|
||||
|
||||
@driver.on_shutdown
|
||||
async def _close_system_status_websockets() -> None:
|
||||
global _SYSTEM_STATUS_STOPPING
|
||||
_SYSTEM_STATUS_STOPPING = True
|
||||
websockets = list(_SYSTEM_STATUS_CONNECTIONS)
|
||||
if not websockets:
|
||||
return
|
||||
await asyncio.gather(
|
||||
*(_close_system_status_websocket(websocket) for websocket in websockets),
|
||||
return_exceptions=True,
|
||||
)
|
||||
_SYSTEM_STATUS_CONNECTIONS.clear()
|
||||
|
||||
|
||||
@router.get(
|
||||
@@ -243,11 +268,39 @@ async def _(param: BotManageUpdateParam):
|
||||
@ws_router.websocket("/system_status")
|
||||
async def system_logs_realtime(websocket: WebSocket, sleep: int = 5):
|
||||
await websocket.accept()
|
||||
_SYSTEM_STATUS_CONNECTIONS.add(websocket)
|
||||
logger.debug("ws system_status is connect")
|
||||
with contextlib.suppress(
|
||||
WebSocketDisconnect, ConnectionClosedError, ConnectionClosedOK
|
||||
):
|
||||
while websocket.client_state == WebSocketState.CONNECTED:
|
||||
|
||||
disconnect_event = asyncio.Event()
|
||||
|
||||
async def _watch_disconnect() -> None:
|
||||
try:
|
||||
while websocket.client_state == WebSocketState.CONNECTED:
|
||||
await websocket.receive()
|
||||
except (WebSocketDisconnect, ConnectionClosedError, ConnectionClosedOK):
|
||||
pass
|
||||
except Exception as e:
|
||||
logger.debug(f"ws system_status receive stopped: {type(e).__name__}")
|
||||
finally:
|
||||
disconnect_event.set()
|
||||
|
||||
receive_task = asyncio.create_task(_watch_disconnect())
|
||||
try:
|
||||
while (
|
||||
websocket.client_state == WebSocketState.CONNECTED
|
||||
and not _SYSTEM_STATUS_STOPPING
|
||||
):
|
||||
system_status = await get_system_status()
|
||||
await websocket.send_text(system_status.json())
|
||||
await asyncio.sleep(sleep)
|
||||
await asyncio.wait_for(websocket.send_text(system_status.json()), timeout=5)
|
||||
try:
|
||||
await asyncio.wait_for(disconnect_event.wait(), timeout=max(sleep, 1))
|
||||
except TimeoutError:
|
||||
pass
|
||||
except (WebSocketDisconnect, ConnectionClosedError, ConnectionClosedOK):
|
||||
pass
|
||||
finally:
|
||||
_SYSTEM_STATUS_CONNECTIONS.discard(websocket)
|
||||
receive_task.cancel()
|
||||
with contextlib.suppress(asyncio.CancelledError):
|
||||
await receive_task
|
||||
await _close_system_status_websocket(websocket)
|
||||
|
||||
@@ -52,34 +52,20 @@ async def _(
|
||||
async def _() -> Result[PluginCount]:
|
||||
try:
|
||||
plugin_count = PluginCount()
|
||||
plugin_count.normal = len(
|
||||
await DbPluginInfo.get_plugins(
|
||||
plugin_type=PluginType.NORMAL,
|
||||
load_status=True,
|
||||
filter_parent=False,
|
||||
)
|
||||
)
|
||||
plugin_count.admin = len(
|
||||
await DbPluginInfo.get_plugins(
|
||||
plugin_type__in=[PluginType.ADMIN, PluginType.SUPER_AND_ADMIN],
|
||||
load_status=True,
|
||||
filter_parent=False,
|
||||
)
|
||||
)
|
||||
plugin_count.superuser = len(
|
||||
await DbPluginInfo.get_plugins(
|
||||
plugin_type__in=[PluginType.SUPERUSER, PluginType.SUPER_AND_ADMIN],
|
||||
load_status=True,
|
||||
filter_parent=False,
|
||||
)
|
||||
)
|
||||
plugin_count.other = len(
|
||||
await DbPluginInfo.get_plugins(
|
||||
plugin_type__in=[PluginType.HIDDEN, PluginType.DEPENDANT],
|
||||
load_status=True,
|
||||
filter_parent=False,
|
||||
)
|
||||
plugins = await DbPluginInfo.get_plugins(
|
||||
load_status=True,
|
||||
filter_parent=False,
|
||||
)
|
||||
for plugin in plugins:
|
||||
plugin_type = plugin.plugin_type
|
||||
if plugin_type == PluginType.NORMAL:
|
||||
plugin_count.normal += 1
|
||||
if plugin_type in {PluginType.ADMIN, PluginType.SUPER_AND_ADMIN}:
|
||||
plugin_count.admin += 1
|
||||
if plugin_type in {PluginType.SUPERUSER, PluginType.SUPER_AND_ADMIN}:
|
||||
plugin_count.superuser += 1
|
||||
if plugin_type in {PluginType.HIDDEN, PluginType.DEPENDANT}:
|
||||
plugin_count.other += 1
|
||||
return Result.ok(plugin_count, "拿到信息啦!")
|
||||
except Exception as e:
|
||||
logger.error(f"{router.prefix}/get_plugin_count 调用错误", "WebUi", e=e)
|
||||
|
||||
@@ -111,10 +111,19 @@ class ApiDataSource:
|
||||
other_update_fields = set()
|
||||
updated_count = 0
|
||||
errors = []
|
||||
modules = [item.module for item in params.updates]
|
||||
plugin_records = await DbPluginInfo.get_plugins(
|
||||
module__in=modules,
|
||||
load_status=None,
|
||||
filter_parent=False,
|
||||
)
|
||||
plugin_map = {plugin.module: plugin for plugin in plugin_records}
|
||||
|
||||
for item in params.updates:
|
||||
try:
|
||||
db_plugin = await DbPluginInfo.get(module=item.module)
|
||||
db_plugin = plugin_map.get(item.module)
|
||||
if db_plugin is None:
|
||||
raise DoesNotExist()
|
||||
plugin_changed_other = False
|
||||
plugin_changed_block = False
|
||||
|
||||
|
||||
Reference in New Issue
Block a user