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
# 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
          unless parent
            @job.write_output("Warning: Cannot find parent of Note with ID: #{n[:id]}")
            Log.warn("Cannot find parent of Note with ID: #{n[:id]}")

            next
          end

          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 = "                  Note:\n                    \#{n[:notes].lit}\n                  would become:\n                    \#{replaced}\\n\n                CHANGES\n                @job.write_output(changes)\n              else\n                n.update(:notes => replaced.to_sequel_blob)\n              end\n              if parent_repo == @job.repo_id\n                modified_records << JSONModel(parent[:parent_type].to_sym).uri_for(parent[:parent_id], :repo_id => @job.repo_id)\n              else\n                modified_records << JSONModel(parent[:parent_type].to_sym).uri_for(parent[:parent_id], :repo_id => parent_repo)\n              end\n            end\n          end\n        end\n\n        @job.write_output(\"\#{count} note(s)\#{' would be' if job_data['dry_run']} modified.\")\n        @job.write_output(\"================================\")\n      end\n\n      if job_data['dry_run']\n        @job.write_output(\"Dry run complete, no records modified.\")\n      elsif modified_records.empty?\n        @job.write_output(\"All done, no records modified.\")\n      else\n        @job.write_output(\"All done, logging modified records.\")\n      end\n\n      self.success!\n\n      @job.record_created_uris(modified_records.uniq) unless job_data['dry_run']\n    rescue Exception => e\n      @job.write_output(e.message)\n      @job.write_output(e.backtrace)\n      raise e\n    end\n  end\nend\n"