Skip to content

[Bug] 定时间隔未按固定间隔执行:每 60 分钟变为 59/1 分钟交替,每 24 小时变为 23/1 小时交替 #10363

Open

Description

@wcqqq1214

问题描述

WebUI 定时任务的“定时间隔”模式,会将输入转换为不等价的 Cron 表达式,导致实际触发间隔与设置不一致。

设置 实际生成的 Cron 实际触发间隔
每 60 分钟 */59 * * * * 59、1 分钟交替
每 24 小时 0 */23 * * * 23、1 小时交替
每 40 分钟 */40 * * * * 40、20 分钟交替

预期“每隔 N 分钟/小时”保持固定时间间隔,不应静默截断输入,也不应在跨小时或跨天时缩短间隔。

初步定位到 dashboard/src/views/CronJobPage.vue 的 buildCronExpression():

if (newJob.value.interval_unit === "minutes") {
  return `*/${Math.min(value, 59)} * * * *`;
}
if (newJob.value.interval_unit === "hours") {
  return `0 */${Math.min(value, 23)} * * *`;
}

这会将 60 分钟截为 59、24 小时截为 23。后端按 Cron 规则正常执行,于是分别在每小时的第 0、59 分钟,以及每天的第 0、23 小时触发。

此外,即使没有触发截断,Cron 的字段步长也不等价于任意固定时间间隔,例如每 40 分钟的情况。因此,仅调整上限不能完整解决问题。

上述逻辑由 PR #8559 — feat: future task UI 引入。

修复方向供讨论:是否考虑为“定时间隔”使用 APScheduler 的 IntervalTrigger,保留其他模式的 Cron 调度?首次触发、重启恢复和历史任务兼容的具体行为,希望由维护者确认。旧任务仅保存转换后的 Cron,无法可靠还原用户最初输入的间隔,不建议直接自动转换。

如何复现?

本次采用代码级复现,未通过浏览器创建真实任务,也未等待实际消息投递:

  1. 使用提交 71f9ccbdd 的代码。
  2. 提取并执行 CronJobPage.vue 中的 validateScheduleFields() 和 buildCronExpression()。
  3. 设置 schedule_mode="interval",分别输入 60 分钟、24 小时、40 分钟。三组输入均通过表单校验。
  4. 将生成的 Cron 表达式传入项目实际的 CronJobManager._build_trigger()。
  5. 使用返回触发器的 get_next_fire_time(),从 2026-10-03 00:00:00 Asia/Shanghai 起计算连续五次触发时间。
  6. 比较相邻时间,得到上述交替间隔。其中 60 分钟和 24 小时两组结果均通过断言验证。

AstrBot 版本

4.29.0-beta.1

操作系统

MacOS

部署方式

本地源码

使用的消息平台适配器

不涉及

错误日志

2026-10-03 00:59:00+08:00
2026-10-03 01:00:00+08:00
2026-10-03 01:59:00+08:00
2026-10-03 02:00:00+08:00
相邻间隔(分钟):[59, 1, 59, 1]

输入:每 24 小时
表单校验:通过
Cron:0 */23 * * *
触发时间:
2026-10-03 00:00:00+08:00
2026-10-03 23:00:00+08:00
2026-10-04 00:00:00+08:00
2026-10-04 23:00:00+08:00
2026-10-05 00:00:00+08:00
相邻间隔(小时):[23, 1, 23, 1]

输入:每 40 分钟
表单校验:通过
Cron:*/40 * * * *
触发时间:
2026-10-03 00:00:00+08:00
2026-10-03 00:40:00+08:00
2026-10-03 01:00:00+08:00
2026-10-03 01:40:00+08:00
2026-10-03 02:00:00+08:00
相邻间隔(分钟):[40, 20, 40, 20]

辅助信息

No response

检查清单

  • 我已在 Issue 列表中搜索过相关问题仍无法解决,或该问题从未被报告过。
  • 我已尝试过禁用所有插件,排除了可能是因插件导致的问题。
  • 我报告的问题与 AstrBot 本体相关,而非在报告某一插件的问题。
  • 我已阅读并同意本项目的贡献者行为准则。
  • (可选)我愿意提交 PR 以帮助修复该问题。

