Skip to content

A platform for community discussion. Free, open, simple.

FIX: Automation Plugin - Monthly recurring triggers shifting dates and skipping months (BYDAY to BYMONTHDAY) (#40747)

Krisde8f59edf5ef
diff --git a/plugins/automation/config/locales/server.en.yml b/plugins/automation/config/locales/server.en.yml
index 541ee3a8577..8cbfda4a0ca 100644
--- a/plugins/automation/config/locales/server.en.yml
+++ b/plugins/automation/config/locales/server.en.yml
@@ -40,6 +40,7 @@ en:
description: When a category is created or edited the automation will be triggered
recurring:
title: Recurring
+ invalid_interval: The interval must be a positive number.
point_in_time:
title: Point in time
description: When the date is reached the automation will be triggered
diff --git a/plugins/automation/lib/discourse_automation/triggers/recurring.rb b/plugins/automation/lib/discourse_automation/triggers/recurring.rb
index 38a7a4509d5..c3deddebffc 100644
--- a/plugins/automation/lib/discourse_automation/triggers/recurring.rb
+++ b/plugins/automation/lib/discourse_automation/triggers/recurring.rb
@@ -46,53 +46,51 @@ module DiscourseAutomation
end
start_date = Time.parse(start_date)
- if start_date > Time.zone.now
+ now = Time.zone.now
+ interval = interval.to_i
+
+ if start_date > now
automation.pending_automations.create!(execute_at: start_date)
return
end
byday = start_date.strftime("%A").upcase[0, 2]
- interval = interval.to_i
interval_end = interval + 1
next_trigger_date =
case frequency
when "minute"
- (Time.zone.now + interval.minute).beginning_of_minute
+ (now + interval.minute).beginning_of_minute
when "hour"
- (Time.zone.now + interval.hour).beginning_of_hour
+ (now + interval.hour).beginning_of_hour
when "day"
RRule::Rule
.new("FREQ=DAILY;INTERVAL=#{interval}", dtstart: start_date)
- .between(Time.now, interval_end.days.from_now)
- .find { |date| date > Time.zone.now }
+ .between(now, now + interval_end.days)
+ .find { |date| date > now }
when "weekday"
max_weekends = (interval_end.to_f / 5).ceil
RRule::Rule
.new("FREQ=DAILY;BYDAY=MO,TU,WE,TH,FR", dtstart: start_date)
- .between(Time.now.end_of_day, max_weekends.weeks.from_now)
+ .between(now.end_of_day, now + max_weekends.weeks)
.drop(interval - 1)
- .find { |date| date > Time.zone.now }
+ .find { |date| date > now }
when "week"
RRule::Rule
.new("FREQ=WEEKLY;INTERVAL=#{interval};BYDAY=#{byday}", dtstart: start_date)
- .between(Time.now.end_of_week, interval_end.weeks.from_now)
- .find { |date| date > Time.zone.now }
+ .between(now.end_of_week, now + interval_end.weeks)
+ .find { |date| date > now }
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 }
+ months_elapsed = (now.year - start_date.year) * 12 + (now.month - start_date.month)
+ steps = (months_elapsed.to_f / interval).ceil * interval
+ next_date = start_date + steps.months
+ next_date = start_date + (steps += interval).months while next_date <= now
+ next_date
when "year"
RRule::Rule
.new("FREQ=YEARLY;INTERVAL=#{interval}", dtstart: start_date)
- .between(Time.now, interval_end.years.from_now)
- .find { |date| date > Time.zone.now }
+ .between(now, now + interval_end.years)
+ .find { |date| date > now }
end
if next_trigger_date
@@ -109,7 +107,7 @@ module DiscourseAutomation
byday:,
interval_end:,
next_trigger_date:,
- now: Time.zone.now,
+ now:,
)
nil
end
@@ -131,7 +129,12 @@ DiscourseAutomation::Triggerable.add(DiscourseAutomation::Triggers::RECURRING) d
extra: {
content: DiscourseAutomation::Triggers::Recurring::RECURRENCE_CHOICES,
},
- required: true
+ required: true,
+ validator: ->(value) do
+ if value && !value["interval"].to_i.positive?
+ I18n.t("discourse_automation.triggerables.recurring.invalid_interval")
+ end
+ end
field :start_date, component: :date_time, required: true
on_update do |automation, fields, previous_fields|
diff --git a/plugins/automation/spec/triggers/recurring_spec.rb b/plugins/automation/spec/triggers/recurring_spec.rb
index ff603163cee..42ff82c5f74 100644
--- a/plugins/automation/spec/triggers/recurring_spec.rb
+++ b/plugins/automation/spec/triggers/recurring_spec.rb
@@ -117,7 +117,124 @@ describe "Recurring" do
pending_automations = automation.reload.pending_automations
expect(pending_automations.count).to eq(1)
expect(pending_automations.first.execute_at).to eq_time(
- Time.parse("2023-11-05 07:30:00 UTC"),
+ Time.parse("2023-11-01 07:30:00 UTC"),
+ )
+ end
+
+ it "clamps to the end of the month if the target month is too short" do
+ automation.upsert_field!(
+ "start_date",
+ "date_time",
+ { value: Time.parse("2023-01-31 07:30:00 UTC") },
+ target: "trigger",
+ )
+ automation.upsert_field!(
+ "recurrence",
+ "period",
+ { value: { interval: 1, frequency: "month" } },
+ target: "trigger",
+ )
+
+ freeze_time(Time.parse("2023-01-31 07:30:00.141775363 UTC")) { automation.trigger! }
+
+ pending_automations = automation.reload.pending_automations
+ expect(pending_automations.first.execute_at).to eq_time(
+ Time.parse("2023-02-28 07:30:00 UTC"),
+ )
+ end
+
+ it "rejects a zero interval" do
+ expect {
+ automation.upsert_field!(
+ "recurrence",
+ "period",
+ { value: { interval: "0", frequency: "month" } },
+ target: "trigger",
+ )
+ }.to raise_error(ActiveRecord::RecordInvalid)
+ end
+
+ it "rejects a negative interval" do
+ expect {
+ automation.upsert_field!(
+ "recurrence",
+ "period",
+ { value: { interval: "-1", frequency: "month" } },
+ target: "trigger",
+ )
+ }.to raise_error(ActiveRecord::RecordInvalid)
+ end
+
+ it "preserves the selected day when it is the last day of February" do
+ automation.upsert_field!(
+ "start_date",
+ "date_time",
+ { value: Time.parse("2023-02-28 07:30:00 UTC") },
+ target: "trigger",
+ )
+ automation.upsert_field!(
+ "recurrence",
+ "period",
+ { value: { interval: 1, frequency: "month" } },
+ target: "trigger",
+ )
+
+ freeze_time(Time.parse("2023-02-28 07:30:00.141775363 UTC")) { automation.trigger! }
+
+ pending_automations = automation.reload.pending_automations
+ expect(pending_automations.first.execute_at).to eq_time(
+ Time.parse("2023-03-28 07:30:00 UTC"),
+ )
+ end
+
+ it "preserves the selected day when it is the last day of April" do
+ automation.upsert_field!(
+ "start_date",
+ "date_time",
+ { value: Time.parse("2024-04-30 07:30:00 UTC") },
+ target: "trigger",
+ )
+ automation.upsert_field!(
+ "recurrence",
+ "period",
+ { value: { interval: 1, frequency: "month" } },
+ target: "trigger",
+ )
+
+ freeze_time(Time.parse("2024-04-30 07:30:00.141775363 UTC")) { automation.trigger! }
+
+ pending_automations = automation.reload.pending_automations
+ expect(pending_automations.first.execute_at).to eq_time(
+ Time.parse("2024-05-30 07:30:00 UTC"),
+ )
+ end
+
+ it "preserves the original day across a real clamped execution" do
+ automation.upsert_field!(
+ "start_date",
+ "date_time",
+ { value: Time.parse("2023-01-31 07:30:00 UTC") },
+ target: "trigger",
+ )
+ automation.upsert_field!(
+ "recurrence",
+ "period",
+ { value: { interval: 1, frequency: "month" } },
+ target: "trigger",
+ )
+
+ freeze_time(Time.parse("2023-01-31 07:30:00.141775363 UTC")) { automation.trigger! }
+ pending_automations = automation.reload.pending_automations
+ expect(pending_automations.count).to eq(1)
+ expect(pending_automations.first.execute_at).to eq_time(
+ Time.parse("2023-02-28 07:30:00 UTC"),
+ )
+
+ freeze_time(Time.parse("2023-02-28 07:30:00.141775363 UTC")) { automation.trigger! }
+ pending_automations = automation.reload.pending_automations
+ expect(pending_automations.count).to eq(1)
+ expect(pending_automations.first.execute_at).to eq_time(
+ Time.parse("2023-03-31 07:30:00 UTC"),
)
end
end
@@ -358,7 +475,7 @@ describe "Recurring" do
pending_automation = automation.pending_automations.last
expect(pending_automation.execute_at).to be_within_one_minute_of(
- Time.parse("2021-07-02 08:00:00 UTC"),
+ Time.parse("2021-07-04 08:00:00 UTC"),
)
end
end