Class: Spree::Imports::ProcessGroupJob

Inherits:
BaseJob
  • Object
show all
Defined in:
app/jobs/spree/imports/process_group_job.rb

Constant Summary collapse

ROWS_BATCH_SIZE =

Rows are loaded in slices so a large group never holds every ImportRow (and its raw CSV data) in memory for the whole job.

100

Instance Method Summary collapse

Instance Method Details

#perform(import_id, row_ids) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/jobs/spree/imports/process_group_job.rb', line 24

def perform(import_id, row_ids)
  import = Spree::Import.find(import_id)
  Spree::Current.store = import.store

  mappings = import.mappings.mapped.to_a
  schema_fields = import.schema_fields
  large = import.large_import?
  grouped = import.group_column.present? && mappings.any? { |m| m.schema_field == import.group_column }
  started_at = Time.current
  processed_rows = []

  row_ids.each_slice(ROWS_BATCH_SIZE) do |ids|
    # Skip rows already completed on a prior attempt so retries don't double-process them.
    rows = import.rows.where(id: ids).pending_and_failed.order(:row_number).to_a
    # Share the already-loaded import across rows: each row's processor reads
    # `row.import` (store, ability, lookup cache), and without this every row
    # lazily loads its own Import instance and rebuilds all of that per row.
    rows.each { |row| row.association(:import).target = import }

    if large
      Spree::Events.disable do
        rows.each { |row| row.bulk_process!(mappings: mappings, schema_fields: schema_fields) }
      end
    elsif grouped
      # A group is one product plus its variants: per-record lifecycle
      # events (variant.created, price.created, product.updated per
      # touch) are noise to subscribers — one product event is published
      # for the whole group below. import_row.* events still flow.
      Spree::Events.disable_lifecycle do
        rows.each { |row| row.process!(mappings: mappings, schema_fields: schema_fields) }
      end
    else
      rows.each do |row|
        row.process!(mappings: mappings, schema_fields: schema_fields)
      end
    end
    processed_rows.concat(rows)
  end

  publish_group_events(processed_rows, started_at) if grouped
  check_import_completion(import, large)
end