Create report file where the content is generated

In the past, we needed the report blob to know when the report has been
finished and uploaded. But not we use cable_ready to notify when the
report is done and we don't need the blob in the controller.
This commit is contained in:
Maikel Linke
2023-10-12 17:21:18 +11:00
committed by Konrad
parent 3f4ef3e48b
commit ff6bcb113f
3 changed files with 15 additions and 15 deletions

View File

@@ -80,11 +80,10 @@ module Admin
block: "start"
).broadcast
blob = ReportBlob.create_for_upload_later!(report_filename)
ReportJob.perform_later(
report_class:, user: spree_current_user, params:,
format:, blob:, channel: ScopedChannel.for_id(params[:uuid]),
format:, filename: report_filename,
channel: ScopedChannel.for_id(params[:uuid]),
)
head :no_content

View File

@@ -9,11 +9,12 @@ class ReportJob < ApplicationJob
NOTIFICATION_TIME = 5.seconds
def perform(report_class:, user:, params:, format:, blob:, channel: nil)
def perform(report_class:, user:, params:, format:, filename:, channel: nil)
start_time = Time.zone.now
report = report_class.new(user, params, render: true)
result = report.render_as(format)
blob = ReportBlob.create_for_upload_later!(filename)
blob.store(result)
execution_time = Time.zone.now - start_time

View File

@@ -4,15 +4,14 @@ require 'spec_helper'
describe ReportJob do
let(:report_args) {
{ report_class:, user:, params:, format:,
blob: }
{ report_class:, user:, params:, format:, filename: }
}
let(:report_class) { Reporting::Reports::UsersAndEnterprises::Base }
let(:user) { enterprise.owner }
let(:enterprise) { create(:enterprise) }
let(:params) { {} }
let(:format) { :csv }
let(:blob) { ReportBlob.create_for_upload_later!("report.csv") }
let(:filename) { "report.csv" }
it "generates a report" do
job = perform_enqueued_jobs(only: ReportJob) do
@@ -22,12 +21,14 @@ describe ReportJob do
end
it "enqueues a job for async processing" do
job = ReportJob.perform_later(**report_args)
expect(blob.content_stored?).to eq false
expect {
ReportJob.perform_later(**report_args)
}.to_not change { ActiveStorage::Blob.count }
perform_enqueued_jobs(only: ReportJob)
expect {
perform_enqueued_jobs(only: ReportJob)
}.to change { ActiveStorage::Blob.count }
expect(blob.content_stored?).to eq true
expect_csv_report
end
@@ -44,10 +45,9 @@ describe ReportJob do
ReportJob.perform_later(**report_args)
perform_enqueued_jobs(only: ReportJob)
}.to enqueue_mail(ReportMailer, :report_ready).with(
params: {
params: hash_including(
to: user.email,
blob:,
},
),
args: [],
)
end
@@ -76,7 +76,7 @@ describe ReportJob do
end
def expect_csv_report
blob.reload
blob = ReportBlob.last
expect(blob.filename.to_s).to eq "report.csv"
expect(blob.content_type).to eq "text/csv"