Class: LyberCore::Robot
- Inherits:
-
Object
- Object
- LyberCore::Robot
- 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
-
#druid ⇒ Object
readonly
Returns the value of attribute druid.
-
#process ⇒ Object
readonly
Returns the value of attribute process.
-
#workflow_name ⇒ Object
readonly
Returns the value of attribute workflow_name.
Instance Method Summary collapse
- #bare_druid ⇒ Object
- #cocina_object ⇒ Object
- #druid_object ⇒ Object
-
#initialize(workflow_name, process) ⇒ Robot
constructor
A new instance of Robot.
- #object_client ⇒ Object
-
#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.
-
#perform_work ⇒ Object
Work performed by the robot.
- #workflow_service ⇒ Object
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
#druid ⇒ Object (readonly)
Returns the value of attribute druid.
10 11 12 |
# File 'lib/lyber_core/robot.rb', line 10 def druid @druid end |
#process ⇒ Object (readonly)
Returns the value of attribute process.
10 11 12 |
# File 'lib/lyber_core/robot.rb', line 10 def process @process end |
#workflow_name ⇒ Object (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_druid ⇒ Object
86 87 88 |
# File 'lib/lyber_core/robot.rb', line 86 def @bare_druid = druid.delete_prefix('druid:') end |
#cocina_object ⇒ Object
27 28 29 |
# File 'lib/lyber_core/robot.rb', line 27 def cocina_object @cocina_object ||= object_client.find end |
#druid_object ⇒ Object
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_client ⇒ Object
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_work ⇒ Object
Work performed by the robot. This method is to be implemented by robot subclasses.
82 83 84 |
# File 'lib/lyber_core/robot.rb', line 82 def perform_work raise NotImplementedError end |
#workflow_service ⇒ Object
19 20 21 |
# File 'lib/lyber_core/robot.rb', line 19 def workflow_service @workflow_service ||= WorkflowClientFactory.build(logger: logger) end |