Class: LyberCore::Robot

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Job
Defined in:
lib/lyber_core/robot.rb

Overview

Base class for all robots. Subclasses should implement the #perform_work method.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(workflow_name, process) ⇒ Robot

Returns a new instance of Robot.



14
15
16
17
# File 'lib/lyber_core/robot.rb', line 14

def initialize(workflow_name, process)
  @workflow_name = workflow_name
  @process = process
end

Instance Attribute Details

#druidObject (readonly)

Returns the value of attribute druid.



10
11
12
# File 'lib/lyber_core/robot.rb', line 10

def druid
  @druid
end

#processObject (readonly)

Returns the value of attribute process.



10
11
12
# File 'lib/lyber_core/robot.rb', line 10

def process
  @process
end

#workflow_nameObject (readonly)

Returns the value of attribute workflow_name.



10
11
12
# File 'lib/lyber_core/robot.rb', line 10

def workflow_name
  @workflow_name
end

Instance Method Details

#bare_druidObject



86
87
88
# File 'lib/lyber_core/robot.rb', line 86

def bare_druid
  @bare_druid = druid.delete_prefix('druid:')
end

#cocina_objectObject



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

def cocina_object
  @cocina_object ||= object_client.find
end

#druid_objectObject



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

def druid_object
  @druid_object ||= DruidTools::Druid.new(druid, Settings.stacks.local_workspace_root)
end

#object_clientObject



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

def object_client
  @object_client ||= Dor::Services::Client.object(druid)
end

#perform(druid) ⇒ Object

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



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
74
75
76
# File 'lib/lyber_core/robot.rb', line 39

def perform(druid)
  @druid = druid
  Honeybadger.context(druid: druid, process: process, workflow_name: workflow_name)

  logger.info "#{druid} processing #{process} (#{workflow_name})"
  return unless check_item_queued?

  # this is the default note to pass back to workflow service,
  # but it can be overriden by a robot that uses the Robots::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 = perform_work
  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.instance_of?(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'

  logger.info "Finished #{druid} in #{format('%0.4f', elapsed)}s"
rescue StandardError => e
  handle_error(e)
end

#perform_workObject

Work performed by the robot. This method is to be implemented by robot subclasses.

Raises:

  • (NotImplementedError)


82
83
84
# File 'lib/lyber_core/robot.rb', line 82

def perform_work
  raise NotImplementedError
end

#workflow_serviceObject



19
20
21
# File 'lib/lyber_core/robot.rb', line 19

def workflow_service
  @workflow_service ||= WorkflowClientFactory.build(logger: logger)
end