Class: Dor::Workflow::Client::WorkflowRoutes

Inherits:
Object
  • Object
show all
Extended by:
Deprecation
Defined in:
lib/dor/workflow/client/workflow_routes.rb

Overview

Makes requests relating to a workflow

Instance Method Summary collapse

Constructor Details

#initialize(requestor:) ⇒ WorkflowRoutes



10
11
12
# File 'lib/dor/workflow/client/workflow_routes.rb', line 10

def initialize(requestor:)
  @requestor = requestor
end

Instance Method Details

#all_workflows(pid:) ⇒ Workflow::Response::Workflows

Retrieves all workflows for the given object



202
203
204
205
# File 'lib/dor/workflow/client/workflow_routes.rb', line 202

def all_workflows(pid:)
  xml = all_workflows_xml(pid)
  Workflow::Response::Workflows.new(xml: xml)
end

#all_workflows_xml(druid) ⇒ String

Retrieves the raw XML for all the workflows for the the given object



195
196
197
# File 'lib/dor/workflow/client/workflow_routes.rb', line 195

def all_workflows_xml(druid)
  requestor.request "objects/#{druid}/workflows"
end

#create_workflow(_repo, druid, workflow_name, _wf_xml, opts = {}) ⇒ Boolean

This method is deprecated and calls create_workflow_by_name.

Options Hash (opts):

  • :lane_id (String)

    adds laneId attribute to all process elements in the wf_xml workflow xml. Defaults to a value of 'default'



24
25
26
# File 'lib/dor/workflow/client/workflow_routes.rb', line 24

def create_workflow(_repo, druid, workflow_name, _wf_xml, opts = {})
  create_workflow_by_name(druid, workflow_name, opts)
end

#create_workflow_by_name(druid, workflow_name, opts = {}) ⇒ Boolean

Creates a workflow for a given object in the repository. If this particular workflow for this objects exists, it will replace the old workflow. You have the option of creating a datastream or not. Returns true on success. Caller must handle any exceptions.

name that is known by the workflow service.

Options Hash (opts):

  • :lane_id (String)

    adds laneId attribute to all process elements in the wf_xml workflow xml. Defaults to a value of 'default'

  • :version (Integer)

    specifies the version so that workflow service doesn't need to query dor-services.



41
42
43
44
45
46
47
48
# File 'lib/dor/workflow/client/workflow_routes.rb', line 41

def create_workflow_by_name(druid, workflow_name, opts = {})
  params = { 'lane-id' => opts.fetch(:lane_id, 'default') }
  params['version'] = opts[:version] if opts[:version]
  requestor.request "objects/#{druid}/workflows/#{workflow_name}", 'post', '',
                    content_type: 'application/xml',
                    params: params
  true
end

#delete_all_workflows(pid:) ⇒ Boolean

Deletes all workflow steps for a particular druid



243
244
245
246
# File 'lib/dor/workflow/client/workflow_routes.rb', line 243

def delete_all_workflows(pid:)
  requestor.request "objects/#{pid}/workflows", 'delete'
  true
end

#delete_workflow(repo, druid, workflow) ⇒ Boolean

Deletes a workflow from a particular repository and druid



235
236
237
238
# File 'lib/dor/workflow/client/workflow_routes.rb', line 235

def delete_workflow(repo, druid, workflow)
  requestor.request "#{repo}/objects/#{druid}/workflows/#{workflow}", 'delete'
  true
end

#update_error_status(druid:, workflow:, process:, error_msg:, error_text: nil) ⇒ Workflow::Response::Update

Updates the status of one step in a workflow to error. Returns true on success. Caller must handle any exceptions

Http Call

The method does an HTTP PUT to the URL defined in Dor::WF_URI.

PUT "/objects/pid:123/workflows/GoogleScannedWF/convert"
<process name=\"convert\" status=\"error\" />"


185
186
187
188
189
# File 'lib/dor/workflow/client/workflow_routes.rb', line 185

def update_error_status(druid:, workflow:, process:, error_msg:, error_text: nil)
  xml = create_process_xml(name: process, status: 'error', errorMessage: error_msg, error_text: error_text)
  response = requestor.request "objects/#{druid}/workflows/#{workflow}/#{process}", 'put', xml, content_type: 'application/xml'
  Workflow::Response::Update.new(json: response)
end

#update_status(druid:, workflow:, process:, status:, elapsed: 0, lifecycle: nil, note: nil, current_status: nil) ⇒ Boolean

Updates the status of one step in a workflow. Returns true on success. Caller must handle any exceptions

Http Call

The method does an HTTP PUT to the URL defined in Dor::WF_URI. As an example:

PUT "/objects/pid:123/workflows/GoogleScannedWF/convert"
<process name=\"convert\" status=\"completed\" />"

Raises:

  • (ArgumentError)


69
70
71
72
73
74
75
76
77
78
79
# File 'lib/dor/workflow/client/workflow_routes.rb', line 69

