Class: BulkCsvParser::ImportJob

Inherits:
ActiveJob::Base
  • Object
show all
Defined in:
lib/bulk_csv_parser/import_job.rb

Overview

Runs BulkCsvParser::Processor in the background via ActiveJob, so large CSV uploads don't block a web request. Enqueue it via BulkCsvParser.enqueue rather than calling perform_later directly -- that helper is what puts the file somewhere the worker (often a different machine than the one that received the upload) can actually read it back from.

Instance Method Summary collapse

Instance Method Details

#perform(file_reference:, model:, find_by:, attributes:, header_mapping: {}, batch_size: nil, strategy: nil, notify_email: nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bulk_csv_parser/import_job.rb', line 14

def perform(
  file_reference:,
  model:,
  find_by:,
  attributes:,
  header_mapping: {},
  batch_size: nil,
  strategy: nil,
  notify_email: nil
)
  local_file = BulkCsvParser::Storage.fetch(file_reference)
  path = local_file.respond_to?(:path) ? local_file.path : local_file

  result = BulkCsvParser::Processor.new(
    file_path: path,
    model: model,
    find_by: find_by,
    attributes: attributes,
    header_mapping: header_mapping,
    batch_size: batch_size,
    strategy: strategy
  ).call

  log(model, result)
  notify(notify_email, model, result)
  result
ensure
  local_file.close! if local_file.respond_to?(:close!)
  BulkCsvParser::Storage.cleanup(file_reference)
end