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

Returns a new instance of WorkflowRoutes.



12
13
14
# File 'lib/dor/workflow/client/workflow_routes.rb', line 12

def initialize(requestor:)
  @requestor = requestor
end

Instance Method Details

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

Retrieves all workflows for the given object

Parameters:

  • pid (String)

    The id of the object

Returns:



151
152
153
154
# File 'lib/dor/workflow/client/workflow_routes.rb', line 151

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

Parameters:

  • druid (String)

    The id of the object

Returns:

  • (String)

    XML of the workflow



144
145
146
# File 'lib/dor/workflow/client/workflow_routes.rb', line 144

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.

Parameters:

  • repo (String)

    Ignored

  • druid (String)

    The id of the object

  • workflow_name (String)

    The name of the workflow you want to create

  • wf_xml (String)

    Ignored

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

    optional params

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'

Returns:

  • (Boolean)

    always true



26
27
28
# File 'lib/dor/workflow/client/workflow_routes.rb', line 26

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.

Parameters:

  • druid (String)

    The id of the object

  • workflow_name (String)

    The name of the workflow you want to create. This must correspond with a workflow

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

    optional params

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.

Returns:

  • (Boolean)

    always true



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

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

Parameters:

  • pid (String)

    The id of the object to delete the workflow from

Returns:

  • (Boolean)

    always true



192
193
194
195
# File 'lib/dor/workflow/client/workflow_routes.rb', line 192

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

Parameters:

  • repo (String)

    The repository the object resides in. The service recoginzes "dor" and "sdr" at the moment

  • druid (String)

    The id of the object to delete the workflow from

  • workflow (String)

    The name of the workflow to be deleted

Returns:

  • (Boolean)

    always true



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

def delete_workflow(repo, druid, workflow)
  requestor.request "#{repo}/objects/#{druid}/workflows/#{workflow}", 'delete'
  true
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 "/dor/objects/pid:123/workflows/GoogleScannedWF/convert"
<process name=\"convert\" status=\"error\" />"

Parameters:

  • repo (String)

    The repository the object resides in. The service recoginzes "dor" and "sdr" at the moment

  • druid (String)

    The id of the object

  • workflow (String)

    The name of the workflow

  • error_msg (String)

    The error message. Ideally, this is a brief message describing the error

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

    optional values for the workflow step

Options Hash (opts):

  • :error_text (String)

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

Returns:

  • (Boolean)

    always true



133
134
135
136
137
138
# File 'lib/dor/workflow/client/workflow_routes.rb', line 133

def update_workflow_error_status(repo, druid, workflow, process, error_msg, opts = {})
  opts = { error_text: nil }.merge!(opts)
  xml = create_process_xml({ name: process, status: 'error', errorMessage: error_msg }.merge!(opts))
  requestor.request "#{repo}/objects/#{druid}/workflows/#{workflow}/#{process}", 'put', xml, content_type: 'application/xml'
  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\" />"

Parameters:

  • repo (String)

    The repository the object resides in. The service recoginzes "dor" and "sdr" at the moment

  • druid (String)

    The id of the object

  • workflow (String)

    The name of the workflow

  • process (String)

    The name of the process step

  • status (String)

    The status that you want to set -- using one of the values in VALID_STATUS

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

    optional values for the workflow step

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

Returns:

  • (Boolean)

    always true

Raises:

  • (ArgumentError)


73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/dor/workflow/client/workflow_routes.rb', line 73

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

Parameters:

  • repo (String) (defaults to: 'dor')

    repository of the object

  • pid (String)

    id of object

  • workflow_name (String)

    The name of the workflow

Returns:



174
175
176
177
# File 'lib/dor/workflow/client/workflow_routes.rb', line 174

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

Parameters:

  • repo (String)

    The repository the object resides in. Currently recoginzes "dor" and "sdr".

  • druid (String)

    The id of the object

  • workflow (String)

    The name of the workflow

  • process (String)

    The name of the process step

Returns:

  • (String)

    status for repo-workflow-process-druid

Raises:



94
95
96
97
98
99
100
101
102
# File 'lib/dor/workflow/client/workflow_routes.rb', line 94

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

Parameters:

  • repo (String)

    The repository the object resides in. Currently recoginzes "dor" and "sdr".

  • druid (String)

    The id of the object

  • workflow (String)

    The name of the workflow

Returns:

  • (String)

    XML of the workflow

Raises:

  • (ArgumentError)


110
111
112
113
114
# File 'lib/dor/workflow/client/workflow_routes.rb', line 110

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"]

Parameters:

  • pid (String)

    of druid

  • repo (String) (defaults to: 'dor')

    repository for the object

Returns:

  • (Array<String>)

    list of worklows



165
166
167
168
# File 'lib/dor/workflow/client/workflow_routes.rb', line 165

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