Class: Actions::RemoteExecution::RunHostsJob

Inherits:
ActionWithSubPlans
  • Object
show all
Extended by:
ApipieDSL::Class
Includes:
ObservableAction, Actions::RecurringAction, EventHelpers
Defined in:
app/lib/actions/remote_execution/run_hosts_job.rb

Defined Under Namespace

Classes: CheckOnProxyActions, Jail

Class Method Summary collapse

Instance Method Summary collapse

Methods included from EventHelpers

#emit_event, included

Class Method Details

.event_statesObject



164
165
166
# File 'app/lib/actions/remote_execution/run_hosts_job.rb', line 164

def self.event_states
  [:success, :failure, :running]
end

Instance Method Details

#batch(from, size) ⇒ Object



115
116
117
# File 'app/lib/actions/remote_execution/run_hosts_job.rb', line 115

def batch(from, size)
  hosts.offset(from).limit(size)
end

#create_sub_plansObject



44
45
46
47
48
49
50
51
52
53
# File 'app/lib/actions/remote_execution/run_hosts_job.rb', line 44

def create_sub_plans
  proxy_selector = RemoteExecutionProxySelector.new

  current_batch.map do |host|
    # composer creates just "pattern" for template_invocations because target is evaluated
    # during actual run (here) so we build template invocations from these patterns
    template_invocation = job_invocation.pattern_template_invocation_for_host(host).deep_clone
    trigger(RunHostJob, job_invocation, host, template_invocation, proxy_selector)
  end
end

#delay(delay_options, job_invocation) ⇒ Object



22
23
24
25
26
27
# File 'app/lib/actions/remote_execution/run_hosts_job.rb', line 22

def delay(delay_options, job_invocation)
  task.add_missing_task_groups(job_invocation.task_group)
  job_invocation.targeting.resolve_hosts! if job_invocation.targeting.static? && !job_invocation.targeting.resolved?
  input.update :job_invocation => job_invocation.to_action_input
  super delay_options, job_invocation
end

#emit_running_event(plan) ⇒ Object



168
169
170
# File 'app/lib/actions/remote_execution/run_hosts_job.rb', line 168

def emit_running_event(plan)
  emit_event(plan, :running)
end

#finalizeObject



83
84
85
86
87
88
89
# File 'app/lib/actions/remote_execution/run_hosts_job.rb', line 83

def finalize
  job_invocation.password = job_invocation.key_passphrase = job_invocation.effective_user_password = nil
  job_invocation.save!

  Rails.logger.debug "cleaning cache for keys that begin with 'job_invocation_#{job_invocation.id}'"
  Rails.cache.delete_matched(cache_deletion_query(job_invocation.id))
end

#hostsObject



132
133
134
# File 'app/lib/actions/remote_execution/run_hosts_job.rb', line 132

def hosts
  job_invocation.targeting.hosts.order("#{TargetingHost.table_name}.id")
end

#humanized_inputObject



152
153
154
# File 'app/lib/actions/remote_execution/run_hosts_job.rb', line 152

def humanized_input
  input.fetch(:job_invocation, {}).fetch(:description, '')
end

#humanized_nameObject



156
157
158
# File 'app/lib/actions/remote_execution/run_hosts_job.rb', line 156

def humanized_name
  '%s:' % _(super)
end

#initiateObject



119
120
121
122
123
# File 'app/lib/actions/remote_execution/run_hosts_job.rb', line 119

def initiate
  output[:host_count] = total_count
  output[:remote_triggered_count] = 0
  super
end

#job_invocationObject



111
112
113
# File 'app/lib/actions/remote_execution/run_hosts_job.rb', line 111

def job_invocation
  @job_invocation ||= JobInvocation.find(job_invocation_id)
end

#job_invocation_idObject



107
108
109
# File 'app/lib/actions/remote_execution/run_hosts_job.rb', line 107

def job_invocation_id
  input[:job_invocation_id] || input.fetch(:job_invocation, {})[:id]
end

#notify_on_failure(_plan) ⇒ Object



99
100
101
102
103
104
105
# File 'app/lib/actions/remote_execution/run_hosts_job.rb', line 99

def notify_on_failure(_plan)
  job_invocation.build_notification.deliver!

  if [RexMailNotification::FAILED_JOBS, RexMailNotification::ALL_JOBS].include?(mail_notification_preference&.interval)
    RexJobMailer.job_finished(job_invocation, subject: _("REX job has failed - %s") % job_invocation.to_s).deliver_now
  end
