Class: BulkImportRunner

Inherits:
JobRunner show all
Defined in:
backend/app/lib/job_runners/bulk_import_runner.rb

Instance Method Summary collapse

Methods inherited from JobRunner

#add_success_hook, #cancelation_signaler, #canceled?, for, #initialize, #parse_job_params_string, register_for_job_type, registered_job_types, registered_runner_for, #success!, #symbol_keys

Constructor Details

This class inherits a constructor from JobRunner

Instance Method Details

#runObject



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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'backend/app/lib/job_runners/bulk_import_runner.rb', line 15

def run
  ticker = Ticker.new(@job)
  ticker.log("Start new bulk_import for job: #{@job.id}")
  last_error = nil
  # Wrap the import in a transaction if the DB supports MVCC
  begin
    DB.open(DB.supports_mvcc?,
            :retry_on_optimistic_locking_fail => true) do
      begin
        @input_file = @job.job_files[0].full_file_path
        @current_user = User.find(:username => @job.owner.username)
        @load_type = @json.job["load_type"]
        @validate_only = @json.job["only_validate"] == "true"
        params = @json.job_params ? parse_job_params_string(@json.job_params) : {}
        params[:validate] = @validate_only
        ticker.log(("=" * 50) + "\n#{@json.job["filename"]}\n" + ("=" * 50))
        begin
          RequestContext.open(:create_enums => true,
                              :current_username => @job.owner.username,
                              :repo_id => @job.repo_id) do
            #               converter.run(@job[:job_blob])
            success = true
            importer = get_importer(@json.job["content_type"], params, ticker.method(:log))
            report = importer.run
            if !report.terminal_error.nil?
              msg = I18n.t("bulk_import.error.error", :term => report.terminal_error)
            else
              msg = I18n.t("bulk_import.processed")
            end
            ticker.log(msg)
            ticker.log(("=" * 50) + "\n")
            ticker.log(I18n.t(@validate_only ? "bulk_import.log_validation" : "bulk_import.log_complete", :file => @json.job["filename"]))
            ticker.log("\n" + ("=" * 50) + "\n")
            file = ASUtils.tempfile("load_spreadsheet_job_")
            generate_csv(file, report)
            file.rewind
            @job.write_output(I18n.t("bulk_import.log_results"))
            @job.add_file(file)
            @job.record_created_uris(importer.record_uris) unless @validate_only
          end
        end
      rescue JSONModel::ValidationException, BulkImportException => e
        last_error = e
      end
    end
  rescue
    last_error = $!
  end
  self.success!
  if last_error
    ticker.log("\n\n")
    ticker.log("!" * 50)
    ticker.log("IMPORT ERROR")
    ticker.log("!" * 50)
    ticker.log("\n\n")

    if last_error.respond_to?(:errors)
      ticker.log("#{last_error}") if last_error.errors.empty? # just spit it out if there's not explicit errors

      ticker.log("The following errors were found:\n")

      last_error.errors.each_pair { |k, v| ticker.log("\t#{k.to_s} : #{v.join(" -- ")}") }
    else
      ticker.log("Error: #{CGI.escapeHTML(last_error.inspect)}")
    end
    ticker.log("!" * 50)
    raise last_error
  end
end