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>
81 lines
2.1 KiB
Python
81 lines
2.1 KiB
Python
from datetime import datetime
|
|
from typing import Literal
|
|
|
|
from pydantic import BaseModel, Field
|
|
|
|
|
|
class BaseTrigger(BaseModel):
|
|
"""触发器配置的基类"""
|
|
|
|
trigger_type: str = Field(..., exclude=True)
|
|
|
|
|
|
class CronTrigger(BaseTrigger):
|
|
"""Cron 触发器配置"""
|
|
|
|
trigger_type: Literal["cron"] = "cron" # type: ignore
|
|
year: int | str | None = None
|
|
month: int | str | None = None
|
|
day: int | str | None = None
|
|
week: int | str | None = None
|
|
day_of_week: int | str | None = None
|
|
hour: int | str | None = None
|
|
minute: int | str | None = None
|
|
second: int | str | None = None
|
|
start_date: datetime | str | None = None
|
|
end_date: datetime | str | None = None
|
|
timezone: str | None = None
|
|
jitter: int | None = None
|
|
|
|
|
|
class IntervalTrigger(BaseTrigger):
|
|
"""Interval 触发器配置"""
|
|
|
|
trigger_type: Literal["interval"] = "interval" # type: ignore
|
|
weeks: int = 0
|
|
days: int = 0
|
|
hours: int = 0
|
|
minutes: int = 0
|
|
seconds: int = 0
|
|
start_date: datetime | str | None = None
|
|
end_date: datetime | str | None = None
|
|
timezone: str | None = None
|
|
jitter: int | None = None
|
|
|
|
|
|
class DateTrigger(BaseTrigger):
|
|
"""Date 触发器配置"""
|
|
|
|
trigger_type: Literal["date"] = "date" # type: ignore
|
|
run_date: datetime | str
|
|
timezone: str | None = None
|
|
|
|
|
|
class Trigger:
|
|
"""
|
|
一个用于创建类型安全触发器配置的工厂类。
|
|
提供了流畅的、具备IDE自动补全功能的API。
|
|
|
|
使用示例:
|
|
from zhenxun.services.scheduler import Trigger
|
|
|
|
@scheduler.job(trigger=Trigger.cron(hour=8))
|
|
async def my_task():
|
|
...
|
|
"""
|
|
|
|
@staticmethod
|
|
def cron(**kwargs) -> CronTrigger:
|
|
"""创建一个 Cron 触发器配置。"""
|
|
return CronTrigger(**kwargs)
|
|
|
|
@staticmethod
|
|
def interval(**kwargs) -> IntervalTrigger:
|
|
"""创建一个 Interval 触发器配置。"""
|
|
return IntervalTrigger(**kwargs)
|
|
|
|
@staticmethod
|
|
def date(**kwargs) -> DateTrigger:
|
|
"""创建一个 Date 触发器配置。"""
|
|
return DateTrigger(**kwargs)
|