mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2025-12-14 21:52:56 +08:00
Some checks are pending
检查bot是否运行正常 / bot check (push) Waiting to run
CodeQL Code Security Analysis / Analyze (${{ matrix.language }}) (none, javascript-typescript) (push) Waiting to run
CodeQL Code Security Analysis / Analyze (${{ matrix.language }}) (none, python) (push) Waiting to run
Sequential Lint and Type Check / ruff-call (push) Waiting to run
Sequential Lint and Type Check / pyright-call (push) Blocked by required conditions
Release Drafter / Update Release Draft (push) Waiting to run
Force Sync to Aliyun / sync (push) Waiting to run
Update Version / update-version (push) Waiting to run
* ♻️ refactor(scheduler): 重构定时任务系统并增强功能 - 【模型重命名】将 `ScheduleInfo` 模型及其数据库表重命名为 `ScheduledJob`,以提高语义清晰度。 - 【触发器抽象】引入 `Trigger` 工厂类,提供类型安全的 Cron、Interval 和 Date 触发器配置。 - 【执行策略】新增 `ExecutionPolicy` 模型,允许为定时任务定义重试策略、延迟、异常类型以及成功/失败回调。 - 【任务执行】重构任务执行逻辑,支持 NoneBot 的依赖注入,并根据 `ExecutionPolicy` 处理任务的重试和回调。 - 【临时任务】增加声明式和编程式的临时任务调度能力,支持非持久化任务在运行时动态创建和执行。 - 【管理命令】更新定时任务管理命令 (`schedule_admin`),使其适配新的 `ScheduledJob` 模型和参数验证逻辑。 - 【展示优化】改进定时任务列表和状态展示,使用新的触发器格式化逻辑和参数模型信息。 - 【重试装饰器】为 `Retry.api` 装饰器添加 `on_success` 回调,允许在任务成功执行后触发额外操作。 * 🚨 auto fix by pre-commit hooks --------- Co-authored-by: webjoin111 <455457521@qq.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
241 lines
8.1 KiB
Python
241 lines
8.1 KiB
Python
import asyncio
|
|
from typing import Any
|
|
|
|
from zhenxun.models.scheduled_job import ScheduledJob
|
|
from zhenxun.services.scheduler import scheduler_manager
|
|
from zhenxun.utils._image_template import ImageTemplate, RowStyle
|
|
from zhenxun.utils.pydantic_compat import model_json_schema
|
|
|
|
|
|
def _get_type_name(annotation) -> str:
|
|
"""获取类型注解的名称"""
|
|
if hasattr(annotation, "__name__"):
|
|
return annotation.__name__
|
|
elif hasattr(annotation, "_name"):
|
|
return annotation._name
|
|
else:
|
|
return str(annotation)
|
|
|
|
|
|
def _get_schedule_attr(schedule: ScheduledJob | dict, attr_name: str) -> Any:
|
|
"""兼容地从字典或对象获取属性"""
|
|
if isinstance(schedule, dict):
|
|
return schedule.get(attr_name)
|
|
return getattr(schedule, attr_name, None)
|
|
|
|
|
|
def _format_trigger_info(schedule: ScheduledJob | dict) -> str:
|
|
"""格式化触发器信息为可读字符串(兼容字典和对象)"""
|
|
trigger_type = _get_schedule_attr(schedule, "trigger_type")
|
|
config = _get_schedule_attr(schedule, "trigger_config")
|
|
|
|
if not isinstance(config, dict):
|
|
return f"配置错误: {config}"
|
|
|
|
if trigger_type == "cron":
|
|
hour = config.get("hour", "??")
|
|
minute = config.get("minute", "??")
|
|
try:
|
|
hour_int = int(hour)
|
|
minute_int = int(minute)
|
|
return f"每天 {hour_int:02d}:{minute_int:02d}"
|
|
except (ValueError, TypeError):
|
|
return f"每天 {hour}:{minute}"
|
|
elif trigger_type == "interval":
|
|
units = {
|
|
"weeks": "周",
|
|
"days": "天",
|
|
"hours": "小时",
|
|
"minutes": "分钟",
|
|
"seconds": "秒",
|
|
}
|
|
for unit, unit_name in units.items():
|
|
if value := config.get(unit):
|
|
return f"每 {value} {unit_name}"
|
|
return "未知间隔"
|
|
elif trigger_type == "date":
|
|
run_date = config.get("run_date", "N/A")
|
|
return f"特定时间 {run_date}"
|
|
else:
|
|
return f"未知触发器类型: {trigger_type}"
|
|
|
|
|
|
def _format_operation_result_card(
|
|
title: str, schedule_info: ScheduledJob, extra_info: list[str] | None = None
|
|
) -> str:
|
|
"""
|
|
生成一个标准的操作结果信息卡片。
|
|
|
|
参数:
|
|
title: 卡片的标题 (例如 "✅ 成功暂停定时任务!")
|
|
schedule_info: 相关的 ScheduledJob 对象
|
|
extra_info: (可选) 额外的补充信息行
|
|
"""
|
|
target_desc = (
|
|
f"群组 {schedule_info.group_id}"
|
|
if schedule_info.group_id
|
|
and schedule_info.group_id != scheduler_manager.ALL_GROUPS
|
|
else "所有群组"
|
|
if schedule_info.group_id == scheduler_manager.ALL_GROUPS
|
|
else "全局"
|
|
)
|
|
|
|
info_lines = [
|
|
title,
|
|
f"✓ 任务 ID: {schedule_info.id}",
|
|
f"🖋 插件: {schedule_info.plugin_name}",
|
|
f"🎯 目标: {target_desc}",
|
|
f"⏰ 时间: {_format_trigger_info(schedule_info)}",
|
|
]
|
|
if extra_info:
|
|
info_lines.extend(extra_info)
|
|
|
|
return "\n".join(info_lines)
|
|
|
|
|
|
def format_pause_success(schedule_info: ScheduledJob) -> str:
|
|
"""格式化暂停成功的消息"""
|
|
return _format_operation_result_card("✅ 成功暂停定时任务!", schedule_info)
|
|
|
|
|
|
def format_resume_success(schedule_info: ScheduledJob) -> str:
|
|
"""格式化恢复成功的消息"""
|
|
return _format_operation_result_card("▶️ 成功恢复定时任务!", schedule_info)
|
|
|
|
|
|
def format_remove_success(schedule_info: ScheduledJob) -> str:
|
|
"""格式化删除成功的消息"""
|
|
return _format_operation_result_card("❌ 成功删除定时任务!", schedule_info)
|
|
|
|
|
|
def format_trigger_success(schedule_info: ScheduledJob) -> str:
|
|
"""格式化手动触发成功的消息"""
|
|
return _format_operation_result_card("🚀 成功手动触发定时任务!", schedule_info)
|
|
|
|
|
|
def format_update_success(schedule_info: ScheduledJob) -> str:
|
|
"""格式化更新成功的消息"""
|
|
return _format_operation_result_card("🔄️ 成功更新定时任务配置!", schedule_info)
|
|
|
|
|
|
def _status_row_style(column: str, text: str) -> RowStyle:
|
|
"""为状态列设置颜色"""
|
|
style = RowStyle()
|
|
if column == "状态":
|
|
if text == "启用":
|
|
style.font_color = "#67C23A"
|
|
elif text == "暂停":
|
|
style.font_color = "#F56C6C"
|
|
elif text == "运行中":
|
|
style.font_color = "#409EFF"
|
|
return style
|
|
|
|
|
|
def _format_params(schedule_status: dict) -> str:
|
|
"""将任务参数格式化为人类可读的字符串"""
|
|
if kwargs := schedule_status.get("job_kwargs"):
|
|
return " | ".join(f"{k}: {v}" for k, v in kwargs.items())
|
|
return "-"
|
|
|
|
|
|
async def format_schedule_list_as_image(
|
|
schedules: list[ScheduledJob], title: str, current_page: int
|
|
):
|
|
"""将任务列表格式化为图片"""
|
|
page_size = 15
|
|
total_items = len(schedules)
|
|
total_pages = (total_items + page_size - 1) // page_size
|
|
start_index = (current_page - 1) * page_size
|
|
end_index = start_index + page_size
|
|
paginated_schedules = schedules[start_index:end_index]
|
|
|
|
if not paginated_schedules:
|
|
return "这一页没有内容了哦~"
|
|
|
|
status_tasks = [
|
|
scheduler_manager.get_schedule_status(s.id) for s in paginated_schedules
|
|
]
|
|
all_statuses = await asyncio.gather(*status_tasks)
|
|
|
|
def get_status_text(status_value):
|
|
if isinstance(status_value, bool):
|
|
return "启用" if status_value else "暂停"
|
|
return str(status_value)
|
|
|
|
data_list = [
|
|
[
|
|
s["id"],
|
|
s["plugin_name"],
|
|
s.get("bot_id") or "N/A",
|
|
s["group_id"] or "全局",
|
|
s["next_run_time"],
|
|
_format_trigger_info(s),
|
|
_format_params(s),
|
|
get_status_text(s["is_enabled"]),
|
|
]
|
|
for s in all_statuses
|
|
if s
|
|
]
|
|
|
|
if not data_list:
|
|
return "没有找到任何相关的定时任务。"
|
|
|
|
return await ImageTemplate.table_page(
|
|
head_text=title,
|
|
tip_text=f"第 {current_page}/{total_pages} 页,共 {total_items} 条任务",
|
|
column_name=["ID", "插件", "Bot", "目标", "下次运行", "规则", "参数", "状态"],
|
|
data_list=data_list,
|
|
column_space=20,
|
|
text_style=_status_row_style,
|
|
)
|
|
|
|
|
|
def format_single_status_message(status: dict) -> str:
|
|
"""格式化单个任务状态为文本消息"""
|
|
info_lines = [
|
|
f"📋 定时任务详细信息 (ID: {status['id']})",
|
|
"--------------------",
|
|
f"▫️ 插件: {status['plugin_name']}",
|
|
f"▫️ Bot ID: {status.get('bot_id') or '默认'}",
|
|
f"▫️ 目标: {status['group_id'] or '全局'}",
|
|
f"▫️ 状态: {'✔️ 已启用' if status['is_enabled'] else '⏸️ 已暂停'}",
|
|
f"▫️ 下次运行: {status['next_run_time']}",
|
|
f"▫️ 触发规则: {_format_trigger_info(status)}",
|
|
f"▫️ 任务参数: {_format_params(status)}",
|
|
]
|
|
return "\n".join(info_lines)
|
|
|
|
|
|
async def format_plugins_list() -> str:
|
|
"""格式化可用插件列表为文本消息"""
|
|
from pydantic import BaseModel
|
|
|
|
registered_plugins = scheduler_manager.get_registered_plugins()
|
|
if not registered_plugins:
|
|
return "当前没有已注册的定时任务插件。"
|
|
|
|
message_parts = ["📋 已注册的定时任务插件:"]
|
|
for i, plugin_name in enumerate(registered_plugins, 1):
|
|
task_meta = scheduler_manager._registered_tasks[plugin_name]
|
|
params_model = task_meta.get("model")
|
|
|
|
param_info_str = "无参数"
|
|
if (
|
|
params_model
|
|
and isinstance(params_model, type)
|
|
and issubclass(params_model, BaseModel)
|
|
):
|
|
schema = model_json_schema(params_model)
|
|
properties = schema.get("properties", {})
|
|
if properties:
|
|
param_info_str = "参数: " + ", ".join(
|
|
f"{field_name}({prop.get('type', 'any')})"
|
|
for field_name, prop in properties.items()
|
|
)
|
|
elif params_model:
|
|
param_info_str = "⚠️ 参数模型配置错误"
|
|
|
|
message_parts.append(f"{i}. {plugin_name} - {param_info_str}")
|
|
|
|
return "\n".join(message_parts)
|