Lilly
(Lillian )
2026 年 5 月 12 日午前 8:04
13
はい、Moin と今日はこのケースについて話していました。彼女は、月の長さが異なるため、月次(monthly)を使用するとバグがあるかもしれないと指摘しました。もちろん、彼女は正しいと言えなくもないです…
ともあれ、少し詳しく調べてみました
コアの plugins/automation/lib/discourse_automation/scripts フォルダに、schedule pm with data-explorer results の自動化スクリプトが見つかりませんでした。どこかに隠れているようです。
しかし、plugins/automation/lib/discourse_automation/triggers/recurring.rb の再帰(recurring)のロジックを見てみました(おそらくそのスクリプトはこれを使用しています):
when "month"
count = 0
(start_date.beginning_of_month.to_date..start_date.end_of_month.to_date).each do |date|
count += 1 if date.strftime("%A") == start_date.strftime("%A")
break if date.day == start_date.day
end
RRule::Rule
.new("FREQ=MONTHLY;INTERVAL=#{interval};BYDAY=#{count}#{byday}", dtstart: start_date)
.between(Time.now, interval_end.months.from_now)
.find { |date| date > Time.zone.now }
月次再帰自動化の基盤コードは、実際の暦日ではなく「第 N 週の曜日」を計算しており、Calendar day とのロジックの不一致があると思います。RRule に BYDAY=#{count}#{byday} を注入することで、自動化は曜日への整合性を強制しており、代わりに BYMONTHDAY を使用するべきです。
つまり、4 月 15 日(4 月の第 3 水曜日)に自動化を開始するように設定すると、システムはそのルールを「毎月の第 3 水曜日に実行する」と解釈します。
5 月になると、第 3 水曜日は 5 月 20 日となり、スケジュールが 5 日ずれてしまいます。
さらに、月の 30 日や 31 日(例:「第 5 火曜日」)に自動化を設定した場合、システムは翌月の第 5 火曜日を探します。その月に火曜日が 4 回しかない場合、自動化は有効な日付を完全に発見できず、その月全体を「静かにスキップ」してしまいます。
私はテスト済みの修正を持っており、チームが確認したい場合は PR を作成中です。これはロジックを「第 N 週の曜日」から「正確な暦日」に変更するものです。
以下は、私のテストとデバッグからのスクリーンショットです:
2 つのインスタンス、一つは修正なし、もう一つは私のロジック修正を適用したもの
両方に全く同じ自動化を作成します(修正版は右側):
「次の自動化はいつトリガーされますか:」と表示されるバナーの違いを確認してください。5 日のズレが生じています。修正前のバージョンは、5 月 15 日ではなく、第 3 水曜日 (5 月 20 日)にズレています。
例えば、実際の日付ではなく「第 4 木曜日」を探している場合、月が完全にスキップされる可能性があります。
rails コンソールでも同じ結果を確認できます
修正前:
修正後:
PR はこちら:
main ← Lillinator:automation-recurring-month-fix
opened 07:59AM - 12 May 26 UTC
**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`
追記: このバグは、月次実行に設定された recurring トリガーを使用するあらゆる自動化スクリプトに影響しているようです。
空のバナーが表示される現象も再現できます。例えば、開始日を 2026 年 4 月 29 日に設定すると、5 月の第 5 水曜日が見つからないため(存在しないため)、自動化はここでトリガーされません(修正前は左、修正後は右):
上記のように、週内の第 N 日が見つからない場合、バグは rails 内で NoMethodError をスローするようです。そのため、次のトリガーのバナーが空になり、実行が失敗します:
「いいね!」 2