Monthly triggered recurring automation is not running as expected

ok so Moin and i were chatting about this case today, and she brought up that perhaps there is a bug with using monthly because the length of a month varies? of course she was right - sort of… :stuck_out_tongue:

anyways, i did a little more digging :detective:

i couldn’t actually find the automation script for the schedule pm with data-explorer results in core plugins/automation/lib/discourse_automation/scripts folder so it is hiding somewhere else.

but i was looking at the logic here for recurring (presumably that script is using it) in plugins/automation/lib/discourse_automation/triggers/recurring.rb:

i think the underlying code for monthly recurring automations is calculating the Nth weekday instead of the actual calendar date and there is a logic mismatch with Calendar day - that by injecting BYDAY=#{count}#{byday} into the RRule, the automation forces a day-of-week alignment, and that it should use BYMONTHDAY instead of BYDAY.

so if you set an automation to start on April 15th (which happens to be the 3rd Wednesday of April) the system interpreted the rule as: run on the 3rd Wednesday of every month.

when May rolls around, the 3rd Wednesday is May 20th, which causes the schedule to drift by 5 days.

moreover, if you set an automation on the 30th or 31st of a month (e.g., the “5th Tuesday”), the system would look for the 5th Tuesday in the following month. if that month only had 4 Tuesdays, the automation would completely fail to find a valid date and silently skip the entire month.

i have a tested fix and am making a PR if the team wants to check it out. it changes the logic from Nth Weekday to exact Calendar Date.


here are some screenshots from my testing and debugging:

2 instances, one without a fix and the other with my logic fix

create the exact same automation in both (fixed version is on the right):

check out the differences in the banners that say " Next automation will trigger at:" messages with 5 day drift - the unpatched version is drifting to the third Wednesday (May 20th), instead of May 15th.

for example, months could get totally skipped if it was looking for the 4th Thursday instead of an actual date.

can also test the same result in the rails console

unpatched:

with fix:

PR here:

edit: bug appears to be affecting any automation script using recurring trigger set to run every month. :grimacing:

i can repro the empty banner sometimes too, for example if one sets the start date for 2025 April 29th, then it cannot find the the 5th Wednesday for May because it doesn’t exist - the automation will fail to trigger here: (unfixed is on left, fixed on right)

it looks like the bug throws a NoMethodError in rails when it can’t find the Nth day in week like above; hence empty next trigger banner and failed run:

2 Likes