# 每月触发的重复自动化未按预期运行

**URL:** <https://meta.discourse.org/t/monthly-triggered-recurring-automation-is-not-running-as-expected/402664>\
**Category:** Bug\
**Tags:** data-explorer, automation, fixed\
**Created:** [2026年五月11日 15:03 UTC](https://meta.discourse.org/t/monthly-triggered-recurring-automation-is-not-running-as-expected/402664 "2026-05-11T15:03:11Z")\
**Posts on this page:** 1\
**Showing post:** 13

<div class="post-metadata">

**Author:** ![Lilly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lilly/32/575047_2.png) [@Lilly](https://meta.discourse.org/u/Lilly)\
**Post date:** [2026年五月12日 08:04 UTC](https://meta.discourse.org/t/monthly-triggered-recurring-automation-is-not-running-as-expected/402664/13 "2026-05-12T08:04:03Z")

</div>

好的，Moin 和我今天聊到了这个案例，她提到使用“每月”时可能存在一个 bug，因为月份的长度各不相同？当然，她是对的——至少部分正确：😛

不管怎样，我又做了一些调查：🕵

我实际上在核心 `plugins/automation/lib/discourse_automation/scripts` 文件夹中找不到 `schedule pm with data-explorer results` 的自动化脚本，所以它可能藏在别的地方。

但我查看了 `plugins/automation/lib/discourse_automation/triggers/recurring.rb` 中关于重复执行的逻辑（推测该脚本会使用它）：

> <https://github.com/discourse/discourse/blob/4c15bb09c420748db9404c3df6dd56fda4c4a118/plugins/automation/lib/discourse_automation/triggers/recurring.rb#L81-L90>

我认为每月重复自动化的底层代码是在计算“第 N 个星期 X”，而不是实际的日历日期，这与“日历日期”存在逻辑不匹配。也就是说，通过向 RRule 注入 `BYDAY=#{count}#{byday}`，自动化强制按星期几对齐，而应该使用 `BYMONTHDAY` 而不是 `BYDAY`。

因此，如果你将自动化设置为从 4 月 15 日开始（这一天恰好是 4 月的第三个星期三），系统会将该规则解释为：_在每个月的第三个星期三运行_。

到了 5 月，第三个星期三是 _5 月 20 日_，这导致计划向后漂移了 5 天。

此外，如果你将自动化设置在某月的 30 日或 31 日（例如“第五个星期二”），系统会在下个月寻找第五个星期二。如果该月只有四个星期二，自动化将完全无法找到有效日期，并 _静默跳过整个月份_。

我已经测试了一个修复方案，并准备提交 PR，如果团队想查看的话。它将逻辑从“第 N 个星期 X”改为精确的“日历日期”。

* * *

以下是我在测试和调试过程中的一些截图：

两个实例，一个未修复，另一个应用了我的逻辑修复。

在两者中创建完全相同的自动化（修复版本在右侧）：

 ![Screenshot 2026-05-12 at 12.29.39 AM](https://global.discourse-cdn.com/meta/original/4X/9/9/5/99594dfedf94893e58126f7940ebf30d489ef056.png)

 ![Screenshot 2026-05-12 at 12.30.01 AM](https://global.discourse-cdn.com/meta/original/4X/5/f/6/5f603b864b2135abc1945994c121b0b234f1be92.png)

查看横幅中显示“下次自动化将在以下时间触发：”的差异，其中存在 5 天的漂移——未打补丁的版本漂移到了 _第三个星期三_（5 月 20 日），而不是 5 月 15 日。

例如，如果系统寻找的是“第四个星期四”而不是实际日期，某些月份可能会被完全跳过。

也可以在 Rails 控制台中测试相同的结果。

未打补丁的版本：

 ![Screenshot 2026-05-12 at 12.43.50 AM](https://global.discourse-cdn.com/meta/original/4X/f/7/4/f745fc258acbdf7a1169ee6a68357c66369bef1b.png)

应用修复后：

 ![Screenshot 2026-05-12 at 12.43.20 AM](https://global.discourse-cdn.com/meta/original/4X/0/8/e/08e284434669b9b226387c1a3efc3858bee9a0a0.png)

PR 地址：

> <https://github.com/discourse/discourse/pull/39917>
>
> \*\*Description:\*\*
> 
> \*\*Context / Bug:\*\*
> Currently, when an admin sets a monthly …recurring automation, the script attempts to calculate the "Nth Weekday" (e.g., "The 3rd Tuesday") using \`BYDAY\` instead of the actual calendar date. This causes two major issues for administrators:
> 1. \*\*Shifting Dates:\*\* Users expect a monthly report to run on the exact same calendar date (e.g., the 15th). Instead, the date shifts back and forth depending on when the "3rd Tuesday" or "2nd Friday" falls in the following month.
> 2. \*\*Silent Failures:\*\* If an admin sets a start date on the 30th or 31st of a month (e.g., the 5th Tuesday), the automation generates a rule like \`BYDAY=5TU\`. Months that only have 4 Tuesdays will completely fail to find a valid execution date and silently skip the entire month.
> 
> \*\*Changes:\*\*
> \* Replaced the \`BYDAY\` string calculation with \`BYMONTHDAY=#{start\_date.day}\`. Monthly schedules will now reliably trigger on the exact calendar date matching user expectations.
> \* Removed the now-unused \`count\` while-loop that was calculating the weekday occurrence.
> \* \*Minor cleanup:\* Swapped \`Time.now\` for \`Time.zone.now\` in the RRule bounds check for standard Rails timezone consistency.
> 
> \*\*Test Updates:\*\*
> \* Updated two existing assertions in \`recurring\_spec.rb\`. The original tests were explicitly (and incorrectly) expecting the buggy Nth-weekday drift. I corrected the assertion dates to expect the proper exact calendar day.
> 
> \*\*Files Touched:\*\*
> \* \`plugins/discourse-automation/lib/discourse\_automation/triggers/recurring.rb\`
> \* \`plugins/discourse-automation/spec/lib/discourse\_automation/triggers/recurring\_spec.rb\`

编辑：该 bug 似乎影响了所有使用 `recurring` 触发器并设置为每月运行的自动化脚本。😬

我有时也能复现空横幅的情况，例如，如果将开始日期设置为 2026 年 4 月 29 日，那么系统将无法找到 5 月的“第五个星期三”，因为该日期不存在——自动化将在此处失败（未修复版本在左侧，修复版本在右侧）：

 ![Screenshot 2026-05-12 at 7.31.48 AM](https://global.discourse-cdn.com/meta/original/4X/6/d/6/6d61715c8c04d3a8a435c7f4b91a6d8770b47ab5.png)

 ![Screenshot 2026-05-12 at 7.41.03 AM](https://global.discourse-cdn.com/meta/original/4X/7/2/c/72c3714047cf6f9d26c093bb5de5a152b05d71ca.png)

看起来当系统无法找到如上的“第 N 个星期 X”时，Rails 会抛出 `NoMethodError`；因此导致下次触发横幅为空，且运行失败：

 ![Screenshot 2026-05-12 at 7.53.22 AM](https://global.discourse-cdn.com/meta/original/4X/b/3/5/b354a68a4dabbf9c033a2e713b118ff313d32186.png)

---

_[View the full topic](https://meta.discourse.org/t/monthly-triggered-recurring-automation-is-not-running-as-expected/402664)._
