mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-05 22:26:07 +00:00
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:
@@ -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
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user