mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-09-28 16:20:56 +08:00
* bugfix:修复内存泄露和信号量饥饿问题 * 修复图片渲染按高度截断问题 * 优化图片渲染速度 * 权限检查去掉无效引用代码 * 添加uv支持 * 🚨 auto fix by pre-commit hooks * bugfix:修改gitignore换行 * bugfix:修复测试没有新生成uv.lock * 修复导入错误 * bugfix:移除重复调用 * 🚨 auto fix by pre-commit hooks * 清理残余poetry引用 * 更新uv安装方式 * 修复阿里云获取问题 * 增加资源下载提示 * 🚨 auto fix by pre-commit hooks * 修改资源下载为流式 * 🚨 auto fix by pre-commit hooks * 提高启动速度 * 移除bot.py支持 * 🚨 auto fix by pre-commit hooks * 优化win脚本逻辑 * 🚨 auto fix by pre-commit hooks * 清理残余无效逻辑 * 代码改进 * 🚨 auto fix by pre-commit hooks * 增加数据库迁移存在性检查 * 🚨 auto fix by pre-commit hooks * chore(test): 添加pytest超时控制和优雅关闭机制 - 在GitHub Actions工作流中添加作业级和步骤级超时限制,防止测试无限期挂起 - 添加pytest-timeout依赖并配置全局超时为120秒 - 在send_queue服务添加关闭钩子,确保worker任务正确取消 - 在priority_manager添加on_shutdown钩子,支持优先级生命周期的关闭阶段 * chore(lint): 禁用超长行的lint警告 * Modify restart logic for Windows platform * 🚨 auto fix by pre-commit hooks * bugfix:修复sys导入问题 * 清理无效结构 * bugfix:修复路径问题 * bugfix:修复shell语法传递给git导致资源获取失败问题 * 优化关闭显示 * bugfix:修复路径问题 * bugfix:增加路径安全 * bugfix:修复orm绕过问题 * 放宽numpy版本限制 * 修改重启方案 * bugfix:修复循环导入 * 优化逻辑 * Enhance disconnect function with error handling Added error handling for disconnect function and imported ConfigurationError. * Implement emergency restart mechanism Added emergency restart mechanism using atexit to ensure process restart even on severe exceptions during shutdown. * 🚨 auto fix by pre-commit hooks * 重启行为归一化 * 修复测试检测问题 * bugfix:修复测试侧类型报错问题 * 引入launcher机制 * 移除重启测试 * 收紧缓存调用路径 * 类型注解收敛 * 优化浏览器回收行为 * 优化浏览器渲染 * bugfix:解决重复关闭浏览器问题 --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: ManyManyTomato <93612024+ATTomatoo@users.noreply.github.com> Co-authored-by: AkashiCoin <l1040186796@gmail.com>
191 lines
6.0 KiB
Python
191 lines
6.0 KiB
Python
from abc import ABC, abstractmethod
|
|
from typing import Any, cast
|
|
|
|
from zhenxun.models.group_console import GroupConsole
|
|
from zhenxun.models.plugin_info import PluginInfo
|
|
from zhenxun.models.task_info import TaskInfo
|
|
from zhenxun.services.cache import CacheRoot
|
|
from zhenxun.services.cache.runtime_cache import (
|
|
PluginInfoMemoryCache,
|
|
TaskInfoMemoryCache,
|
|
)
|
|
from zhenxun.utils.enum import BlockType, CacheType, PluginType
|
|
|
|
|
|
class SwitchStrategy(ABC):
|
|
"""插件与被动技能切换策略基类"""
|
|
|
|
@property
|
|
@abstractmethod
|
|
def entity_type_name(self) -> str:
|
|
pass
|
|
|
|
@property
|
|
@abstractmethod
|
|
def norm_field(self) -> str:
|
|
"""普通的群组禁用字段名"""
|
|
pass
|
|
|
|
@property
|
|
@abstractmethod
|
|
def su_field(self) -> str:
|
|
"""超级用户群组禁用字段名"""
|
|
pass
|
|
|
|
@abstractmethod
|
|
async def get_entity(self, name: str) -> Any:
|
|
"""通过名称获取实体信息"""
|
|
pass
|
|
|
|
@abstractmethod
|
|
async def check_block_status(self, group_id: str, module: str) -> tuple[bool, bool]:
|
|
"""检查目标群组的禁用状态,返回 (is_su_blocked, is_norm_blocked)"""
|
|
pass
|
|
|
|
@abstractmethod
|
|
async def get_all_modules(self) -> list[str]:
|
|
"""获取所有模块的名称列表"""
|
|
pass
|
|
|
|
@abstractmethod
|
|
async def set_default_status(self, entity: Any, status: bool) -> None:
|
|
"""设置单个实体的进群默认状态"""
|
|
pass
|
|
|
|
@abstractmethod
|
|
async def set_global_status(
|
|
self, entity: Any, status: bool, block_type: BlockType | None = None
|
|
) -> None:
|
|
"""设置单个实体的全局状态"""
|
|
pass
|
|
|
|
@abstractmethod
|
|
async def set_all_default_status(self, status: bool) -> None:
|
|
"""设置所有实体的进群默认状态"""
|
|
pass
|
|
|
|
@abstractmethod
|
|
async def set_all_global_status(self, status: bool) -> None:
|
|
"""设置所有实体的全局状态"""
|
|
pass
|
|
|
|
@abstractmethod
|
|
async def refresh_cache(self) -> None:
|
|
"""刷新相关的内存缓存"""
|
|
pass
|
|
|
|
|
|
class PluginStrategy(SwitchStrategy):
|
|
@property
|
|
def entity_type_name(self) -> str:
|
|
return "功能"
|
|
|
|
@property
|
|
def norm_field(self) -> str:
|
|
return "block_plugin"
|
|
|
|
@property
|
|
def su_field(self) -> str:
|
|
return "superuser_block_plugin"
|
|
|
|
async def get_entity(self, name: str) -> Any:
|
|
if name.isdigit():
|
|
return await PluginInfo.get_or_none(id=int(name))
|
|
return await PluginInfo.get_or_none(
|
|
name=name, load_status=True, plugin_type__not=PluginType.PARENT
|
|
)
|
|
|
|
async def check_block_status(self, group_id: str, module: str) -> tuple[bool, bool]:
|
|
is_su_blocked = await GroupConsole.is_superuser_block_plugin(group_id, module)
|
|
is_norm_blocked = await GroupConsole.is_normal_block_plugin(group_id, module)
|
|
return is_su_blocked, is_norm_blocked
|
|
|
|
async def get_all_modules(self) -> list[str]:
|
|
return cast(
|
|
list[str],
|
|
await PluginInfo.get_plugins_values_list(
|
|
"module",
|
|
load_status=None,
|
|
filter_parent=False,
|
|
plugin_type=PluginType.NORMAL,
|
|
),
|
|
)
|
|
|
|
async def set_default_status(self, entity: PluginInfo, status: bool) -> None:
|
|
entity.default_status = status
|
|
await entity.save(update_fields=["default_status"])
|
|
|
|
async def set_global_status(
|
|
self, entity: PluginInfo, status: bool, block_type: BlockType | None = None
|
|
) -> None:
|
|
entity.block_type = block_type
|
|
entity.status = not bool(block_type)
|
|
await entity.save(update_fields=["status", "block_type"])
|
|
|
|
async def set_all_default_status(self, status: bool) -> None:
|
|
await PluginInfo.filter(plugin_type=PluginType.NORMAL).update(
|
|
default_status=status
|
|
)
|
|
await self.refresh_cache()
|
|
|
|
async def set_all_global_status(self, status: bool) -> None:
|
|
await PluginInfo.filter(plugin_type=PluginType.NORMAL).update(
|
|
status=status, block_type=None if status else BlockType.ALL
|
|
)
|
|
await self.refresh_cache()
|
|
|
|
async def refresh_cache(self) -> None:
|
|
await CacheRoot.invalidate_cache(CacheType.PLUGINS)
|
|
await PluginInfoMemoryCache.refresh()
|
|
|
|
|
|
class TaskStrategy(SwitchStrategy):
|
|
@property
|
|
def entity_type_name(self) -> str:
|
|
return "被动"
|
|
|
|
@property
|
|
def norm_field(self) -> str:
|
|
return "block_task"
|
|
|
|
@property
|
|
def su_field(self) -> str:
|
|
return "superuser_block_task"
|
|
|
|
async def get_entity(self, name: str) -> Any:
|
|
return await TaskInfo.get_or_none(name=name)
|
|
|
|
async def check_block_status(self, group_id: str, module: str) -> tuple[bool, bool]:
|
|
is_su_blocked = await GroupConsole.is_superuser_block_task(group_id, module)
|
|
is_norm_blocked = await GroupConsole.is_block_task(group_id, module)
|
|
return is_su_blocked, is_norm_blocked
|
|
|
|
async def get_all_modules(self) -> list[str]:
|
|
return await TaskInfo.get_modules(load_status=None)
|
|
|
|
async def set_default_status(self, entity: TaskInfo, status: bool) -> None:
|
|
entity.default_status = status
|
|
await entity.save(update_fields=["default_status"])
|
|
|
|
async def set_global_status(
|
|
self, entity: TaskInfo, status: bool, block_type: BlockType | None = None
|
|
) -> None:
|
|
entity.status = status
|
|
await entity.save(update_fields=["status"])
|
|
|
|
async def set_all_default_status(self, status: bool) -> None:
|
|
await TaskInfo.all().update(default_status=status)
|
|
await self.refresh_cache()
|
|
|
|
async def set_all_global_status(self, status: bool) -> None:
|
|
await TaskInfo.all().update(status=status)
|
|
await self.refresh_cache()
|
|
|
|
async def refresh_cache(self) -> None:
|
|
await TaskInfoMemoryCache.refresh()
|
|
|
|
|
|
def get_strategy(is_task: bool) -> SwitchStrategy:
|
|
"""工厂方法:获取对应的处理策略"""
|
|
return TaskStrategy() if is_task else PluginStrategy()
|