Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Retire the state machines on Import and ImportRow

Damian Legawiec0ba1aaa0a6da
diff --git a/spree/api/app/controllers/spree/api/v3/admin/imports_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/imports_controller.rb
index 10188af415..0d62b1c27f 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/imports_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/imports_controller.rb
@@ -29,7 +29,7 @@ module Spree
if @resource.save
begin
- @resource.start_mapping!
+ Spree.import_start_mapping_workflow.call(import: @resource)
rescue ::CSV::MalformedCSVError, EncodingError => e
@resource.update_columns(status: 'failed', processing_errors: e.message, updated_at: Time.current)
return render_error(
@@ -72,7 +72,7 @@ module Spree
apply_mappings!(@resource)
if @resource.mapping_done?
- @resource.complete_mapping!
+ Spree.import_complete_mapping_workflow.call(import: @resource)
render json: serialize_resource(@resource)
else
missing = @resource.required_fields - @resource.mappings.mapped.pluck(:schema_field)
@@ -96,7 +96,9 @@ module Spree
@resource = find_resource
authorize_resource!(@resource, :update)
- if @resource.retry_failed_rows
+ result = Spree.import_retry_failed_rows_workflow.call(import: @resource)
+
+ if result.success?
render json: serialize_resource(@resource)
else
render_error(
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/imports_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/imports_controller_spec.rb
index e56346fdef..af20db7505 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/imports_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/imports_controller_spec.rb
@@ -204,7 +204,7 @@ RSpec.describe Spree::Api::V3::Admin::ImportsController, type: :controller do
imp.attachment.attach(
io: StringIO.new(csv_content), filename: 'import.csv', content_type: 'text/csv'
)
- imp.start_mapping!
+ Spree.import_start_mapping_workflow.call(import: imp)
end
end
diff --git a/spree/api/spec/integration/spree/api/v3/admin/imports_spec.rb b/spree/api/spec/integration/spree/api/v3/admin/imports_spec.rb
index df46337bba..84ce045d20 100644
--- a/spree/api/spec/integration/spree/api/v3/admin/imports_spec.rb
+++ b/spree/api/spec/integration/spree/api/v3/admin/imports_spec.rb
@@ -252,7 +252,7 @@ RSpec.describe 'Admin Imports API', type: :request, swagger_doc: 'api-reference/
path '/api/v3/admin/imports/{id}/complete_mapping' do
let(:import_in_mapping) do
- create(:product_import, owner: store, user: admin_user).tap(&:start_mapping!)
+ create(:product_import, owner: store, user: admin_user).tap { |imp| Spree.import_start_mapping_workflow.call(import: imp) }
end
let(:id) { import_in_mapping.prefixed_id }
diff --git a/spree/core/app/jobs/spree/imports/process_group_job.rb b/spree/core/app/jobs/spree/imports/process_group_job.rb
index 52e653ceb5..708210a449 100644
--- a/spree/core/app/jobs/spree/imports/process_group_job.rb
+++ b/spree/core/app/jobs/spree/imports/process_group_job.rb
@@ -115,7 +115,7 @@ module Spree
import.reload
if import.completed_groups_count >= import.processing_groups_count && import.rows.in_flight.none?
- import.complete! if import.status == 'processing'
+ Spree.import_complete_workflow.call(import: import) if import.status == 'processing'
elsif large && (import.completed_groups_count % 10).zero?
import.publish_event('import.progress')
end
diff --git a/spree/core/app/jobs/spree/imports/process_job.rb b/spree/core/app/jobs/spree/imports/process_job.rb
index 581e0c7464..801954ddf6 100644
--- a/spree/core/app/jobs/spree/imports/process_job.rb
+++ b/spree/core/app/jobs/spree/imports/process_job.rb
@@ -25,7 +25,7 @@ module Spree
unless skip_row_creation
step :begin_processing do
- @import.start_processing! if @import.status != 'processing'
+ Spree.import_start_processing_workflow.call(import: @import) if @import.status != 'processing'
end
step :create_rows, start: 1
# A permanent CSV failure marked the import failed inside the step
diff --git a/spree/core/app/models/spree/import.rb b/spree/core/app/models/spree/import.rb
index 47b9c80fbe..da03100d41 100644
--- a/spree/core/app/models/spree/import.rb
+++ b/spree/core/app/models/spree/import.rb
@@ -56,40 +56,14 @@ module Spree
has_one_attached :attachment, service: Spree.private_storage_service_name
#
- # State machine
+ # Status
#
- state_machine initial: :pending, attribute: :status do
- event :start_mapping do
- transition to: :mapping
- end
- before_transition to: :mapping, do: :create_mappings
-
- event :complete_mapping do
- transition from: :mapping, to: :completed_mapping
- end
- after_transition to: :completed_mapping, do: :create_rows_async
-
- event :start_processing do
- transition from: :completed_mapping, to: :processing
- end
-
- event :complete do
- transition from: :processing, to: :completed
- end
- after_transition to: :completed, do: :touch_store
- after_transition to: :completed, do: :publish_import_completed_event
-
- event :fail do
- transition to: :failed
- end
-
- # Re-processes rows that failed: the dispatcher targets pending_and_failed
- # rows, so retry is just a transition + re-dispatch.
- event :retry_failed_rows do
- transition from: :completed, to: :processing, if: ->(import) { import.rows.failed.exists? }
- end
- after_transition on: :retry_failed_rows, do: :process_rows_async
- end
+ # No state machine — the pipeline's steps are the Spree::Imports workflows
+ # (docs/plans/6.0-service-workflows.md), which is also what let the row
+ # dispatch stop tiptoeing around a transition's transaction.
+ include Spree::HasStatus
+ has_status :pending, :mapping, :completed_mapping, :processing, :completed, :failed,
+ default: :pending
#
# Preferences
@@ -162,6 +136,44 @@ module Spree
nil
end
+ # @deprecated Call the Spree::Imports workflows — removed in 6.1.
+ def start_mapping!
+ Spree::Deprecation.warn('Spree::Import#start_mapping! is deprecated and will be removed in Spree 6.1. Call Spree.import_start_mapping_workflow instead.')
+ run_pipeline_workflow(Spree.import_start_mapping_workflow)
+ end
+
+ # @deprecated Call Spree.import_complete_mapping_workflow — removed in 6.1.
+ def complete_mapping!
+ Spree::Deprecation.warn('Spree::Import#complete_mapping! is deprecated and will be removed in Spree 6.1. Call Spree.import_complete_mapping_workflow instead.')
+ run_pipeline_workflow(Spree.import_complete_mapping_workflow)
+ end
+
+ # @deprecated Call Spree.import_start_processing_workflow — removed in 6.1.
+ def start_processing!
+ Spree::Deprecation.warn('Spree::Import#start_processing! is deprecated and will be removed in Spree 6.1. Call Spree.import_start_processing_workflow instead.')
+ run_pipeline_workflow(Spree.import_start_processing_workflow)
+ end
+
+ # @deprecated Call Spree.import_complete_workflow — removed in 6.1.
+ def complete!
+ Spree::Deprecation.warn('Spree::Import#complete! is deprecated and will be removed in Spree 6.1. Call Spree.import_complete_workflow instead.')
+ run_pipeline_workflow(Spree.import_complete_workflow)
+ end
+
+ # @deprecated Set the status — removed in 6.1. Failure was never a step of
+ # its own; the controller records the parse error directly.
+ def fail!
+ Spree::Deprecation.warn('Spree::Import#fail! is deprecated and will be removed in Spree 6.1. Write the status instead.')
+ update!(status: 'failed')
+ end
+
+ # @deprecated Call Spree.import_retry_failed_rows_workflow — removed in 6.1.
+ # Returns false when there is nothing to retry, as the guarded event did.
+ def retry_failed_rows
+ Spree::Deprecation.warn('Spree::Import#retry_failed_rows is deprecated and will be removed in Spree 6.1. Call Spree.import_retry_failed_rows_workflow instead.')
+ Spree.import_retry_failed_rows_workflow.call(import: self).success?
+ end
+
# Returns true if the import is in mapping state
# @return [Boolean]
def mapping?
@@ -305,14 +317,6 @@ module Spree
"#{Spree.t(type.demodulize.pluralize.downcase)} #{number}"
end
- def touch_store
- store.touch
- end
-
- def publish_import_completed_event
- publish_event('import.completed')
- end
-
# Returns the headers of the csv file
# @return [Array<String>]
def csv_headers
@@ -330,43 +334,6 @@ module Spree
@attachment_file_content ||= attachment.attached? ? attachment.blob.download&.force_encoding('UTF-8') : nil
end
- # Creates mappings from the schema fields
- # TODO: get mappings from the previous import if it exists, so user won't have to map the same columns again
- def create_mappings
- schema_fields.each do |schema_field|
- mapping = mappings.find_or_create_by!(schema_field: schema_field[:name])
- mapping.try_to_auto_assign_file_column(csv_headers)
- mapping.save!
- end
- end
-
- # Creates rows asynchronously
- # @return [void]
- def create_rows_async
- if preferred_inline
- # The transition callback runs inside the state machine's
- # transaction and Continuations forbid checkpoints there — the
- # inline run starts at commit, still before the transition call
- # returns to the caller.
- ActiveRecord.after_all_transactions_commit { Spree::Imports::ProcessJob.perform_now(id) }
- return
- end
-
- # The 2s delay lets the attachment settle before the job reads it.
- Spree::Imports::ProcessJob.set(wait: 2.seconds).perform_later(id)
- end
-
- # Processes rows asynchronously
- # @return [void]
- def process_rows_async
- if preferred_inline
- ActiveRecord.after_all_transactions_commit { Spree::Imports::ProcessJob.perform_now(id, skip_row_creation: true) }
- return
- end
-
- Spree::Imports::ProcessJob.perform_later(id, skip_row_creation: true)
- end
-
# Returns the store for the import
# @return [Spree::Store]
def store
@@ -473,6 +440,18 @@ module Spree
private
+ # Mirrors the machine's bang events: an illegal move raised.
+ def run_pipeline_workflow(workflow)
+ result = workflow.call(import: self)
+
+ if result.failure?
+ errors.add(:base, result.error.value.to_s)
+ raise ActiveRecord::RecordInvalid, self
+ end
+
+ true
+ end
+
def ensure_whitelisted_type
return if type.blank?
diff --git a/spree/core/app/models/spree/import_row.rb b/spree/core/app/models/spree/import_row.rb
index 9760a8c42d..61849ebfd1 100644
--- a/spree/core/app/models/spree/import_row.rb
+++ b/spree/core/app/models/spree/import_row.rb
@@ -24,23 +24,13 @@ module Spree
self.whitelisted_ransackable_attributes = %w[status row_number]
#
- # State machine
+ # Status
#
- state_machine initial: :pending, attribute: :status do
- event :start_processing do
- transition to: :processing
- end
-
- event :fail do
- transition to: :failed
- end
- after_transition to: :failed, do: :publish_import_row_failed_event
-
- event :complete do
- transition to: :completed
- end
- after_transition to: :completed, do: :publish_import_row_completed_event
- end
+ # No state machine — a row's whole lifecycle already lives in +process!+,
+ # which is the only thing that moves it. The events publish from the write
+ # rather than a transition callback.
+ include Spree::HasStatus
+ has_status :pending, :processing, :completed, :failed, default: :pending
# How long a row may sit in `processing` before we consider its owning worker dead
# (OOM, SIGKILL, deploy without graceful drain — none of these trigger a Sidekiq
@@ -90,15 +80,40 @@ module Spree
data_json[mapping.file_column]
end
+ # @deprecated Write the status — removed in 6.1. A row's lifecycle belongs
+ # to +process!+, which is the only thing that should move it.
+ def start_processing!
+ Spree::Deprecation.warn('Spree::ImportRow#start_processing! is deprecated and will be removed in Spree 6.1. Use #process! instead.')
+ update!(status: 'processing')
+ end
+
+ # @deprecated Write the status — removed in 6.1.
+ def complete!
+ Spree::Deprecation.warn('Spree::ImportRow#complete! is deprecated and will be removed in Spree 6.1. Use #process! instead.')
+ update!(status: 'completed')
+ publish_event('import_row.completed')
+ true
+ end
+
+ # @deprecated Write the status — removed in 6.1.
+ def fail!
+ Spree::Deprecation.warn('Spree::ImportRow#fail! is deprecated and will be removed in Spree 6.1. Use #process! instead.')
+ update!(status: 'failed')
+ publish_event('import_row.failed')
+ true
+ end
+
def process!(mappings: nil, schema_fields: nil)
- start_processing!
+ update!(status: 'processing')
self.item = import.row_processor_class.new(self, mappings: mappings, schema_fields: schema_fields).process!
self.validation_errors = nil # clear a stale error when a retried row succeeds
- complete!
+ update!(status: 'completed')
+ publish_event('import_row.completed')
rescue StandardError => e
Rails.error.report(e, handled: true, context: { import_row_id: id }, source: 'spree.core')
self.validation_errors = e.message
- fail!
+ update!(status: 'failed')
+ publish_event('import_row.failed')
end
# Bulk processing mode for large imports.
@@ -113,12 +128,5 @@ module Spree
update_columns(status: 'failed', validation_errors: e.message, updated_at: Time.current)
end
- def publish_import_row_completed_event
- publish_event('import_row.completed')
- end
-
- def publish_import_row_failed_event
- publish_event('import_row.failed')
- end
end
end
diff --git a/spree/core/app/services/spree/sample_data/import_runner.rb b/spree/core/app/services/spree/sample_data/import_runner.rb
index 5917bec897..2a14840d7d 100644
--- a/spree/core/app/services/spree/sample_data/import_runner.rb
+++ b/spree/core/app/services/spree/sample_data/import_runner.rb
@@ -25,8 +25,8 @@ module Spree
inline: inline, skip_events: skip_events
)
- import.start_mapping
- import.complete_mapping
+ Spree.import_start_mapping_workflow.call(import: import)
+ Spree.import_complete_mapping_workflow.call(import: import)
success(import.reload)
end
diff --git a/spree/core/lib/spree/testing_support/imports.rb b/spree/core/lib/spree/testing_support/imports.rb
new file mode 100644
index 0000000000..c1540258a8
--- /dev/null
+++ b/spree/core/lib/spree/testing_support/imports.rb
@@ -0,0 +1,16 @@
+module Spree
+ module TestingSupport
+ module Imports
+ # Builds an import's column mappings without moving it out of whatever
+ # status the example put it in. Spree::Imports::StartMapping does both,
+ # which is right for the pipeline but wrong for a spec that starts an
+ # import mid-flight.
+ def seed_import_mappings(import)
+ status_before = import.status
+ Spree.import_start_mapping_workflow.call(import: import)
+ import.update_columns(status: status_before, updated_at: Time.current)
+ import.reload
+ end
+ end
+ end
+end
diff --git a/spree/core/spec/jobs/spree/imports/process_group_job_spec.rb b/spree/core/spec/jobs/spree/imports/process_group_job_spec.rb
index edeb20b166..7782f1c375 100644
--- a/spree/core/spec/jobs/spree/imports/process_group_job_spec.rb
+++ b/spree/core/spec/jobs/spree/imports/process_group_job_spec.rb
@@ -15,7 +15,7 @@ RSpec.describe Spree::Imports::ProcessGroupJob, type: :job do
before do
allow_any_instance_of(Spree::Import).to receive(:csv_headers).and_return(['slug', 'sku', 'name', 'price'])
- import.create_mappings
+ seed_import_mappings(import)
end
it 'sets Spree::Current.store from the import' do
@@ -225,7 +225,7 @@ RSpec.describe Spree::Imports::ProcessGroupJob, type: :job do
before do
allow_any_instance_of(Spree::Import).to receive(:csv_headers).and_return(['slug', 'sku', 'name', 'price', 'option1_name', 'option1_value'])
import.mappings.destroy_all
- import.create_mappings
+ seed_import_mappings(import)
end
it 'creates product and variant in row_number order' do
@@ -270,7 +270,7 @@ RSpec.describe Spree::Imports::ProcessGroupJob, type: :job do
before do
allow_any_instance_of(Spree::Import).to receive(:csv_headers).and_return(['slug', 'sku', 'name', 'price', 'option1_name', 'option1_value'])
import.mappings.destroy_all
- import.create_mappings
+ seed_import_mappings(import)
end
it 'creates product and variant via bulk_process!' do
diff --git a/spree/core/spec/jobs/spree/imports/process_job_spec.rb b/spree/core/spec/jobs/spree/imports/process_job_spec.rb
index 0f9f844907..649f410f40 100644
--- a/spree/core/spec/jobs/spree/imports/process_job_spec.rb
+++ b/spree/core/spec/jobs/spree/imports/process_job_spec.rb
@@ -98,7 +98,7 @@ RSpec.describe Spree::Imports::ProcessJob, type: :job do
before do
allow_any_instance_of(Spree::Import).to receive(:csv_headers).and_return(['slug', 'sku', 'name', 'price', 'option1_name', 'option1_value'])
- import.create_mappings
+ seed_import_mappings(import)
end
let!(:product_row) do
diff --git a/spree/core/spec/models/spree/import_row_spec.rb b/spree/core/spec/models/spree/import_row_spec.rb
index 0a324826c4..df94277cf4 100644
--- a/spree/core/spec/models/spree/import_row_spec.rb
+++ b/spree/core/spec/models/spree/import_row_spec.rb
@@ -39,47 +39,20 @@ RSpec.describe Spree::ImportRow, :job, type: :model do
end
end
- describe 'State machine' do
+ describe 'status' do
before { import_row.save! }
- describe 'initial state' do
- it 'starts in pending state' do
- expect(import_row.status).to eq('pending')
- end
+ it 'has no state machine' do
+ expect(described_class).not_to respond_to(:state_machines)
end
- describe 'start_processing event' do
- it 'transitions from pending to processing' do
- expect { import_row.start_processing! }.to change(import_row, :status).from('pending').to('processing')
- end
- end
-
- describe 'fail event' do
- before { import_row.start_processing! }
-
- it 'transitions from processing to failed' do
- expect { import_row.fail! }.to change(import_row, :status).from('processing').to('failed')
- end
-
- it 'publishes import_row.fail event' do
- expect(import_row).to receive(:publish_import_row_failed_event)
- import_row.fail!
- end
-
+ it 'starts in pending status' do
+ expect(import_row.status).to eq('pending')
end
- describe 'complete event' do
- before { import_row.start_processing! }
-
- it 'transitions from processing to completed' do
- expect { import_row.complete! }.to change(import_row, :status).from('processing').to('completed')
- end
-
- it 'publishes import_row.complete event' do
- expect(import_row).to receive(:publish_import_row_completed_event)
- import_row.complete!
- end
-
+ it 'rejects an unknown status' do
+ import_row.status = 'nonsense'
+ expect(import_row).not_to be_valid
end
end
@@ -145,7 +118,7 @@ RSpec.describe Spree::ImportRow, :job, type: :model do
describe '#to_schema_hash' do
before do
- import.create_mappings
+ Spree.import_start_mapping_workflow.call(import: import)
end
it 'returns attributes mapped to schema fields' do
@@ -157,7 +130,7 @@ RSpec.describe Spree::ImportRow, :job, type: :model do
describe '#attribute_by_schema_field' do
before do
- import.create_mappings
+ Spree.import_start_mapping_workflow.call(import: import)
end
it 'returns the mapped attribute value' do
@@ -201,6 +174,13 @@ RSpec.describe Spree::ImportRow, :job, type: :model do
expect { import_row.process! }.to change(import_row, :status).from('pending').to('failed')
end
+ it 'publishes import_row.failed', events: true do
+ allow(import_row).to receive(:publish_event).with(anything)
+ expect(import_row).to receive(:publish_event).with('import_row.failed')
+
+ import_row.process!
+ end
+
it 'sets validation errors' do
import_row.process!
expect(import_row.validation_errors).to eq(error_message)
diff --git a/spree/core/spec/models/spree/import_spec.rb b/spree/core/spec/models/spree/import_spec.rb
index 99aa0d2892..61b7ee58e3 100644
--- a/spree/core/spec/models/spree/import_spec.rb
+++ b/spree/core/spec/models/spree/import_spec.rb
@@ -41,71 +41,20 @@ RSpec.describe Spree::Import, :job, type: :model do
end
end
- describe 'State machine' do
+ describe 'status' do
before { import.save! }
- describe 'initial state' do
- it 'starts in pending state' do
- expect(import.status).to eq('pending')
- end
+ it 'has no state machine' do
+ expect(described_class).not_to respond_to(:state_machines)
end
- describe 'start_mapping event' do
- it 'transitions from pending to mapping' do
- expect { import.start_mapping! }.to change(import, :status).from('pending').to('mapping')
- end
-
- it 'creates mappings before transition' do
- expect { import.start_mapping! }.to change { import.mappings.count }.from(0)
- end
- end
-
- describe 'complete_mapping event' do
- before { import.start_mapping! }
-
- it 'transitions from mapping to completed_mapping' do
- expect { import.complete_mapping! }.to change(import, :status).from('mapping').to('completed_mapping')
- end
-
- it 'creates rows asynchronously after transition' do
- expect(import).to receive(:create_rows_async)
- import.complete_mapping!
- end
+ it 'starts in pending status' do
+ expect(import.status).to eq('pending')
end
- describe 'start_processing event' do
- before do
- import.start_mapping!
- import.complete_mapping!
- end
-
- it 'transitions from completed_mapping to processing' do
- expect { import.start_processing! }.to change(import, :status).from('completed_mapping').to('processing')
- end
- end
-
- describe 'complete event' do
- before do
- import.start_mapping!
- import.complete_mapping!
- import.start_processing!
- end
-
- it 'transitions from processing to completed' do
- expect { import.complete! }.to change(import, :status).from('processing').to('completed')
- end
-
- it 'touches the store' do
- expect { import.complete! }.to change { store.reload.updated_at }
- end
-
- it 'publishes import.completed event' do
- expect(import).to receive(:publish_import_completed_event)
- import.complete!
- end
-
- # Email and loader updates are now handled by ImportSubscriber
- # See spec/subscribers/spree/import_subscriber_spec.rb
+ it 'rejects an unknown status' do
+ import.status = 'nonsense'
+ expect(import).not_to be_valid
end
end
@@ -288,15 +237,15 @@ RSpec.describe Spree::Import, :job, type: :model do
end
end
- describe '#create_mappings' do
+ describe 'mapping creation' do
before { import.save! }
it 'creates mappings for schema fields' do
- expect { import.create_mappings }.to change { import.mappings.count }.from(0)
+ expect { Spree.import_start_mapping_workflow.call(import: import) }.to change { import.mappings.count }.from(0)
end
it 'auto-assigns file columns when possible' do
- import.create_mappings
+ Spree.import_start_mapping_workflow.call(import: import)
slug_mapping = import.mappings.find_by(schema_field: 'slug')
expect(slug_mapping.file_column).to eq('slug')
end
@@ -305,7 +254,7 @@ RSpec.describe Spree::Import, :job, type: :model do
describe '#unmapped_file_columns' do
before do
import.save!
- import.create_mappings
+ Spree.import_start_mapping_workflow.call(import: import)
# Map only slug column
import.mappings.find_by(schema_field: 'slug').update!(file_column: 'slug')
end
@@ -323,7 +272,7 @@ RSpec.describe Spree::Import, :job, type: :model do
context 'when all required fields are mapped' do
before do
- import.create_mappings
+ Spree.import_start_mapping_workflow.call(import: import)
end
it 'returns true' do
@@ -338,24 +287,6 @@ RSpec.describe Spree::Import, :job, type: :model do
end
end
- describe '#create_rows_async' do
- before { import.save! }
-
- it 'enqueues ProcessJob' do
- expect { import.create_rows_async }
- .to have_enqueued_job(Spree::Imports::ProcessJob).with(import.id).on_queue(Spree.queues.imports)
- end
- end
-
- describe '#process_rows_async' do
- before { import.save! }
-
- it 'enqueues ProcessJob, skipping row creation' do
- expect { import.process_rows_async }
- .to have_enqueued_job(Spree::Imports::ProcessJob).with(import.id, skip_row_creation: true)
- end
- end
-
describe '#store' do
context 'when owner is a Store' do
it 'returns the owner' do
@@ -455,16 +386,16 @@ RSpec.describe Spree::Import, :job, type: :model do
describe 'import.completed' do
before do
import.save!
- import.start_mapping!
- import.complete_mapping!
- import.start_processing!
+ Spree.import_start_mapping_workflow.call(import: import)
+ Spree.import_complete_mapping_workflow.call(import: import)
+ Spree.import_start_processing_workflow.call(import: import)
end
it 'publishes import.completed event when completed' do
expect(import).to receive(:publish_event).with('import.completed')
allow(import).to receive(:publish_event).with(anything)
- import.complete!
+ Spree.import_complete_workflow.call(import: import)
end
end
end
diff --git a/spree/core/spec/services/spree/imports/row_processors/customer_spec.rb b/spree/core/spec/services/spree/imports/row_processors/customer_spec.rb
index 27a0abaa51..0af2218796 100644
--- a/spree/core/spec/services/spree/imports/row_processors/customer_spec.rb
+++ b/spree/core/spec/services/spree/imports/row_processors/customer_spec.rb
@@ -9,7 +9,7 @@ RSpec.describe Spree::Imports::RowProcessors::Customer, type: :service do
let(:csv_row_headers) { Spree::ImportSchemas::Customers.new.headers }
before do
- import.create_mappings
+ Spree.import_start_mapping_workflow.call(import: import)
end
def csv_row_hash(attrs = {})
diff --git a/spree/core/spec/services/spree/imports/row_processors/product_translation_spec.rb b/spree/core/spec/services/spree/imports/row_processors/product_translation_spec.rb
index 5e73f72dfd..8c135a9f88 100644
--- a/spree/core/spec/services/spree/imports/row_processors/product_translation_spec.rb
+++ b/spree/core/spec/services/spree/imports/row_processors/product_translation_spec.rb
@@ -9,7 +9,7 @@ RSpec.describe Spree::Imports::RowProcessors::ProductTranslation, type: :service
let(:csv_row_headers) { Spree::ImportSchemas::ProductTranslations.new.headers }
before do
- import.create_mappings
+ Spree.import_start_mapping_workflow.call(import: import)
end
def csv_row_hash(attrs = {})
diff --git a/spree/core/spec/services/spree/imports/row_processors/product_variant_spec.rb b/spree/core/spec/services/spree/imports/row_processors/product_variant_spec.rb
index 73ee7374f5..d94ef4e469 100644
--- a/spree/core/spec/services/spree/imports/row_processors/product_variant_spec.rb
+++ b/spree/core/spec/services/spree/imports/row_processors/product_variant_spec.rb
@@ -10,7 +10,7 @@ RSpec.describe Spree::Imports::RowProcessors::ProductVariant, type: :service do
let(:variant) { subject.process! }
before do
- import.create_mappings
+ Spree.import_start_mapping_workflow.call(import: import)
# Manually map fields since there's no CSV file attached
import.mappings.find_by(schema_field: 'tax_category')&.update(file_column: 'tax_category')
import.mappings.find_by(schema_field: 'product_type')&.update(file_column: 'product_type')
@@ -641,7 +641,7 @@ RSpec.describe Spree::Imports::RowProcessors::ProductVariant, type: :service do
# Override the top-level before to create mappings after custom_field definitions exist
before do
- import.create_mappings
+ Spree.import_start_mapping_workflow.call(import: import)
end
let(:product) { subject.process! }
diff --git a/spree/core/spec/spec_helper.rb b/spree/core/spec/spec_helper.rb
index d1c5b80e46..ce9f96c5ab 100644
--- a/spree/core/spec/spec_helper.rb
+++ b/spree/core/spec/spec_helper.rb
@@ -63,6 +63,7 @@ require 'spree/testing_support/lifecycle_events'
require 'spree/testing_support/preferences'
require 'spree/testing_support/url_helpers'
require 'spree/testing_support/kernel'
+require 'spree/testing_support/imports'
require 'spree/testing_support/rspec_retry_config'
require 'spree/testing_support/next_instance_of'
@@ -102,6 +103,7 @@ RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
config.include Spree::TestingSupport::Preferences
config.include Spree::TestingSupport::Kernel
+ config.include Spree::TestingSupport::Imports
config.order = :random
Kernel.srand config.seed
diff --git a/spree/core/spec/workflows/spree/imports/pipeline_spec.rb b/spree/core/spec/workflows/spree/imports/pipeline_spec.rb
new file mode 100644
index 0000000000..bfd852b4df
--- /dev/null
+++ b/spree/core/spec/workflows/spree/imports/pipeline_spec.rb
@@ -0,0 +1,109 @@
+require 'spec_helper'
+
+RSpec.describe 'Spree::Imports pipeline workflows' do
+ let(:store) { Spree::Store.default }
+ let(:import) { create(:product_import, owner: store) }
+
+ describe Spree::Imports::StartMapping do
+ it 'moves the import into mapping and builds a mapping per schema field' do
+ expect { described_class.call(import: import) }.to change { import.reload.status }.from('pending').to('mapping')
+ expect(import.mappings.count).to be > 0
+ end
+ end
+
+ describe Spree::Imports::CompleteMapping do
+ before { Spree.import_start_mapping_workflow.call(import: import) }
+
+ it 'accepts the mapping' do
+ expect { described_class.call(import: import) }.to change { import.reload.status }.from('mapping').to('completed_mapping')
+ end
+
+ it 'enqueues row creation' do
+ expect { described_class.call(import: import) }
+ .to have_enqueued_job(Spree::Imports::ProcessJob).with(import.id).on_queue(Spree.queues.imports)
+ end
+
+ it 'refuses an import that is not being mapped' do
+ import.update!(status: 'pending')
+
+ result = described_class.call(import: import)
+
+ expect(result).not_to be_success
+ expect(result.error.value).to eq(:import_not_mapping)
+ end
+ end
+
+ # The machine ran its callbacks inside a transaction, so an inline run had
+ # to defer itself to after_all_transactions_commit. Dispatching outside the
+ # transaction removes that workaround — this proves the whole pipeline still
+ # runs to completion with no worker attached, which is what seeds and rake
+ # tasks depend on.
+ describe 'an inline run', :job do
+ it 'processes every row through to a completed import' do
+ import.set_preference(:inline, true)
+ import.save!
+
+ Spree.import_start_mapping_workflow.call(import: import)
+ Spree.import_complete_mapping_workflow.call(import: import)
+
+ expect(import.reload.status).to eq('completed')
+ expect(import.rows.count).to be > 0
+ expect(import.rows.failed.count).to eq(0)
+ expect(import.rows.completed.count).to eq(import.rows.count)
+ end
+ end
+
+ describe Spree::Imports::StartProcessing do
+ before { import.update!(status: 'completed_mapping') }
+
+ it 'moves the import into processing' do
+ expect { described_class.call(import: import) }.to change { import.reload.status }.to('processing')
+ end
+ end
+
+ describe Spree::Imports::Complete do
+ before { import.update!(status: 'processing') }
+
+ it 'completes the import' do
+ expect { described_class.call(import: import) }.to change { import.reload.status }.from('processing').to('completed')
+ end
+
+ it 'touches the store, so its cache keys move with the catalog' do
+ expect { described_class.call(import: import) }.to change { store.reload.updated_at }
+ end
+
+ it 'publishes import.completed', events: true do
+ allow(import).to receive(:publish_event).with(anything)
+ expect(import).to receive(:publish_event).with('import.completed')
+
+ described_class.call(import: import)
+ end
+ end
+
+ describe Spree::Imports::RetryFailedRows do
+ before { import.update!(status: 'completed') }
+
+ context 'with failed rows' do
+ before { create(:import_row, import: import, status: 'failed') }
+
+ it 'puts the import back to processing' do
+ expect { described_class.call(import: import) }.to change { import.reload.status }.from('completed').to('processing')
+ end
+
+ it 'enqueues processing that skips row creation' do
+ expect { described_class.call(import: import) }
+ .to have_enqueued_job(Spree::Imports::ProcessJob).with(import.id, skip_row_creation: true)
+ end
+ end
+
+ context 'without failed rows' do
+ it 'refuses' do
+ result = described_class.call(import: import)
+
+ expect(result).not_to be_success
+ expect(result.error.value).to eq(:import_has_no_failed_rows)
+ expect(import.reload.status).to eq('completed')
+ end
+ end
+ end
+end