A platform for community discussion. Free, open, simple.
FIX: logs time even when automation raises (#32254)
Joffrey JAFFEUX01ce003b8eb3
diff --git a/plugins/automation/app/models/discourse_automation/stat.rb b/plugins/automation/app/models/discourse_automation/stat.rbindex 9b85ba8c2b7..8f81bc8dea3 100644--- a/plugins/automation/app/models/discourse_automation/stat.rb+++ b/plugins/automation/app/models/discourse_automation/stat.rb@@ -7,12 +7,17 @@ module DiscourseAutomationdef self.log(automation_id, run_time = nil)if block_given? && run_time.nil?start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)- result = yield- run_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time- result+ begin+ result = yield+ run_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time+ result+ rescue => e+ run_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time+ raise e+ endendensure- update_stats(automation_id, run_time)+ update_stats(automation_id, run_time || 0)enddef self.fetch_period_summariesdiff --git a/plugins/automation/spec/models/stat_spec.rb b/plugins/automation/spec/models/stat_spec.rbindex 4cda513c725..f221db60952 100644--- a/plugins/automation/spec/models/stat_spec.rb+++ b/plugins/automation/spec/models/stat_spec.rb@@ -159,6 +159,23 @@ RSpec.describe DiscourseAutomation::Stat doexpect(stat.max_run_time).to eq(0.75)expect(stat.total_runs).to eq(1)end++ context "when an error occurs" do+ it "yields the correct error and records it" do+ allow(Process).to receive(:clock_gettime).and_return(10, 10.75)++ expect { DiscourseAutomation::Stat.log(automation_id) { raise } }.to raise_error(+ RuntimeError,+ )++ stat = DiscourseAutomation::Stat.find_by(automation_id: automation_id)+ expect(stat.total_time).to eq(0.75)+ expect(stat.average_run_time).to eq(0.75)+ expect(stat.min_run_time).to eq(0.75)+ expect(stat.max_run_time).to eq(0.75)+ expect(stat.total_runs).to eq(1)+ end+ endendcontext "with direct call form" do