Notify only about slow reports

It would be annoying to get an email for reports which display on the
screen immediately.
This commit is contained in:
Maikel Linke
2023-04-20 11:38:24 +10:00
committed by Konrad
parent 1f4af7f990
commit cf5a8a26ce
2 changed files with 23 additions and 1 deletions

View File

@@ -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

View File

@@ -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"