Class: BulkArchivalObjectUpdaterRunner

Inherits:
JobRunner
  • Object
show all
Defined in:
backend/app/lib/job_runners/bulk_archival_object_updater_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



6
7
8
9
10
11
12
13
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
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
# File 'backend/app/lib/job_runners/bulk_archival_object_updater_runner.rb', line 6

def run
  @job.write_output("Starting spreadsheet bulk archival object updater job\n")

  if @job.job_files.length != 1
    @job.write_output("\nNo spreadsheet found.\n")
    @job.finish!(:failed)

    raise Exception.new('No spreadsheet found.')
  end

  spreadsheet = @job.job_files[0]

  begin
    RequestContext.open(:current_username => @job.owner.username,
                        :inside_bulk_update => true,
                        :repo_id => @job.repo_id) do

      parameters = {}
      if @job.job.has_key?('create_missing_top_containers')
        parameters['create_missing_top_containers'] = @job.job['create_missing_top_containers']
      end

      bulk_archival_object_updater = BulkArchivalObjectUpdater.new(spreadsheet.full_file_path, parameters.transform_keys(&:to_sym))

      result = bulk_archival_object_updater.run

      bulk_archival_object_updater.info_messages.each do |message|
        @job.write_output(message)
      end

      @job.write_output("\nBulk update job successfully completed. %d record(s) were updated." % [ result[:updated_uris].length ])
      @job.record_created_uris(result[:updated_uris])
      @job.job_blob = ASUtils.to_json(result)
      @job.job_files[0].delete
      @job.save
      @job.finish!(:completed)
      self.success!
    end
  rescue BulkArchivalObjectUpdater::BulkUpdateFailed => e
    @job.write_output("\nErrors encountered during processing.\n")
    @job.write_output("All changes have been reverted.\n\n")

    e.errors.each_with_index do |error, index|
      if index > 0
        @job.write_output("\n")
      end

      formatted_errors = error.fetch(:errors).join("\n")

      @job.write_output("Sheet name: #{error.fetch(:sheet)}")
      @job.write_output("Row number: #{error.fetch(:row)}")
      @job.write_output("Column: #{error.fetch(:column, 'N/A')}")
      @job.write_output("JSONModel Property: #{error.fetch(:json_property, 'N/A')}")
      @job.write_output("Errors: " + formatted_errors)
    end

    @job.write_output("\nPlease correct any issues with your import spreadsheet and retry.\n")
    @job.finish!(:failed)

    raise e
  rescue => e
    Log.exception(e)
    @job.write_output("Unexpected failure while running @job. Error: #{e}")

    @job.finish!(:failed)
    raise e
  end
end