Skip to content

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

FIX: Destroy pending PM after an unexpected send error (#40461)

Gabriel Grubba6e69e6029d9a
diff --git a/plugins/automation/app/jobs/scheduled/discourse_automation/tracker.rb b/plugins/automation/app/jobs/scheduled/discourse_automation/tracker.rb
index e8c47f47daa..14f2a583948 100644
--- a/plugins/automation/app/jobs/scheduled/discourse_automation/tracker.rb
+++ b/plugins/automation/app/jobs/scheduled/discourse_automation/tracker.rb
@@ -56,6 +56,7 @@ module Jobs
::DiscourseAutomation::Logger.error(
"Error sending pending PM '#{pending_pm.title}': #{e.message}",
)
+ pending_pm.destroy!
end
def run_pending_automation(pending_automation)
diff --git a/plugins/automation/spec/jobs/scheduled/discourse_automation/tracker_spec.rb b/plugins/automation/spec/jobs/scheduled/discourse_automation/tracker_spec.rb
index aca9830443c..6c31c114767 100644
--- a/plugins/automation/spec/jobs/scheduled/discourse_automation/tracker_spec.rb
+++ b/plugins/automation/spec/jobs/scheduled/discourse_automation/tracker_spec.rb
@@ -141,6 +141,34 @@ describe Jobs::DiscourseAutomation::Tracker do
end
end
+ context "when send_pm raises an unexpected error" do
+ before { pending_pm.update!(execute_at: 2.hours.ago) }
+
+ it "destroys the pending pm so it isn't retried forever" do
+ DiscourseAutomation::Scriptable::Utils.stubs(:send_pm).raises(
+ StandardError.new("unexpected error"),
+ )
+
+ expect { Jobs::DiscourseAutomation::Tracker.new.execute }.to change {
+ automation.pending_pms.count
+ }.by(-1)
+ end
+ end
+
+ context "when send_pm raises RecordNotSaved" do
+ before { pending_pm.update!(execute_at: 2.hours.ago) }
+
+ it "destroys the pending pm" do
+ DiscourseAutomation::Scriptable::Utils.stubs(:send_pm).raises(
+ ActiveRecord::RecordNotSaved.new("not saved"),
+ )
+
+ expect { Jobs::DiscourseAutomation::Tracker.new.execute }.to change {
+ automation.pending_pms.count
+ }.by(-1)
+ end
+ end
+
it "doesn't send multiple messages if the job is invoked multiple times concurrently" do
pending_pm.update!(execute_at: 1.hour.from_now)
expect do