mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-04 19:29:59 +08:00
✨ 资源下载分离 (#1814)
* 🚀 资源文件单独下载,分离被动任务初始化 * 🔥 移除resources文件夹 * 🚀 优化版本更新逻辑,增加资源下载异常处理 * 🚀 增加资源下载失败异常处理及提示信息 * 🚀 增加资源下载选项及异常处理优化 * 🚀 移除空ID检查及资源更新条件处理 * ✨ 简易配置中未加载的配置将被移除 * 🐛 测试更新 * ✨ 修复更新完成消息格式 --------- Co-authored-by: Flern <cb56ec362bbbfb5272eb941281299d8d-qq_connect@git.osc>
This commit is contained in:
@@ -25,7 +25,7 @@ if old_config_file.exists():
|
||||
old_config_file.rename(SIMPLE_CONFIG_FILE)
|
||||
|
||||
|
||||
def _handle_config(plugin: Plugin):
|
||||
def _handle_config(plugin: Plugin, exists_module: list[str]):
|
||||
"""处理配置项
|
||||
|
||||
参数:
|
||||
@@ -49,9 +49,10 @@ def _handle_config(plugin: Plugin):
|
||||
arg_parser=reg_config.arg_parser,
|
||||
_override=False,
|
||||
)
|
||||
exists_module.append(f"{module}:{reg_config.key}")
|
||||
|
||||
|
||||
def _generate_simple_config():
|
||||
def _generate_simple_config(exists_module: list[str]):
|
||||
"""
|
||||
生成简易配置
|
||||
|
||||
@@ -70,9 +71,12 @@ def _generate_simple_config():
|
||||
try:
|
||||
if _data.get(module) and k in _data[module].keys():
|
||||
Config.set_config(module, k, _data[module][k])
|
||||
_tmp_data[module][k] = Config.get_config(module, k)
|
||||
if f"{module}:{k}" in exists_module:
|
||||
_tmp_data[module][k] = Config.get_config(module, k)
|
||||
except AttributeError as e:
|
||||
raise AttributeError(f"{e}\n可能为config.yaml配置文件填写不规范") from e
|
||||
if not _tmp_data[module]:
|
||||
_tmp_data.pop(module)
|
||||
Config.save()
|
||||
temp_file = DATA_PATH / "temp_config.yaml"
|
||||
# 重新生成简易配置文件
|
||||
@@ -103,9 +107,10 @@ def _():
|
||||
初始化插件数据配置
|
||||
"""
|
||||
plugins2config_file = DATA_PATH / "configs" / "plugins2config.yaml"
|
||||
exists_module = []
|
||||
for plugin in get_loaded_plugins():
|
||||
if plugin.metadata:
|
||||
_handle_config(plugin)
|
||||
_handle_config(plugin, exists_module)
|
||||
if not Config.is_empty():
|
||||
Config.save()
|
||||
_data: CommentedMap = _yaml.load(plugins2config_file.open(encoding="utf8"))
|
||||
@@ -118,4 +123,4 @@ def _():
|
||||
# 存完插件基本设置
|
||||
with plugins2config_file.open("w", encoding="utf8") as wf:
|
||||
_yaml.dump(_data, wf)
|
||||
_generate_simple_config()
|
||||
_generate_simple_config(exists_module)
|
||||
|
||||
@@ -34,7 +34,6 @@ async def _handle_setting(
|
||||
plugin: Plugin,
|
||||
plugin_list: list[PluginInfo],
|
||||
limit_list: list[PluginLimit],
|
||||
task_list: list[tuple[bool, TaskInfo]],
|
||||
):
|
||||
"""处理插件设置
|
||||
|
||||
@@ -91,20 +90,6 @@ async def _handle_setting(
|
||||
)
|
||||
for limit in extra_data.limits
|
||||
)
|
||||
if extra_data.tasks:
|
||||
task_list.extend(
|
||||
(
|
||||
task.create_status,
|
||||
TaskInfo(
|
||||
module=task.module,
|
||||
name=task.name,
|
||||
status=task.status,
|
||||
run_time=task.run_time,
|
||||
default_status=task.default_status,
|
||||
),
|
||||
)
|
||||
for task in extra_data.tasks
|
||||
)
|
||||
|
||||
|
||||
@driver.on_startup
|
||||
@@ -114,14 +99,13 @@ async def _():
|
||||
"""
|
||||
plugin_list: list[PluginInfo] = []
|
||||
limit_list: list[PluginLimit] = []
|
||||
task_list = []
|
||||
module2id = {}
|
||||
load_plugin = []
|
||||
if module_list := await PluginInfo.all().values("id", "module_path"):
|
||||
module2id = {m["module_path"]: m["id"] for m in module_list}
|
||||
for plugin in get_loaded_plugins():
|
||||
load_plugin.append(plugin.module_name)
|
||||
await _handle_setting(plugin, plugin_list, limit_list, task_list)
|
||||
await _handle_setting(plugin, plugin_list, limit_list)
|
||||
create_list = []
|
||||
update_list = []
|
||||
for plugin in plugin_list:
|
||||
@@ -170,33 +154,6 @@ async def _():
|
||||
# limit_create.append(limit)
|
||||
# if limit_create:
|
||||
# await PluginLimit.bulk_create(limit_create, 10)
|
||||
if task_list:
|
||||
module_dict = {
|
||||
t[1]: t[0] for t in await TaskInfo.all().values_list("id", "module")
|
||||
}
|
||||
create_list = []
|
||||
update_list = []
|
||||
for status, task in task_list:
|
||||
if task.module not in module_dict:
|
||||
create_list.append((status, task))
|
||||
else:
|
||||
task.id = module_dict[task.module]
|
||||
update_list.append(task)
|
||||
if create_list:
|
||||
_create_list = [t[1] for t in create_list]
|
||||
await TaskInfo.bulk_create(_create_list, 10)
|
||||
if block := [t[1].module for t in create_list if not t[0]]:
|
||||
block_task = ",".join(block) + ","
|
||||
if group_list := await GroupConsole.all():
|
||||
for group in group_list:
|
||||
group.block_task += block_task
|
||||
await GroupConsole.bulk_update(group_list, ["block_task"], 10)
|
||||
if update_list:
|
||||
await TaskInfo.bulk_update(
|
||||
update_list,
|
||||
["run_time", "name"],
|
||||
10,
|
||||
)
|
||||
await data_migration()
|
||||
await PluginInfo.filter(module_path__in=load_plugin).update(load_status=True)
|
||||
await PluginInfo.filter(module_path__not_in=load_plugin).update(load_status=False)
|
||||
|
||||
@@ -0,0 +1,164 @@
|
||||
import nonebot
|
||||
from nonebot import get_loaded_plugins
|
||||
from nonebot.drivers import Driver
|
||||
from nonebot.plugin import Plugin
|
||||
from nonebot.utils import is_coroutine_callable
|
||||
from nonebot_plugin_apscheduler import scheduler
|
||||
|
||||
from zhenxun.configs.utils import PluginExtraData, Task
|
||||
from zhenxun.models.group_console import GroupConsole
|
||||
from zhenxun.models.task_info import TaskInfo
|
||||
from zhenxun.services.log import logger
|
||||
from zhenxun.utils.common_utils import CommonUtils
|
||||
|
||||
driver: Driver = nonebot.get_driver()
|
||||
|
||||
|
||||
async def _handle_setting(
|
||||
plugin: Plugin,
|
||||
task_info_list: list[tuple[bool, TaskInfo]],
|
||||
task_list: list[Task],
|
||||
):
|
||||
"""处理插件设置
|
||||
|
||||
参数:
|
||||
plugin: Plugin
|
||||
task_info_list: 被动技能db数据列表
|
||||
task_list: 被动技能列表
|
||||
"""
|
||||
metadata = plugin.metadata
|
||||
if not metadata:
|
||||
return
|
||||
extra = metadata.extra
|
||||
extra_data = PluginExtraData(**extra)
|
||||
if extra_data.tasks:
|
||||
task_info_list.extend(
|
||||
(
|
||||
task.create_status,
|
||||
TaskInfo(
|
||||
module=task.module,
|
||||
name=task.name,
|
||||
status=task.status,
|
||||
default_status=task.default_status,
|
||||
),
|
||||
)
|
||||
for task in extra_data.tasks
|
||||
)
|
||||
task_list.extend(extra_data.tasks)
|
||||
|
||||
|
||||
async def update_to_group(create_list: list[tuple[bool, TaskInfo]]):
|
||||
"""根据创建时状态对群组进行被动技能更新
|
||||
|
||||
参数:
|
||||
create_list: 被动技能创建列表
|
||||
"""
|
||||
if blocks := [t[1].module for t in create_list if not t[0]]:
|
||||
if group_list := await GroupConsole.all():
|
||||
for group in group_list:
|
||||
block_tasks = list(
|
||||
set(CommonUtils.convert_module_format(group.block_task) + blocks)
|
||||
)
|
||||
group.block_task = CommonUtils.convert_module_format(block_tasks)
|
||||
await GroupConsole.bulk_update(group_list, ["block_task"], 10)
|
||||
|
||||
|
||||
async def to_db(
|
||||
load_task: list[str],
|
||||
create_list: list[tuple[bool, TaskInfo]],
|
||||
update_list: list[TaskInfo],
|
||||
):
|
||||
"""将被动技能保存至数据库
|
||||
|
||||
参数:
|
||||
load_task: 已加载的被动技能模块
|
||||
create_list: 被动技能创建列表
|
||||
update_list: 被动技能更新列表
|
||||
"""
|
||||
if create_list:
|
||||
_create_list = [t[1] for t in create_list]
|
||||
await TaskInfo.bulk_create(_create_list, 10)
|
||||
await update_to_group(create_list)
|
||||
if update_list:
|
||||
await TaskInfo.bulk_update(
|
||||
update_list,
|
||||
["run_time", "name"],
|
||||
10,
|
||||
)
|
||||
if load_task:
|
||||
await TaskInfo.filter(module__in=load_task).update(load_status=True)
|
||||
await TaskInfo.filter(module__not_in=load_task).update(load_status=False)
|
||||
|
||||
|
||||
async def get_run_task(task: Task, *args, **kwargs):
|
||||
is_run = False
|
||||
if task.check:
|
||||
if is_coroutine_callable(task.check):
|
||||
if await task.check(*task.check_args):
|
||||
is_run = True
|
||||
elif task.check(*task.check_args):
|
||||
is_run = True
|
||||
else:
|
||||
bot = task.check_args[0]
|
||||
group_id = task.check_args[1]
|
||||
if not await CommonUtils.task_is_block(bot, task.module, group_id):
|
||||
is_run = True
|
||||
if is_run and task.run_func:
|
||||
if is_coroutine_callable(task.run_func):
|
||||
await task.run_func(*args, **kwargs)
|
||||
else:
|
||||
task.run_func(*args, **kwargs)
|
||||
|
||||
|
||||
async def create_schedule(task: Task):
|
||||
scheduler_model = task.scheduler
|
||||
if not scheduler_model or not task.run_func:
|
||||
return
|
||||
try:
|
||||
scheduler.add_job(
|
||||
get_run_task,
|
||||
scheduler_model.trigger,
|
||||
run_date=scheduler_model.run_date,
|
||||
hour=scheduler_model.hour,
|
||||
minute=scheduler_model.minute,
|
||||
second=scheduler_model.second,
|
||||
id=scheduler_model.id,
|
||||
max_instances=scheduler_model.max_instances,
|
||||
args=scheduler_model.args,
|
||||
kwargs=scheduler_model.kwargs,
|
||||
)
|
||||
logger.debug(f"成功动态创建定时任务: {task.name}({task.module})")
|
||||
except Exception as e:
|
||||
logger.error(f"动态创建定时任务 {task.name}({task.module}) 失败", e=e)
|
||||
|
||||
|
||||
@driver.on_startup
|
||||
async def _():
|
||||
"""
|
||||
初始化插件数据配置
|
||||
"""
|
||||
task_list: list[Task] = []
|
||||
task_info_list: list[tuple[bool, TaskInfo]] = []
|
||||
for plugin in get_loaded_plugins():
|
||||
await _handle_setting(plugin, task_info_list, task_list)
|
||||
if not task_info_list:
|
||||
await TaskInfo.all().update(load_status=False)
|
||||
return
|
||||
module_dict = {t[1]: t[0] for t in await TaskInfo.all().values_list("id", "module")}
|
||||
load_task = []
|
||||
create_list = []
|
||||
update_list = []
|
||||
for status, task in task_info_list:
|
||||
if task.module not in module_dict:
|
||||
create_list.append((status, task))
|
||||
else:
|
||||
task.id = module_dict[task.module]
|
||||
update_list.append(task)
|
||||
load_task.append(task.module)
|
||||
await to_db(load_task, create_list, update_list)
|
||||
# db_task = await TaskInfo.filter(load_status=True, status=True).values_list(
|
||||
# "module", flat=True
|
||||
# )
|
||||
# task_list = [t for t in task_list if t.module in db_task]
|
||||
# for task in task_list:
|
||||
# create_schedule(task)
|
||||
Reference in New Issue
Block a user