def update_status(druid:, workflow:, process:, status:, elapsed: 0, lifecycle: nil, note: nil, current_status: nil)
  raise ArgumentError, "Unknown status value #{status}" unless VALID_STATUS.include?(status)
  raise ArgumentError, "Unknown current_status value #{current_status}" if current_status && !VALID_STATUS.include?(current_status)

  xml = create_process_xml(name: process, status: status, elapsed: elapsed, lifecycle: lifecycle, note: note)
  uri = "objects/#{druid}/workflows/#{workflow}/#{process}"
  uri += "?current-status=#{current_status}" if current_status
  response = requestor.request(uri, 'put', xml, content_type: 'application/xml')

  Workflow::Response::Update.new(json: response)
end

#update_workflow_error_status(_repo, druid, workflow, process, error_msg, opts = {}) ⇒ Boolean

Updates the status of one step in a workflow to error. Returns true on success. Caller must handle any exceptions

Http Call

The method does an HTTP PUT to the URL defined in Dor::WF_URI.

PUT "/objects/pid:123/workflows/GoogleScannedWF/convert"
<process name=\"convert\" status=\"error\" />"

Options Hash (opts):

  • :error_text (String)

    A slot to hold more information about the error, like a full stacktrace



163
164
165
166
# File 'lib/dor/workflow/client/workflow_routes.rb', line 163

def update_workflow_error_status(_repo, druid, workflow, process, error_msg, opts = {})
  update_error_status(druid: druid, workflow: workflow, process: process, error_msg: error_msg, error_text: opts[:error_text])
  true
end

#update_workflow_status(repo, druid, workflow, process, status, opts = {}) ⇒ Boolean

Updates the status of one step in a workflow. Returns true on success. Caller must handle any exceptions

Http Call

The method does an HTTP PUT to the URL defined in Dor::WF_URI. As an example:

PUT "/dor/objects/pid:123/workflows/GoogleScannedWF/convert"
<process name=\"convert\" status=\"completed\" />"

Options Hash (opts):

  • :elapsed (Float)

    The number of seconds it took to complete this step. Can have a decimal. Is set to 0 if not passed in.

  • :lifecycle (String)

    Bookeeping label for this particular workflow step. Examples are: 'registered', 'shelved'

  • :note (String)

    Any kind of string annotation that you want to attach to the workflow

  • :lane_id (String)

    Id of processing lane used by the job manager. Can convey priority or name of an applicaiton specific processing lane (e.g. 'high', 'critical', 'hydrus')

  • :current_status (String)

    Setting this string tells the workflow service to compare the current status to this value. If the current value does not match this value, the update is not performed

Raises:

  • (ArgumentError)


102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/dor/workflow/client/workflow_routes.rb', line 102

def update_workflow_status(repo, druid, workflow, process, status, opts = {})
  raise ArgumentError, "Unknown status value #{status}" unless VALID_STATUS.include?(status.downcase)

  opts = { elapsed: 0, lifecycle: nil, note: nil }.merge!(opts)
  opts[:elapsed] = opts[:elapsed].to_s
  current_status = opts.delete(:current_status)
  xml = create_process_xml({ name: process, status: status.downcase }.merge!(opts))
  uri = "#{repo}/objects/#{druid}/workflows/#{workflow}/#{process}"
  uri += "?current-status=#{current_status.downcase}" if current_status
  response = requestor.request(uri, 'put', xml, content_type: 'application/xml')
  Workflow::Response::Update.new(json: response)
end

#workflow(repo: 'dor', pid:, workflow_name:) ⇒ Workflow::Response::Workflow



225
226
227
228
# File 'lib/dor/workflow/client/workflow_routes.rb', line 225

def workflow(repo: 'dor', pid:, workflow_name:)
  xml = workflow_xml(repo, pid, workflow_name)
  Workflow::Response::Workflow.new(xml: xml)
end

#workflow_status(repo, druid, workflow, process) ⇒ String

Retrieves the process status of the given workflow for the given object identifier



123
124
125
126
127
128
129
130
131
# File 'lib/dor/workflow/client/workflow_routes.rb', line 123

def workflow_status(repo, druid, workflow, process)
  workflow_md = workflow_xml(repo, druid, workflow)
  doc = Nokogiri::XML(workflow_md)
  raise Dor::WorkflowException, "Unable to parse response:\n#{workflow_md}" if doc.root.nil?

  processes = doc.root.xpath("//process[@name='#{process}']")
  process = processes.max { |a, b| a.attr('version').to_i <=> b.attr('version').to_i }
  process&.attr('status')
end

#workflow_xml(repo, druid, workflow) ⇒ String

Retrieves the raw XML for the given workflow

Raises:

  • (ArgumentError)


139
140
141
142
143
# File 'lib/dor/workflow/client/workflow_routes.rb', line 139

def workflow_xml(repo, druid, workflow)
  raise ArgumentError, 'missing workflow' unless workflow

  requestor.request "#{repo}/objects/#{druid}/workflows/#{workflow}"
end

#workflows(pid, repo = 'dor') ⇒ Array<String>

Get workflow names into an array for given PID This method only works when this gem is used in a project that is configured to connect to DOR

Examples:

client.workflows('druid:sr100hp0609')
=> ["accessionWF", "assemblyWF", "disseminationWF"]


216
217
218
219
# File 'lib/dor/workflow/client/workflow_routes.rb', line 216

def workflows(pid, repo = 'dor')
  xml_doc = Nokogiri::XML(workflow_xml(repo, pid, ''))
  xml_doc.xpath('//workflow').collect { |workflow| workflow['id'] }
end