end

#notify_on_success(_plan) ⇒ Object



91
92
93
94
95
96
97
# File 'app/lib/actions/remote_execution/run_hosts_job.rb', line 91

def notify_on_success(_plan)
  job_invocation.build_notification.deliver!

  if [RexMailNotification::SUCCEEDED_JOBS, RexMailNotification::ALL_JOBS].include?(mail_notification_preference&.interval)
    RexJobMailer.job_finished(job_invocation, subject: _("REX job has succeeded - %s") % job_invocation.to_s).deliver_now
  end
end

#on_planning_finishedObject



77
78
79
80
81
# File 'app/lib/actions/remote_execution/run_hosts_job.rb', line 77

def on_planning_finished
  trigger_remote_batch
  plan_event(Actions::TriggerProxyBatch::TriggerLastBatch, nil, step_id: input[:trigger_run_step_id])
  super
end

#plan(job_invocation) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/lib/actions/remote_execution/run_hosts_job.rb', line 29

def plan(job_invocation)
  job_invocation.task_group.save! if job_invocation.task_group.try(:new_record?)
  task.add_missing_task_groups(job_invocation.task_group) if job_invocation.task_group
  features = job_invocation.pattern_templates.flat_map { |t| t.remote_execution_features.pluck(:label) }.uniq
  action_subject(job_invocation, job_features: features)
  job_invocation.targeting.resolve_hosts! if job_invocation.targeting.dynamic? || !job_invocation.targeting.resolved?
  set_up_concurrency_control job_invocation
  input.update(:job_category => job_invocation.job_category)
  plan_self(:job_invocation_id => job_invocation.id)
  provider = job_invocation.pattern_template_invocations.first&.template&.provider
  input[:proxy_batch_size] ||= provider&.proxy_batch_size || Setting['foreman_tasks_proxy_batch_size']
  trigger_action = plan_action(Actions::TriggerProxyBatch, batch_size: proxy_batch_size, total_count: hosts.count)
  input[:trigger_run_step_id] = trigger_action.run_step_id
end

#proxy_batch_sizeObject



160
161
162
# File 'app/lib/actions/remote_execution/run_hosts_job.rb', line 160

def proxy_batch_size
  input[:proxy_batch_size]
end

#queueObject



18
19
20
# File 'app/lib/actions/remote_execution/run_hosts_job.rb', line 18

def queue
  ForemanRemoteExecution::DYNFLOW_QUEUE
end

#rescue_strategyObject



140
141
142
# File 'app/lib/actions/remote_execution/run_hosts_job.rb', line 140

def rescue_strategy
  ::Dynflow::Action::Rescue::Skip
end

#run(event = nil) ⇒ Object



144
145
146
147
148
149
150
# File 'app/lib/actions/remote_execution/run_hosts_job.rb', line 144

def run(event = nil)
  if event == Dynflow::Action::Skip
    plan_event(Dynflow::Action::Skip, nil, step_id: input[:trigger_run_step_id])
  else
    super
  end
end

#set_up_concurrency_control(invocation) ⇒ Object



136
137
138
# File 'app/lib/actions/remote_execution/run_hosts_job.rb', line 136

def set_up_concurrency_control(invocation)
  limit_concurrency_level! invocation.concurrency_level unless invocation.concurrency_level.nil?
end

#spawn_plansObject



55
56
57
58
59
# File 'app/lib/actions/remote_execution/run_hosts_job.rb', line 55

def spawn_plans
  super
ensure
  trigger_remote_batch
end

#total_countObject



125
126
127
128
129
130
# File 'app/lib/actions/remote_execution/run_hosts_job.rb', line 125

def total_count
  # For compatibility with already existing tasks
  return output[:total_count] || hosts.count unless output.has_key?(:host_count) || task.pending?

  output[:host_count] || hosts.count
end

#trigger_remote_batchObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/lib/actions/remote_execution/run_hosts_job.rb', line 61

def trigger_remote_batch
  remaining = output[:planned_count] - output[:remote_triggered_count]
  return if remaining.zero?
  batches_ready = remaining / proxy_batch_size
  if concurrency_limit
    count = remaining
  else
    return unless batches_ready > 0
    count = proxy_batch_size * batches_ready
  end
  batches_ready = [1, batches_ready].max

  plan_event(Actions::TriggerProxyBatch::TriggerNextBatch[batches_ready], nil, step_id: input[:trigger_run_step_id])
  output[:remote_triggered_count] += count
end