discourse/plugins/automation/spec/triggers
Osama Sayegh 4406bbb020
FIX: Prevent recurring automations from getting stalled under specific conditions (#28913)
Under certain conditions, a recurring automation can end up in a state with no pending automation records, causing it to not execute again until manually triggered.

We use the `RRule` gem to calculate the next execution date and time for recurring automations. The gem takes the interval, frequency, start date, and a time range, and returns all dates/times within this range that meet the recurrence rule. For example:

```ruby
RRule::Rule
  .new("FREQ=DAILY;INTERVAL=1", dtstart: Time.parse("2023-01-01 07:30:00 UTC"))
  .between(Time.zone.now, Time.zone.now + 2.days)
# => [Sat, 14 Sep 2024 07:30:00.000000000 UTC +00:00, Sun, 15 Sep 2024 07:30:00.000000000 UTC +00:00]
```

However, if the time component of the first point provided to `.between()` is slightly ahead of the start date (e.g., `dtstart`), the first date/time returned by `RRule` can fall outside the specified range by the same subsecond amount. For instance:

```ruby
RRule::Rule
  .new("FREQ=DAILY;INTERVAL=1", dtstart: Time.parse("2023-01-01 07:30:00 UTC"))
  .between(Time.parse("2023-01-01 07:30:00.999 UTC"), Time.parse("2023-01-03 07:30:00 UTC"))
  .first
# => Sun, 01 Jan 2023 07:30:00.000000000 UTC +00:00
```

Here, the start date/time given to `.between()` is 999 milliseconds after 07:30:00, but the first date returned is exactly 07:30:00 without the 999 milliseconds. This causes the next recurring date to fall into the past if the automation executes within a subsecond of the start time, leading to the automation stalling.

I'm not sure why `RRule` does this, but it seems intentional judging by the source of the `.between()` method:

b9911b7147/lib/rrule/rule.rb (L28-L32)

This commit fixes the issue by selecting the first date ahead of the current time from the list returned by `RRule`, rather than the first date directly.

Internal topic: t/138045.
2024-09-16 18:23:26 +03:00
..
after_post_cook_spec.rb DEV: Convert some files to autoloading and various improvements (#26860) 2024-05-06 23:12:55 +03:00
category_created_edited_spec.rb DEV: Convert some files to autoloading and various improvements (#26860) 2024-05-06 23:12:55 +03:00
pm_created_spec.rb UX: Use a dropdown for SSL mode for group SMTP (#27932) 2024-07-18 10:33:14 +10:00
point_in_time_spec.rb DEV: Convert some files to autoloading and various improvements (#26860) 2024-05-06 23:12:55 +03:00
post_created_edited_spec.rb FIX: supports groups field in post_created_edited (#28783) 2024-09-06 17:22:42 +02:00
recurring_spec.rb FIX: Prevent recurring automations from getting stalled under specific conditions (#28913) 2024-09-16 18:23:26 +03:00
stalled_wiki_spec.rb DEV: Catch missing translations during test runs (#26258) 2024-05-24 22:15:53 +08:00
topic_spec.rb DEV: Move array type custom fields to JSON type in automation (#26939) 2024-05-10 18:47:12 +03:00
topic_tags_changed_spec.rb FEATURE: Add user to topic_tags_changed event (#28714) 2024-09-06 11:23:30 -03:00
user_added_to_group_spec.rb DEV: Convert some files to autoloading and various improvements (#26860) 2024-05-06 23:12:55 +03:00
user_badge_granted_spec.rb DEV: Convert some files to autoloading and various improvements (#26860) 2024-05-06 23:12:55 +03:00
user_first_logged_in_spec.rb DEV: Convert some files to autoloading and various improvements (#26860) 2024-05-06 23:12:55 +03:00
user_promoted_spec.rb DEV: Convert some files to autoloading and various improvements (#26860) 2024-05-06 23:12:55 +03:00
user_removed_from_group_spec.rb DEV: Convert some files to autoloading and various improvements (#26860) 2024-05-06 23:12:55 +03:00
user_updated_spec.rb DEV: Move array type custom fields to JSON type in automation (#26939) 2024-05-10 18:47:12 +03:00