Files
zhenxun_bot/zhenxun/services/scheduler/repository.py
T
be86e0bb7f
检查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): 重构定时任务系统并增强功能 (#2009)
* ♻️ 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>
2025-08-06 09:02:23 +08:00

99 lines
3.0 KiB
Python

"""
数据持久层 (Repository)
封装所有对 ScheduledJob 模型的数据库操作,将数据访问逻辑与业务逻辑分离。
"""
from typing import Any
from tortoise.queryset import QuerySet
from zhenxun.models.scheduled_job import ScheduledJob
class ScheduleRepository:
"""封装 ScheduledJob 模型的数据库操作"""
@staticmethod
async def get_by_id(schedule_id: int) -> ScheduledJob | None:
"""
通过ID获取任务
参数:
schedule_id: 任务ID。
返回:
ScheduledJob | None: 任务对象,不存在时返回None。
"""
return await ScheduledJob.get_or_none(id=schedule_id)
@staticmethod
async def get_all_enabled() -> list[ScheduledJob]:
"""
获取所有启用的任务
返回:
list[ScheduledJob]: 所有启用状态的任务列表。
"""
return await ScheduledJob.filter(is_enabled=True).all()
@staticmethod
async def get_all(plugin_name: str | None = None) -> list[ScheduledJob]:
"""获取所有任务,可按插件名过滤"""
if plugin_name:
return await ScheduledJob.filter(plugin_name=plugin_name).all()
return await ScheduledJob.all()
@staticmethod
async def save(schedule: ScheduledJob, update_fields: list[str] | None = None):
"""
保存任务
参数:
schedule: 要保存的任务对象。
update_fields: 要更新的字段列表,None表示更新所有字段。
"""
await schedule.save(update_fields=update_fields)
@staticmethod
async def exists(**kwargs: Any) -> bool:
"""检查任务是否存在"""
return await ScheduledJob.exists(**kwargs)
@staticmethod
async def get_by_plugin_and_group(
plugin_name: str, group_ids: list[str]
) -> list[ScheduledJob]:
"""根据插件和群组ID列表获取任务"""
return await ScheduledJob.filter(
plugin_name=plugin_name, group_id__in=group_ids
).all()
@staticmethod
async def update_or_create(
defaults: dict, **kwargs: Any
) -> tuple[ScheduledJob, bool]:
"""更新或创建任务"""
return await ScheduledJob.update_or_create(defaults=defaults, **kwargs)
@staticmethod
async def query_schedules(**filters: Any) -> list[ScheduledJob]:
"""
根据任意条件查询任务列表
参数:
**filters: 过滤条件,如 group_id="123", plugin_name="abc"
返回:
list[ScheduledJob]: 任务列表
"""
cleaned_filters = {k: v for k, v in filters.items() if v is not None}
if not cleaned_filters:
return await ScheduledJob.all()
return await ScheduledJob.filter(**cleaned_filters).all()
@staticmethod
def filter(**kwargs: Any) -> QuerySet[ScheduledJob]:
"""提供一个通用的过滤查询接口,供Targeter使用"""
return ScheduledJob.filter(**kwargs)