Module: LyberCore::Robot

Defined in:
lib/lyber_core/robot.rb,
lib/lyber_core/return_state.rb

Defined Under Namespace

Modules: ClassMethods Classes: ReturnState

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#check_queued_statusObject

Returns the value of attribute check_queued_status.



21
22
23
# File 'lib/lyber_core/robot.rb', line 21

def check_queued_status
  @check_queued_status
end

#processObject (readonly)

Returns the value of attribute process.



22
23
24
# File 'lib/lyber_core/robot.rb', line 22

def process
  @process
end

#workflow_nameObject (readonly)

Returns the value of attribute workflow_name.



22
23
24
# File 'lib/lyber_core/robot.rb', line 22

def workflow_name
  @workflow_name
end

Class Method Details

.included(base) ⇒ Object

Add the ClassMethods to the class this is being mixed into



8
9
10
# File 'lib/lyber_core/robot.rb', line 8

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#initialize(workflow_name, process, check_queued_status: true) ⇒ Object



24
25
26
27
28
29
# File 'lib/lyber_core/robot.rb', line 24

def initialize(workflow_name, process, check_queued_status: true)
  Signal.trap('QUIT') { puts "#{Process.pid} ignoring SIGQUIT" } # SIGQUIT ignored to let the robot finish
  @workflow_name = workflow_name
  @process = process
  @check_queued_status = check_queued_status
end

#work(druid, context) ⇒ Object

Sets up logging, timing and error handling of the job Calls the #perform method, then sets workflow to ‘completed’ or ‘error’ depending on success rubocop:disable Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity



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
84
85
86
87
# File 'lib/lyber_core/robot.rb', line 40

def work(druid, context)
  Honeybadger.context(druid: druid, process: process, workflow_name: workflow_name) if defined? Honeybadger
  workflow = workflow(druid)
  LyberCore::Log.set_logfile($stdout) # let process manager handle logging
  LyberCore::Log.info "#{druid} processing #{process} (#{workflow_name})"
  return if check_queued_status && !item_queued?(druid)

  # this is the default note to pass back to workflow service,
  # but it can be overriden by a robot that uses the Lybercore::Robot::ReturnState
  # object to return a status
  note = Socket.gethostname

  # update the workflow status to indicate that started
  workflow.start(note)

  result = nil
  elapsed = Benchmark.realtime do
    result = if method(:perform).arity == 1
      perform druid # implemented in the mixed-in robot class
    else
      perform druid, context
    end
  end

  # the final workflow state is determined by the return value of the perform step, if it is a ReturnState object,
  # we will use the defined status, otherwise default to completed
  # if a note is passed back, we will also use that instead of the default
  if result.class == LyberCore::Robot::ReturnState
    workflow_state = result.status
    note = result.note unless result.note.blank?
  else
    workflow_state = 'completed'
  end
  # update the workflow status from its current state to the state returned by perform (or 'completed' as the default)
  # noop allows a robot to not set a workflow as complete, e.g., if that is delegated to another service.
  workflow.complete(workflow_state, elapsed, note) unless workflow_state == 'noop'

  LyberCore::Log.info "Finished #{druid} in #{sprintf('%0.4f', elapsed)}s"
rescue StandardError => e
  Honeybadger.notify(e) if defined? Honeybadger
  begin
    LyberCore::Log.error e.message + "\n" + e.backtrace.join("\n")
    workflow.error(e.message, Socket.gethostname)
  rescue StandardError => e
    LyberCore::Log.error "Cannot set #{druid} to status='error'\n#{e.message}\n#{e.backtrace.join("\n")}"
    raise e # send exception to Resque failed queue
  end
end

#workflow_serviceObject



31
32
33
# File 'lib/lyber_core/robot.rb', line 31

def workflow_service
  raise 'The workflow_service method must be implemented on the class that includes LyberCore::Robot'
end