Class: Actions::BulkAction
Instance Method Summary
collapse
#humanized_output
Methods inherited from EntryAction
#action_subject, all_action_names, #delay, #drop_all_locks!, #resource_locks, serializer_class
#exclusive_lock!, #link!, #lock!
#serialize_args
Methods inherited from Base
#already_running?, #humanized_errors, #humanized_output, #notify_paused, #serializer_class, #task, #task_input, #task_output
included, #sync_execution_plan_to_task
included, #log_task_state_change
Instance Method Details
#batch(from, size) ⇒ Object
68
69
70
|
# File 'app/lib/actions/bulk_action.rb', line 68
def batch(from, size)
input[:target_ids].slice(from, size)
end
|
#check_targets!(targets) ⇒ Object
61
62
63
64
65
66
|
# File 'app/lib/actions/bulk_action.rb', line 61
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'app/lib/actions/bulk_action.rb', line 46
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
|
36
37
38
39
40
41
42
|
# File 'app/lib/actions/bulk_action.rb', line 36
def humanized_input
a_sub_task = task.sub_tasks.first
if a_sub_task
[a_sub_task.humanized[:action].to_s.downcase] +
Array(a_sub_task.humanized[:input]) + ['...']
end
end
|
#humanized_name ⇒ Object
24
25
26
27
28
29
30
|
# File 'app/lib/actions/bulk_action.rb', line 24
def humanized_name
if task.sub_tasks.first
task.sub_tasks.first.humanized[:action]
else
_('Bulk action')
end
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
|
# File 'app/lib/actions/bulk_action.rb', line 10
def plan(action_class, targets, *args, concurrency_limit: nil, **kwargs)
check_targets!(targets)
limit_concurrency_level!(concurrency_limit) if 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
32
33
34
|
# File 'app/lib/actions/bulk_action.rb', line 32
def rescue_strategy
Dynflow::Action::Rescue::Skip
end
|
#run(event = nil) ⇒ Object
20
21
22
|
# File 'app/lib/actions/bulk_action.rb', line 20
def run(event = nil)
super unless event == Dynflow::Action::Skip
end
|
#total_count ⇒ Object
72
73
74
|
# File 'app/lib/actions/bulk_action.rb', line 72
def total_count
input[:target_ids].count
end
|