Activity

  1. added
    bug
    on Oct 3, 2026
  2. LIghtJUNction commented on Oct 4, 2026

    @LIghtJUNction
    Member

    我按当前 master@42972e9 复核了 WebUI、API service、持久化模型和调度器。issue 的复现成立,而且不能只把 59/23 改成 60/24:当前整条数据模型只表达 Date/Cron,没有表达“固定持续时间间隔”。

    判断与代码依据

    • dashboard/src/views/CronJobPage.vue::buildCronExpression() 将 interval 强制编码为 crontab 字段步长;validateScheduleFields() 却允许任意正整数,所以 40 分钟、60 分钟、24 小时都会通过校验。
    • 同文件的 readScheduleFromJob() 和 scheduleProductLabel() 又把 */N 反向解释/展示成“每隔 N”。这不只是调度错误,也会在编辑页和任务卡片上继续向用户承诺错误语义。
    • buildPayload() 只提交 run_once / cron_expression。后端 astrbot/dashboard/services/cron_service.py::create_job()、_merge_active_agent_updates() 对非一次性任务强制要求 cron_expression。
    • astrbot/core/db/po.py::CronJob 没有 interval 类型或锚点字段;astrbot/core/cron/manager.py::_build_trigger() 也只构造 DateTrigger 或 CronTrigger。因此仅改前端表达式无法获得固定间隔,也无法在重启后恢复其相位。
    • 当前测试主要覆盖 Cron/Date 的校验和调度;没有覆盖 WebUI interval 的 round-trip 与连续 fire time 差值。

    已确认的原因链是:

    interval_value/unit → 前端转换为五段 Cron → interval 原始语义被丢弃 → DB 只保存 Cron 字符串 → CronTrigger.from_crontab() 按日历字段边界触发 → 出现 40/20、59/1、23/1 的交替间隔。

    修改建议

    建议把 interval 作为真正的第三种调度类型,而不是特殊 Cron。最小可执行的数据契约可以是:

    run_once=true                         -> DateTrigger
    run_once=false, interval_seconds!=null -> IntervalTrigger
    run_once=false, interval_seconds=null  -> CronTrigger(cron_expression)
    

    同时持久化一个 UTC 的 interval_anchor_at。具体改动边界:

    1. 在 CronJob、数据库创建/更新方法以及旧 SQLite 前向迁移中增加 nullable interval_seconds、interval_anchor_at;二者与 cron_expression 做互斥校验。API/OpenAPI 类型也同步更新。
    2. CronService.create_job() 和 _merge_active_agent_updates() 对 interval 做服务端整数、正数和合理上限校验,不能只信任前端。创建 interval 时固定一次 anchor;仅修改名称、备注、会话或启用状态不应重置 anchor,修改间隔时才按明确规则重置。
    3. CronJobManager._build_trigger() 引入 APScheduler 的 IntervalTrigger,从已持久化的 seconds + anchor 重建。建议定义:
      • 首次执行为 anchor + interval,而不是创建后立刻执行;
      • 重启后保持原相位,选择下一个未来时刻,不补发停机期间的全部次数;
      • “每 24 小时”表示固定 24 小时,跨 DST 时当地钟点可能变化;需要固定当地钟点的用户继续使用现有“每天”模式。
    4. WebUI interval 模式直接提交结构化 interval,不再调用 buildCronExpression();readScheduleFromJob() 只在后端明确返回 interval 字段时恢复为 interval。
    5. 不要把已有 */N 自动迁移为 interval。 旧记录只保留转换后的 Cron,无法知道用户原本选的是 interval 还是手写 Cron;应继续按 Cron 执行并在 UI 按自定义 Cron 展示。否则升级会静默改变既有任务的触发时刻。
    6. scheduleProductLabel() 对旧 */N 不再显示“每隔 N”,或至少标明它是“Cron 字段步长”;只有真正的 IntervalTrigger 才使用“每隔 N”文案。

    修改后的效果

    新建的 40 分钟、60 分钟、24 小时间隔任务会保持固定时间差,并能在重启后保持相位;每天/每周/每月和自定义 Cron 的日历语义不变。旧任务不会被错误猜测、自动改义。编辑任务的备注等非时间字段也不会意外推迟下一次执行。

    建议验证

    • 连续 fire time:40 分钟始终差 40 分钟,60 分钟始终差 60 分钟,24 小时始终差 24 小时;
    • 创建后的首次 fire time,以及修改 interval 后的 anchor/next run;
    • 重启前后使用同一持久化 anchor,停机跨过多个周期后只得到下一个未来时刻;
    • DST 前后:interval 保持 elapsed duration,“每天 HH:mm”保持当地钟点;
    • create/update 对零、负数、浮点、超大值、同时提交 cron+interval 的拒绝;
    • 只改名称/备注/会话/启用状态不重置 interval 相位;
    • 旧 */40 * * * * 仍按 Cron 运行且编辑页不冒充固定间隔;
    • tests/unit/test_cron_manager.py、test_cron_service.py 增加后端用例,并为 CronJobPage.vue 的构建、回读和标签增加前端单测。
  3. ArcueidMP commented on Oct 4, 2026

    @ArcueidMP

    I'd like to take this on and prepare a fix with regression tests. Thanks for the detailed analysis above; I'll use it as a starting point and preserve the behavior of existing Cron jobs.

    Please assign this issue to me if possible.

  4. moved this to In Progress in AstrBot Roadmapon Nov 2, 2026
  5. ArcueidMP commented on Nov 10, 2026

    @ArcueidMP

    @Soulter Could the maintainers pick one of the options below for this issue? I'll follow whichever you choose:

    A. Full fix (#10373, already submitted)
    Following LIghtJUNction's analysis above, intervals become a separate schedule type, run by APScheduler's IntervalTrigger with a persisted UTC anchor. Every 40 minutes, 60 minutes, or 24 hours becomes a true fixed interval and keeps its phase across restarts; existing jobs keep their original Cron expressions, and regression tests are included. The cost is two nullable columns on the cron_jobs table (with a migration), plus API and WebUI changes. The PR is up to date with the latest master and has no conflicts; as a first-time contribution, its CI needs maintainer approval before it can run.

    B. Small frontend-only fix
    The WebUI only accepts intervals that divide evenly: minutes must be a factor of 60, hours a factor of 24, and days only 1. 60 minutes becomes hourly, 24 hours becomes daily, and other values show a hint to use a custom Cron expression. The change is small and leaves the database and API untouched, but it cannot provide true fixed intervals such as every 40 minutes.

    C. No change for now
    Keep the current behavior (already described in the docs).

    If you choose B, I'll close #10373 and submit a small PR within a day or two; if you choose C, I'll close #10373 as well. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions