Skip to content

Commit 590d89c

Browse files
authored
Merge pull request #3151 from sul-dlss/bulk-workflow-add
Move start workflow to an asynchonous bulk action
2 parents 639f643 + 1b5355f commit 590d89c

13 files changed

Lines changed: 183 additions & 33 deletions

app/components/bulk_actions_form_component.html.erb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@
5656
Deletes unpublished DOR objects
5757
</span>
5858
</div>
59+
60+
<div role='tabpanel' class='tab-pane' id='AddWorkflowJob'>
61+
<span class='help-block'>
62+
Starts a workflow for individual objects.
63+
</span>
64+
<%= render 'bulk_actions/forms/add_workflow_form', f: f %>
65+
</div>
5966

6067
<div role='tabpanel' class='tab-pane' id='SetCatkeysAndBarcodesJob'>
6168
<span class='help-block'>

app/components/bulk_actions_form_component.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ def action_types
2424
['Manage release', 'ReleaseObjectJob'],
2525
%w[Reindex RemoteIndexingJob],
2626
['Republish objects', 'RepublishJob'],
27-
['Purge object', 'PurgeJob']
27+
['Purge object', 'PurgeJob'],
28+
['Add workflow', 'AddWorkflowJob']
2829
]],
2930
['Modify objects (via form)', [
3031
['Open new object versions', 'PrepareJob'],

app/controllers/bulk_actions_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def create
2525
flash[:notice] = 'Bulk action was successfully created.'
2626
redirect_to action: :index
2727
else
28-
render :new
28+
render :new, status: :unprocessable_entity
2929
end
3030
end
3131

@@ -55,6 +55,7 @@ def bulk_action_params
5555
:action_type,
5656
:description,
5757
:pids,
58+
add_workflow: %i[workflow],
5859
manage_release: %i[tag what who to],
5960
set_governing_apo: [:new_apo_id],
6061
set_catkeys_and_barcodes: %i[

app/controllers/workflows_controller.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,7 @@ def create
8181
Argo::Indexer.reindex_pid_remotely(cocina_object.externalIdentifier)
8282

8383
msg = "Added #{wf_name}"
84-
85-
if params[:bulk]
86-
render plain: msg
87-
else
88-
redirect_to solr_document_path(cocina_object.externalIdentifier), notice: msg
89-
end
84+
redirect_to solr_document_path(cocina_object.externalIdentifier), notice: msg
9085
end
9186

9287
def history

app/forms/bulk_action_form.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
# This models the values set from the bulk action form
44
class BulkActionForm < BaseForm
55
VIRTUAL_PROPERTIES = %i[
6-
manage_release set_governing_apo set_catkeys_and_barcodes set_catkeys_and_barcodes_csv prepare register_druids
7-
create_virtual_objects import_tags set_license_and_rights_statements manage_embargo
6+
add_workflow manage_release set_governing_apo
7+
set_catkeys_and_barcodes set_catkeys_and_barcodes_csv
8+
prepare register_druids create_virtual_objects import_tags
9+
set_license_and_rights_statements manage_embargo
810
].freeze
911

1012
def initialize(model, groups:)

app/javascript/bulk.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,6 @@ function apply_apo_defaults(druids) {
104104
process_post(druids, apo_apply_defaults_url, {}, 'Defaults_applied.')
105105
}
106106

107-
function add_workflow(druids){
108-
var params = { 'wf': document.getElementById('wf').value }
109-
process_post(druids, add_workflow_url, params, 'Workflow Added');
110-
}
111-
112-
113107
function refresh_metadata(druids){
114108
process_post(druids, refresh_metadata_url, {}, "Updated.");
115109
}
@@ -282,7 +276,6 @@ document.addEventListener("turbo:load", () => {
282276
$('#set-collection-button').on('click', () => $('#set_collection').show(400))
283277

284278
$('#apply-apo-defaults-button').on('click', () => $('#apply_apo_defaults').show(400))
285-
$('#add-workflow-button').on('click', () => $('#add_workflow').show(400))
286279
$('#close-versions-button').on('click', () => $('#close').show(400))
287280
$('#show_tags').on('click', () => {
288281
$('#tag').show(400)
@@ -304,11 +297,6 @@ document.addEventListener("turbo:load", () => {
304297
$('#content_type').hide(400)
305298
})
306299

307-
$('#confirm-add-workflow-button').on('click', () => {
308-
fetch_druids(add_workflow)
309-
$('#add_workflow').hide(400)
310-
})
311-
312300
$('#confirm-refresh-metadata-button').on('click', () => {
313301
fetch_druids(refresh_metadata)
314302
$('#refresh_metadata').hide(400)

app/jobs/add_workflow_job.rb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# frozen_string_literal: true
2+
3+
##
4+
# Job to add a workflow to and object
5+
class AddWorkflowJob < GenericJob
6+
attr_reader :workflow_name
7+
8+
# @param [Integer] bulk_action_id GlobalID for a BulkAction object
9+
# @param [Hash] params additional parameters that an Argo job may need
10+
# @option params [Array] :pids required list of pids
11+
# @option params [Hash] :add_workflow required Hash of workflow options
12+
# @option add_workflow [String] :workflow the name of the workflow to start
13+
# @option params [Array] :groups the groups the user belonged to when the started the job. Required for permissions check
14+
def perform(bulk_action_id, params)
15+
super
16+
@workflow_name = params.fetch(:add_workflow).fetch(:workflow)
17+
with_bulk_action_log do |log|
18+
log.puts("#{Time.current} Starting AddWorkflowJob for BulkAction #{bulk_action_id}")
19+
update_druid_count
20+
21+
pids.each { |current_druid| start_workflow(current_druid, log) }
22+
log.puts("#{Time.current} Finished AddWorkflowJob for BulkAction #{bulk_action_id}")
23+
end
24+
end
25+
26+
private
27+
28+
def start_workflow(current_druid, log)
29+
cocina_object = Dor::Services::Client.object(current_druid).find
30+
31+
unless ability.can?(:manage_item, cocina_object)
32+
bulk_action.increment(:druid_count_fail).save
33+
log.puts("#{Time.current} Not authorized for #{current_druid}")
34+
return
35+
end
36+
37+
# check the workflow is present and active (not archived)
38+
if workflow_active?(cocina_object.externalIdentifier, cocina_object.version)
39+
bulk_action.increment(:druid_count_fail).save
40+
log.puts("#{Time.current} #{workflow_name} already exists for #{current_druid}")
41+
return
42+
end
43+
44+
client.create_workflow_by_name(cocina_object.externalIdentifier,
45+
workflow_name,
46+
version: cocina_object.version)
47+
log.puts("#{Time.current} started #{workflow_name} for #{current_druid}")
48+
49+
bulk_action.increment(:druid_count_success).save
50+
rescue StandardError => e
51+
log.puts("#{Time.current} Workflow creation failed #{e.class} #{e.message}")
52+
bulk_action.increment(:druid_count_fail).save
53+
end
54+
55+
# Fetches the workflow from the workflow service and checks to see if it's active
56+
def workflow_active?(druid, version)
57+
workflow = client.workflow(pid: druid, workflow_name: workflow_name)
58+
workflow.active_for?(version: version)
59+
end
60+
61+
def client
62+
@client ||= WorkflowClientFactory.build
63+
end
64+
end

app/models/bulk_action.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ class BulkAction < ApplicationRecord
55
belongs_to :user
66
validates :action_type,
77
inclusion: {
8-
in: %w[GenericJob
8+
in: %w[AddWorkflowJob
9+
GenericJob
910
DescmetadataDownloadJob
1011
ReleaseObjectJob
1112
RemoteIndexingJob

app/services/bulk_action_persister.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def persist
2222

2323
attr_reader :bulk_action_form
2424

25-
delegate :pids, :manage_release, :set_governing_apo,
25+
delegate :pids, :add_workflow, :manage_release, :set_governing_apo,
2626
:set_catkeys_and_barcodes, :groups, :prepare, :create_virtual_objects,
2727
:import_tags, :set_license_and_rights_statements,
2828
to: :bulk_action_form
@@ -59,6 +59,7 @@ def active_job_class
5959
def job_params
6060
{
6161
pids: pids.split,
62+
add_workflow: add_workflow,
6263
manage_release: manage_release,
6364
set_catkeys_and_barcodes: set_catkeys_and_barcodes,
6465
set_governing_apo: set_governing_apo,
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<% f.fields_for :add_workflow do |add_workflow_fields| %>
2+
<div class='mt-2 mb-3'>
3+
<%= add_workflow_fields.label :workflow %>
4+
<%= add_workflow_fields.select :workflow, workflow_options, {}, class: 'form-select' %>
5+
</div>
6+
<% end %>

0 commit comments

Comments
 (0)