Class: Robots::SdrRepo::SdrRobot

Inherits:
Object
  • Object
show all
Includes:
LyberCore::Robot
Defined in:
lib/sdr/sdr_robot.rb

Overview

Contains the shared methods used by all robots that inherit from this object

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(workflow_name, step_name, opts = {}) ⇒ SdrRobot

Returns a new instance of SdrRobot


21
22
23
# File 'lib/sdr/sdr_robot.rb', line 21

def initialize(workflow_name, step_name, opts = {})
  super('sdr', workflow_name, step_name, opts)
end

Class Attribute Details

.step_nameObject

Returns the value of attribute step_name


18
19
20
# File 'lib/sdr/sdr_robot.rb', line 18

def step_name
  @step_name
end

.workflow_nameObject

Returns the value of attribute workflow_name


17
18
19
# File 'lib/sdr/sdr_robot.rb', line 17

def workflow_name
  @workflow_name
end

Instance Method Details

#create_workflow_rows(repo, druid, workflow_name, wf_xml, opts = {:create_ds => true}) ⇒ Object


78
79
80
# File 'lib/sdr/sdr_robot.rb', line 78

def create_workflow_rows(repo, druid, workflow_name, wf_xml, opts = {:create_ds => true})
  transmit(opts) { Dor::WorkflowService.create_workflow(repo, druid, workflow_name, wf_xml, opts) }
end

#get_workflow_status(repo, druid, workflow_name, step_name, opts = {}) ⇒ Object


86
87
88
# File 'lib/sdr/sdr_robot.rb', line 86

def get_workflow_status(repo, druid, workflow_name, step_name, opts={})
  transmit(opts) { Dor::WorkflowService.get_workflow_status(repo, druid, workflow_name, step_name) }
end

#get_workflow_xml(repo, druid, workflow_name, opts = {}) ⇒ Object


82
83
84
# File 'lib/sdr/sdr_robot.rb', line 82

def get_workflow_xml(repo, druid, workflow_name, opts={})
  transmit(opts) { Dor::WorkflowService.get_workflow_xml(repo, druid, workflow_name) }
end

#process_item(druid) ⇒ Boolean

Returns process the object, then set success or error status

Parameters:

  • druid (String)

    The object being processed

Returns:

  • (Boolean)

    process the object, then set success or error status


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
# File 'lib/sdr/sdr_robot.rb', line 27

def process_item(druid)
  elapsed = Benchmark.realtime do
    perform druid # implemented in the robot subclass
  end
  update_workflow_status 'sdr', druid, self.class.workflow_name, self.class.step_name, 'completed', elapsed
  LyberCore::Log.info "Completed #{druid} in #{elapsed} seconds"
  return 'completed'
rescue StandardError => e
  Honeybadger.notify(e)

  message = if e.cause
              "#{e.message}; caused by #{e.cause.inspect}"
            else
              e.message
            end

  backtrace = if e.cause
                e.cause.backtrace
              else
                e.backtrace
              end

  update_workflow_error_status 'sdr', druid, self.class.workflow_name, self.class.step_name, message

  log_message = "#{druid} #{message} #{backtrace.join("\n")}"

  if  e.is_a?(FatalError)
    LyberCore::Log.fatal log_message
    'fatal'
  else
    LyberCore::Log.error log_message
    'error'
  end
end

#transmit(opts = {}, &request) ⇒ Boolean

Returns retry request up to :tries times, sleeping :interval seconds after each failed attempt

Parameters:

  • opts (Hash) (defaults to: {})

    options (:tries and :interval)

  • request (Object)

    the block of code to execute

Returns:

  • (Boolean)

    retry request up to :tries times, sleeping :interval seconds after each failed attempt


65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/sdr/sdr_robot.rb', line 65

def transmit(opts={}, &request)
  tries ||= opts[:tries] || 3
  interval ||= opts[:interval] || 20
  request.call(nil)
rescue StandardError => e
  if (tries -= 1) > 0
    sleep interval
    retry
  else
    raise FatalError.new("Failed to transmit request")
  end
end

#update_workflow_error_status(repo, druid, workflow_name, step_name, msg, opts = {}) ⇒ Object


96
97
98
99
100
101
102
103
104
# File 'lib/sdr/sdr_robot.rb', line 96

def update_workflow_error_status(repo, druid, workflow_name, step_name, msg, opts={})
  LyberCore::Log.debug("( #{__FILE__} : #{__LINE__} ) Enter update_workflow_error_status")
  message = msg.lines.first.chomp[0..80] + (msg.length > 80 ? ' ...' : '')
  params = {repo: repo, druid: druid, workflow_name: workflow_name, step_name: step_name, message: message}
  LyberCore::Log.debug(params.inspect)
  LyberCore::Log.debug(opts.inspect)
  opts[:note] = Socket.gethostname
  transmit(opts) { Dor::WorkflowService.update_workflow_error_status(repo, druid, workflow_name, step_name, message, opts ) }
end

#update_workflow_status(repo, druid, workflow_name, step_name, status, elapsed, opts = {}) ⇒ Object


90
91
92
93
94
# File 'lib/sdr/sdr_robot.rb', line 90

def update_workflow_status(repo, druid, workflow_name, step_name, status, elapsed, opts={})
  opts[:elapsed] = elapsed
  opts[:note] = Socket.gethostname
  transmit(opts) { Dor::WorkflowService.update_workflow_status(repo, druid, workflow_name, step_name, status, opts) }
end