Class: Actions::BulkAction
- Inherits:
-
ActionWithSubPlans
- Object
- ActionWithSubPlans
- Actions::BulkAction
- Defined in:
- app/lib/actions/bulk_action.rb
Instance Method Summary collapse
- #batch(from, size) ⇒ Object
- #check_targets!(targets) ⇒ Object
- #create_sub_plans ⇒ Object
- #humanized_input ⇒ Object
- #humanized_name ⇒ Object
-
#plan(action_class, targets, *args, concurrency_limit: nil, **kwargs) ⇒ Object
Parameters: actions_class:: Class of action to trigger on targets targets:: Array of objects on which the action_class should be triggered *args:: Arguments that all the targets share.
- #rescue_strategy ⇒ Object
- #run(event = nil) ⇒ Object
- #total_count ⇒ Object
Instance Method Details
#batch(from, size) ⇒ Object
64 65 66 |
# File 'app/lib/actions/bulk_action.rb', line 64 def batch(from, size) input[:target_ids].slice(from, size) end |
#check_targets!(targets) ⇒ Object
57 58 59 60 61 62 |
# File 'app/lib/actions/bulk_action.rb', line 57 def check_targets!(targets) raise Foreman::Exception, N_('Empty bulk action') if targets.empty? if targets.map(&:class).uniq.length > 1 raise Foreman::Exception, N_('The targets are of different types') end end |
#create_sub_plans ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'app/lib/actions/bulk_action.rb', line 42 def create_sub_plans action_class = input[:action_class].constantize target_class = input[:target_class].constantize targets = target_class.unscoped.where(:id => current_batch) missing = Array.new((current_batch - targets.map(&:id)).count) { nil } args = input[:args] args += [input[:kwargs]] unless input[:kwargs].empty? (targets + missing).map do |target| trigger(action_class, target, *args) end end |
#humanized_input ⇒ Object
33 34 35 36 37 38 |
# File 'app/lib/actions/bulk_action.rb', line 33 def humanized_input with_sub_task do |a_sub_task| [a_sub_task.humanized[:action].to_s.downcase] + Array(a_sub_task.humanized[:input]) + ['...'] end end |
#humanized_name ⇒ Object
25 26 27 |
# File 'app/lib/actions/bulk_action.rb', line 25 def humanized_name with_sub_task { |t| t.humanized[:action] } || _('Bulk action') end |
#plan(action_class, targets, *args, concurrency_limit: nil, **kwargs) ⇒ Object
Parameters:
- actions_class
Class of action to trigger on targets
- targets
Array of objects on which the action_class should be triggered
- *args
Arguments that all the targets share
10 11 12 13 14 15 16 17 18 19 |
# File 'app/lib/actions/bulk_action.rb', line 10 def plan(action_class, targets, *args, concurrency_limit: nil, **kwargs) check_targets!(targets) extracted_concurrency_limit = extract_concurrency_limit(args, concurrency_limit) limit_concurrency_level!(extracted_concurrency_limit) if extracted_concurrency_limit plan_self(:action_class => action_class.to_s, :target_ids => targets.map(&:id), :target_class => targets.first.class.to_s, :args => args, :kwargs => kwargs) end |
#rescue_strategy ⇒ Object
29 30 31 |
# File 'app/lib/actions/bulk_action.rb', line 29 def rescue_strategy Dynflow::Action::Rescue::Skip end |
#run(event = nil) ⇒ Object
21 22 23 |
# File 'app/lib/actions/bulk_action.rb', line 21 def run(event = nil) super unless event == Dynflow::Action::Skip end |
#total_count ⇒ Object
68 69 70 |
# File 'app/lib/actions/bulk_action.rb', line 68 def total_count input[:target_ids].count end |