Class: NS2RemoverRunner

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



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
# File 'backend/app/lib/job_runners/ns2_remover_runner.rb', line 8

def run
  begin
    modified_records = []

    job_data = @json.job

    RequestContext.open(:repo_id => @job.repo_id) do
      count = 0
      Note.each do |n|
        parent = NotePersistentId.where(:note_id => n[:id]).first
        next unless ['resource', 'archival_object', 'digital_object', 'digital_object_component'].include?(parent[:parent_type])
        if n.notes.lit.include?(' ns2:')
          replaced = n.notes.lit.gsub('ns2:', '')
          if replaced != n[:notes].lit
            count += 1
            parent_repo = parent[:parent_type].capitalize.constantize.where(id: parent[:parent_id]).get(:repo_id)
            if job_data['dry_run']
              changes = <<~CHANGES
                Note:
                  #{n[:notes].lit}
                would become:
                  #{replaced}\n
              CHANGES
              @job.write_output(changes)
            else
              n.update(:notes => replaced.to_sequel_blob)
            end
            if parent_repo == @job.repo_id
              modified_records << JSONModel(parent[:parent_type].to_sym).uri_for(parent[:parent_id], :repo_id => @job.repo_id)
            else
              modified_records << JSONModel(parent[:parent_type].to_sym).uri_for(parent[:parent_id], :repo_id => parent_repo)
            end
          end
        end
      end

      @job.write_output("#{count} note(s)#{' would be' if job_data['dry_run']} modified.")
      @job.write_output("================================")
    end

    if job_data['dry_run']
      @job.write_output("Dry run complete, no records modified.")
    elsif modified_records.empty?
      @job.write_output("All done, no records modified.")
    else
      @job.write_output("All done, logging modified records.")
    end

    self.success!

    @job.record_created_uris(modified_records.uniq) unless job_data['dry_run']
  rescue Exception => e
    @job.write_output(e.message)
    @job.write_output(e.backtrace)
    raise e
  end
end