Skip to content

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

FIX: Handle error in recurring event creation (#45083)

Martin Brennana7bb75e26612
diff --git a/plugins/discourse-events/app/models/discourse_events/events/event.rb b/plugins/discourse-events/app/models/discourse_events/events/event.rb
index af203bc1435..4a88308dbae 100644
--- a/plugins/discourse-events/app/models/discourse_events/events/event.rb
+++ b/plugins/discourse-events/app/models/discourse_events/events/event.rb
@@ -154,8 +154,10 @@ module DiscourseEvents
return unless sync_event_date
reset_invitees_for_next_occurrence
- notify_if_new_event
- publish_update!
+ DB.after_commit do
+ notify_if_new_event
+ publish_update!
+ end
end
def set_topic_bump
diff --git a/plugins/discourse-events/jobs/scheduled/monitor_event_dates.rb b/plugins/discourse-events/jobs/scheduled/monitor_event_dates.rb
index 75152b6b46f..3cacb426c99 100644
--- a/plugins/discourse-events/jobs/scheduled/monitor_event_dates.rb
+++ b/plugins/discourse-events/jobs/scheduled/monitor_event_dates.rb
@@ -43,21 +43,31 @@ module Jobs
def finish(event_date)
return if !event_date.ended?
- event_date.update!(finished_at: Time.current)
- # The occurrence goes along with the event: `set_next_recurrent_event_date` below moves
- # the event on to the next one, so it can no longer name the one that ended.
- DiscourseEvent.trigger(:discourse_post_event_event_ended, event_date.event, event_date)
- MessageBus.publish(
- "/topic/#{event_date.event.post.topic_id}",
- { reload_topic: true, refresh_stream: true },
- event_date.event.post.topic.secure_audience_publish_messages,
- )
+ # Roll back completion and successor creation together if advancement fails.
+ event_date.event.with_lock do
+ event_date.reload
+ next if event_date.finished_at || !event_date.ended?
+ event_date.update!(finished_at: Time.current)
- return if event_date.event.recurrence.blank?
- event_date.event.set_next_recurrent_event_date
- TopicTrackingState.publish_latest(event_date.event.post.topic)
- event_date.event.set_topic_bump
+ recurring = event_date.event.recurring?
+ event_date.event.set_next_recurrent_event_date if recurring
+
+ DB.after_commit do
+ # The event now points to its successor, so pass the finished occurrence explicitly.
+ DiscourseEvent.trigger(:discourse_post_event_event_ended, event_date.event, event_date)
+ MessageBus.publish(
+ "/topic/#{event_date.event.post.topic_id}",
+ { reload_topic: true, refresh_stream: true },
+ event_date.event.post.topic.secure_audience_publish_messages,
+ )
+
+ if recurring
+ TopicTrackingState.publish_latest(event_date.event.post.topic)
+ event_date.event.set_topic_bump
+ end
+ end
+ end
end
def due_reminders(event_date)
diff --git a/plugins/discourse-events/spec/jobs/scheduled/monitor_event_dates_spec.rb b/plugins/discourse-events/spec/jobs/scheduled/monitor_event_dates_spec.rb
index 9624b2429ac..1b7528c6b60 100644
--- a/plugins/discourse-events/spec/jobs/scheduled/monitor_event_dates_spec.rb
+++ b/plugins/discourse-events/spec/jobs/scheduled/monitor_event_dates_spec.rb
@@ -128,6 +128,97 @@ describe Jobs::DiscourseCalendar::MonitorEventDates do
end
describe "#finish" do
+ it "rolls back advancement without emitting side effects and advances on the next run" do
+ past_event.update!(recurrence: "every_week")
+ original_date = past_date
+ freeze_time 8.days.after
+
+ allow_any_instance_of(DiscourseEvents::Events::Event).to receive(
+ :set_next_recurrent_event_date,
+ ).and_wrap_original do |original|
+ original.call
+ raise "Advancement failed"
+ end
+
+ messages = nil
+ events =
+ DiscourseEvent.track_events do
+ messages =
+ MessageBus.track_publish do
+ expect { job.finish(original_date) }.to raise_error("Advancement failed")
+ end
+ end
+
+ expect(events).to be_empty
+ expect(messages).to be_empty
+ expect(original_date.reload.finished_at).to be_nil
+ expect(past_event.event_dates.pluck(:id)).to contain_exactly(original_date.id)
+
+ allow_any_instance_of(DiscourseEvents::Events::Event).to receive(
+ :set_next_recurrent_event_date,
+ ).and_call_original
+ events =
+ DiscourseEvent.track_events(:discourse_post_event_event_ended) do
+ job.finish(original_date)
+ job.finish(original_date)
+ end
+
+ expect(events.size).to eq(1)
+ expect(original_date.reload.finished_at).to be_present
+ expect(past_event.event_dates.pending.pluck(:starts_at)).to contain_exactly(
+ be_within_one_second_of(original_date.starts_at + 7.days),
+ )
+ end
+
+ it "defers side effects until the enclosing transaction commits" do
+ past_event.update!(recurrence: "every_week")
+ original_date = past_date
+ freeze_time 8.days.after
+
+ messages = nil
+ events =
+ DiscourseEvent.track_events do
+ messages =
+ MessageBus.track_publish do
+ DiscourseEvents::Events::Event.transaction(requires_new: true) do
+ job.finish(original_date)
+ raise ActiveRecord::Rollback
+ end
+ end
+ end
+
+ expect(events).to be_empty
+ expect(messages).to be_empty
+ expect(original_date.reload.finished_at).to be_nil
+ expect(past_event.event_dates.pluck(:id)).to contain_exactly(original_date.id)
+ end
+
+ it "keeps advancement committed when a broadcast fails without repeating the ended event" do
+ past_event.update!(recurrence: "every_week")
+ original_date = past_date
+ freeze_time 8.days.after
+
+ allow(MessageBus).to receive(
+ :publish,
+ ).and_wrap_original do |original, channel, *args, **kwargs|
+ raise "Event broadcast failed" if channel == "/topic/#{past_event.post.topic_id}"
+
+ original.call(channel, *args, **kwargs)
+ end
+
+ events =
+ DiscourseEvent.track_events(:discourse_post_event_event_ended) do
+ expect { job.finish(original_date) }.to raise_error("Event broadcast failed")
+ job.finish(original_date)
+ end
+
+ expect(events.size).to eq(1)
+ expect(original_date.reload.finished_at).to be_present
+ expect(past_event.event_dates.pending.pluck(:starts_at)).to contain_exactly(
+ be_within_one_second_of(original_date.starts_at + 7.days),
+ )
+ end
+
it "finishes past event" do
job.execute({})
expect(future_date.finished_at).to eq(nil)