diff --git a/app/jobs/report_job.rb b/app/jobs/report_job.rb index a7dceb7960..f3ac9b8f6b 100644 --- a/app/jobs/report_job.rb +++ b/app/jobs/report_job.rb @@ -2,11 +2,18 @@ # Renders a report and stores it in a given blob. class ReportJob < ApplicationJob + NOTIFICATION_TIME = 5.seconds + def perform(report_class, user, params, format, blob) + start_time = Time.zone.now + report = report_class.new(user, params, render: true) result = report.render_as(format) blob.store(result) - email_result + + execution_time = Time.zone.now - start_time + + email_result if execution_time > NOTIFICATION_TIME end def email_result diff --git a/spec/jobs/report_job_spec.rb b/spec/jobs/report_job_spec.rb index a5104c5691..45b527d8f5 100644 --- a/spec/jobs/report_job_spec.rb +++ b/spec/jobs/report_job_spec.rb @@ -32,6 +32,9 @@ describe ReportJob do # Setup test data which also triggers emails: report_args + # Send emails for quick jobs as well: + stub_const("ReportJob::NOTIFICATION_TIME", 0) + expect { # We need to create this job within the block because of a bug in # rspec-rails: https://github.com/rspec/rspec-rails/issues/2668 @@ -40,6 +43,18 @@ describe ReportJob do }.to enqueue_mail(ReportMailer, :report_ready) end + it "triggers no email when the report is done quickly" do + # Setup test data which also triggers emails: + report_args + + expect { + # We need to create this job within the block because of a bug in + # rspec-rails: https://github.com/rspec/rspec-rails/issues/2668 + ReportJob.perform_later(*report_args) + perform_enqueued_jobs(only: ReportJob) + }.to_not enqueue_mail + end + def expect_csv_report blob.reload expect(blob.filename.to_s).to eq "report.csv"