From f61a2fa2bf7356acfbcf9b98e7779d322060bf85 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Fri, 6 Oct 2023 16:27:15 +1100 Subject: [PATCH] Rescue errors in report generation So we can report them to Bugsnag and the job isn't marked as failed and therefore won't repeat. --- app/jobs/report_job.rb | 6 ++++++ spec/jobs/report_job_spec.rb | 11 +++++++++++ 2 files changed, 17 insertions(+) diff --git a/app/jobs/report_job.rb b/app/jobs/report_job.rb index 73184ae574..38b7358c1f 100644 --- a/app/jobs/report_job.rb +++ b/app/jobs/report_job.rb @@ -21,6 +21,12 @@ class ReportJob < ApplicationJob email_result(user, blob) if execution_time > NOTIFICATION_TIME broadcast_result(channel, format, blob) if channel + rescue StandardError => e + Bugsnag.notify(e) do |payload| + payload.add_metadata :report, { + report_class:, user:, params:, format: + } + end end def email_result(user, blob) diff --git a/spec/jobs/report_job_spec.rb b/spec/jobs/report_job_spec.rb index b8326634b0..c99f5fc38a 100644 --- a/spec/jobs/report_job_spec.rb +++ b/spec/jobs/report_job_spec.rb @@ -64,6 +64,17 @@ describe ReportJob do }.to_not enqueue_mail end + it "rescues errors" do + expect(report_class).to receive(:new).and_raise + expect(Bugsnag).to receive(:notify) + + job = ReportJob.perform_later(**report_args) + + expect { + perform_enqueued_jobs(only: ReportJob) + }.to_not raise_error + end + def expect_csv_report blob.reload expect(blob.filename.to_s).to eq "report.csv"