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 rubocop:disable Metrics/ClassLength

Instance Method Summary collapse

Constructor Details

#initialize(requestor:) ⇒ WorkflowRoutes

Returns a new instance of WorkflowRoutes.



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

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:



239
240
241
242
# File 'lib/dor/workflow/client/workflow_routes.rb', line 239

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



232
233
234
# File 'lib/dor/workflow/client/workflow_routes.rb', line 232

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



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

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, version: nil, lane_id: 'default') ⇒ 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

  • lane_id (String) (defaults to: 'default')

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

  • version (Integer) (defaults to: nil)

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

Returns:

  • (Boolean)

    always true



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

def create_workflow_by_name(druid, workflow_name, version: nil, lane_id: 'default')
  params = { 'lane-id' => lane_id }
  if version
    params['version'] = version
  else
    Deprecation.warn(self, 'Calling create_workflow_by_name without passing version is deprecated and will result in an error in dor-workflow-client 4.0')
  end
  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



297
298
299
300
# File 'lib/dor/workflow/client/workflow_routes.rb', line 297

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

#delete_workflow(repo, druid, workflow, version: nil) ⇒ Boolean

Deletes a workflow from a particular repository and druid. This is only used by Hydrus.

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

  • version (Integer) (defaults to: nil)

    The version of the workflow to delete

Returns:

  • (Boolean)

    always true



283
284
285
286
287
288
289
290
291
292
# File 'lib/dor/workflow/client/workflow_routes.rb', line 283

def delete_workflow(repo, druid, workflow, version: nil)
  qs_args = if version
              "?version=#{version}"
            else
              Deprecation.warn(self, 'Calling delete_workflow without passing version is deprecated and will result in an error in dor-workflow-client 4.0')
              ''
            end
  requestor.request "#{repo}/objects/#{druid}/workflows/#{workflow}#{qs_args}", 'delete'
  true
end

#process(pid:, workflow_name:, process:) ⇒ Workflow::Response::Process

Parameters:

  • pid (String)

    id of object

  • workflow_name (String)

    The name of the workflow

  • process (String)

    The name of the workflow step

Returns:



273
274
275
# File 'lib/dor/workflow/client/workflow_routes.rb', line 273

def process(pid:, workflow_name:, process:)
  workflow(pid: pid, workflow_name: workflow_name).process_for_recent_version(name: process)
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 base URL.

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

Parameters:

  • druid (String)

    The id of the object

  • workflow (String)

    The name of the workflow

  • process (String)

    The name of the workflow step

  • error_msg (String)

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

  • error_text (String) (defaults to: nil)

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

Returns:



222
223
224
225
226
# File 'lib/dor/workflow/client/workflow_routes.rb', line 222

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 base URL. As an example:

PUT "/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

  • :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

  • :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
# File 'lib/dor/workflow/client/workflow_routes.rb', line 73

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 base URL.

PUT "/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

  • process (String)

    The name of the workflow step

  • 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



200
201
202
203
# File 'lib/dor/workflow/client/workflow_routes.rb', line 200

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 base URL. 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)


106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/dor/workflow/client/workflow_routes.rb', line 106

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: nil, pid:, workflow_name:) ⇒ Workflow::Response::Workflow

Parameters:

  • repo (String) (defaults to: nil)

    repository of the object

  • pid (String)

    id of object

  • workflow_name (String)

    The name of the workflow

Returns:



263
264
265
266
267
# File 'lib/dor/workflow/client/workflow_routes.rb', line 263

def workflow(repo: nil, pid:, workflow_name:)
  Deprecation.warn(self, 'passing the repo parameter is deprecated and will be removed in the next major versions') if repo
  xml = fetch_workflow(druid: pid, workflow: workflow_name)
  Workflow::Response::Workflow.new(xml: xml)
end

#workflow_status(*args) ⇒ String

Retrieves the process status of the given workflow for the given object identifier rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize

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:



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/dor/workflow/client/workflow_routes.rb', line 129

def workflow_status(*args)
  case args.length
  when 4
    Deprecation.warn(self, 'Calling workflow_status with positional args is deprecated, use kwargs instead')
    (_repo, druid, workflow, process) = *args
  when 1
    opts = args.first
    repo = opts[:repo]
    Deprecation.warn(self, 'Passing `:repo` to workflow_status is deprecated and can be omitted') if repo
    druid = opts[:druid]
    workflow = opts[:workflow]
    process = opts[:process]
  end
  workflow_md = fetch_workflow(druid: druid, workflow: 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(*args) ⇒ String

Retrieves the raw XML for the given workflow rubocop:disable Metrics/MethodLength

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)


160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/dor/workflow/client/workflow_routes.rb', line 160

def workflow_xml(*args)
  case args.length
  when 3
    Deprecation.warn(self, 'Calling workflow_xml with positional args is deprecated, use kwargs instead')
    (repo, druid, workflow) = *args
  when 1
    opts = args.first
    repo = opts[:repo]
    Deprecation.warn(self, 'Passing `:repo` to workflow_xml is deprecated and can be omitted') if repo
    druid = opts[:druid]
    workflow = opts[:workflow]
  end

  raise ArgumentError, 'missing workflow' unless workflow
  return requestor.request "#{repo}/objects/#{druid}/workflows/#{workflow}" if repo

  fetch_workflow(druid: druid, workflow: workflow)
end

#workflows(pid, repo = nil) ⇒ 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: nil)

    repository for the object

Returns:

  • (Array<String>)

    list of worklows



253
254
255
256
257
# File 'lib/dor/workflow/client/workflow_routes.rb', line 253

def workflows(pid, repo = nil)
  Deprecation.warn(self, 'Passing the second argument (repo) to workflows is deprecated and can be omitted') if repo
  xml_doc = Nokogiri::XML(fetch_workflow(druid: pid, workflow: ''))
  xml_doc.xpath('//workflow').collect { |workflow| workflow['id'] }
end