A platform for community discussion. Free, open, simple.
FIX: Prevent recurring automations from getting stalled under specific conditions (#28913)
Osama Sayegh4406bbb0209c
diff --git a/plugins/automation/lib/discourse_automation/triggers/recurring.rb b/plugins/automation/lib/discourse_automation/triggers/recurring.rbindex 5962c7bfa23..f373420ad14 100644--- a/plugins/automation/lib/discourse_automation/triggers/recurring.rb+++ b/plugins/automation/lib/discourse_automation/triggers/recurring.rb@@ -65,19 +65,19 @@ module DiscourseAutomationRRule::Rule.new("FREQ=DAILY;INTERVAL=#{interval}", dtstart: start_date).between(Time.now, interval_end.days.from_now)- .first+ .find { |date| date > Time.zone.now }when "weekday"max_weekends = (interval_end.to_f / 5).ceilRRule::Rule.new("FREQ=DAILY;BYDAY=MO,TU,WE,TH,FR", dtstart: start_date).between(Time.now.end_of_day, max_weekends.weeks.from_now).drop(interval - 1)- .first+ .find { |date| date > Time.zone.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)- .first+ .find { |date| date > Time.zone.now }when "month"count = 0(start_date.beginning_of_month.to_date..start_date.end_of_month.to_date).each do |date|@@ -87,15 +87,15 @@ module DiscourseAutomationRRule::Rule.new("FREQ=MONTHLY;INTERVAL=#{interval};BYDAY=#{count}#{byday}", dtstart: start_date).between(Time.now, interval_end.months.from_now)- .first+ .find { |date| date > Time.zone.now }when "year"RRule::Rule.new("FREQ=YEARLY;INTERVAL=#{interval}", dtstart: start_date).between(Time.now, interval_end.years.from_now)- .first+ .find { |date| date > Time.zone.now }end- if next_trigger_date && next_trigger_date > Time.zone.now+ if next_trigger_dateautomation.pending_automations.create!(execute_at: next_trigger_date)elselog_debugging_info(diff --git a/plugins/automation/spec/triggers/recurring_spec.rb b/plugins/automation/spec/triggers/recurring_spec.rbindex 3d09ef5f4ed..27126fd50d2 100644--- a/plugins/automation/spec/triggers/recurring_spec.rb+++ b/plugins/automation/spec/triggers/recurring_spec.rb@@ -21,6 +21,133 @@ describe "Recurring" doexpect(triggerable.settings[DiscourseAutomation::Triggerable::MANUAL_TRIGGER_KEY]).to eq(true)end+ describe "scheduling next pending automations" do+ context "with daily frequency" do+ it "doesn't fail to schedule if the current time is within subsecond of the time component of start_date" do+ automation.upsert_field!(+ "start_date",+ "date_time",+ { value: Time.parse("2022-11-01 07:30:00 UTC") },+ target: "trigger",+ )+ automation.upsert_field!(+ "recurrence",+ "period",+ { value: { interval: 1, frequency: "day" } },+ target: "trigger",+ )++ freeze_time(Time.parse("2023-09-13 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-09-14 07:30:00 UTC"),+ )+ end+ end++ context "with weekday frequency" do+ it "doesn't fail to schedule if the current time is within subsecond of the time component of start_date" do+ automation.upsert_field!(+ "start_date",+ "date_time",+ { value: Time.parse("2024-10-01 23:59:59 UTC") },+ target: "trigger",+ )+ automation.upsert_field!(+ "recurrence",+ "period",+ { value: { interval: 1, frequency: "weekday" } },+ target: "trigger",+ )++ freeze_time(Time.parse("2024-10-01 23:59:59.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("2024-10-02 23:59:59 UTC"),+ )+ end+ end++ context "with weekly frequency" do+ it "doesn't fail to schedule if the current time is within subsecond of the time component of start_date" do+ automation.upsert_field!(+ "start_date",+ "date_time",+ { value: Time.parse("2024-09-15 23:59:59 UTC") },+ target: "trigger",+ )+ automation.upsert_field!(+ "recurrence",+ "period",+ { value: { interval: 1, frequency: "week" } },+ target: "trigger",+ )++ freeze_time(Time.parse("2024-09-15 23:59:59.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("2024-09-22 23:59:59 UTC"),+ )+ end+ end++ context "with monthly frequency" do+ it "doesn't fail to schedule if the current time is within subsecond of the time component of start_date" do+ automation.upsert_field!(+ "start_date",+ "date_time",+ { value: Time.parse("2023-10-01 07:30:00 UTC") },+ target: "trigger",+ )+ automation.upsert_field!(+ "recurrence",+ "period",+ { value: { interval: 1, frequency: "month" } },+ target: "trigger",+ )++ freeze_time(Time.parse("2023-10-01 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-11-05 07:30:00 UTC"),+ )+ end+ end++ context "with yearly frequency" do+ it "doesn't fail to schedule if the current time is within subsecond of the time component of start_date" do+ automation.upsert_field!(+ "start_date",+ "date_time",+ { value: Time.parse("2023-01-01 07:30:00 UTC") },+ target: "trigger",+ )+ automation.upsert_field!(+ "recurrence",+ "period",+ { value: { interval: 1, frequency: "year" } },+ target: "trigger",+ )++ freeze_time(Time.parse("2023-01-01 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("2024-01-01 07:30:00 UTC"),+ )+ end+ end+ end+describe "updating trigger" docontext "when date is in future" dobefore { freeze_time Time.parse("2021-06-04 10:00 UTC") }