Class: ForemanTasks::Task::DynflowTask

Inherits:
ForemanTasks::Task show all
Includes:
Algebrick::TypeCheck
Defined in:
app/models/foreman_tasks/task/dynflow_task.rb

Constant Summary

Constants included from Search

Search::SUPPORTED_DURATION_FORMAT

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ForemanTasks::Task

#action, #action_continuous_output, #add_missing_task_groups, authorized_resource_name, #build_notifications, #check_permissions_after_save, #delayed?, #execution_type, #notification_recipients_ids, #owner, #paused?, #pending?, #recurring?, #scheduled?, #self_and_parents, #sub_tasks_counts, #to_label, #username

Methods included from Search

#search_by_duration, #search_by_generic_resource, #search_by_taxonomy

Class Method Details

.consistency_checkObject



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'app/models/foreman_tasks/task/dynflow_task.rb', line 220

def self.consistency_check
  fixed_count = 0
  logger = Foreman::Logging.logger('foreman-tasks')
  running.each do |task|
    changes = task.update_from_dynflow(task.execution_plan.to_hash)
    unless changes.empty?
      fixed_count += 1
      logger.warn('Task %s updated at consistency check: %s' % [task.id, changes.inspect])
    end
  rescue => e
    # if we fail updating the data from dynflow, it usually means there is something
    # odd with the data consistency and at this point it is not possible to resume, switching
    # the task to stopped/error
    task.update(:state => 'stopped', :result => 'error')
    Foreman::Logging.exception("Failed at consistency check for task #{task.id}", e, :logger => 'foreman-tasks')
  end
  fixed_count
end

.model_nameObject



246
247
248
# File 'app/models/foreman_tasks/task/dynflow_task.rb', line 246

def self.model_name
  superclass.model_name
end

.new_for_execution_plan(execution_plan_id, data) ⇒ Object



239
240
241
242
243
244
# File 'app/models/foreman_tasks/task/dynflow_task.rb', line 239

def self.new_for_execution_plan(execution_plan_id, data)
  new(:external_id => execution_plan_id,
      :state => data[:state].to_s,
      :result => data[:result].to_s,
      :user_id => User.current.try(:id))
end

Instance Method Details

#abortObject



36
37
38
# File 'app/models/foreman_tasks/task/dynflow_task.rb', line 36

def abort
  execution_plan!.cancel(true).any?
end

#active_job?Boolean

Returns:

  • (Boolean)


166
167
168
169
170
# File 'app/models/foreman_tasks/task/dynflow_task.rb', line 166

def active_job?
  return false unless execution_plan.present? &&
                      execution_plan.root_plan_step.present?
  execution_plan_action.class == ::Dynflow::ActiveJob::QueueAdapters::JobWrapper
end

#active_job_action(klass, args) ⇒ Object

The class for ActiveJob jobs in Dynflow, JobWrapper is not expected to implement any humanized actions. Individual jobs are expected to implement humanized_* methods for foreman-tasks integration.



159
160
161
162
163
164
# File 'app/models/foreman_tasks/task/dynflow_task.rb', line 159

def active_job_action(klass, args)
  return if klass.blank?
  if (active_job_class = klass.safe_constantize)
    active_job_class.new(*args)
  end
end

#active_job_dataObject



172
173
174
175
176
177
178
179
180
181
# File 'app/models/foreman_tasks/task/dynflow_task.rb', line 172

def active_job_data
  args = if execution_plan.delay_record
           execution_plan.delay_record.args.first
         else
           execution_plan_action.input
         end
  return args['job_data'] if args.key?('job_data')
  # For dynflow <= 1.2.1
  { 'job_class' => args['job_class'], 'arguments' => args['job_arguments'] }
end

#cancelObject



32
33
34
# File 'app/models/foreman_tasks/task/dynflow_task.rb', line 32

def cancel
  execution_plan!.cancel.any?
end

#cancellable?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'app/models/foreman_tasks/task/dynflow_task.rb', line 28

def cancellable?
  execution_plan.try(:cancellable?)
end

#cancellable_action?(action) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'app/models/foreman_tasks/task/dynflow_task.rb', line 44

def cancellable_action?(action)
  action.is_a?(::Dynflow::Action::Cancellable)
end

#cli_exampleObject



137
138
139
# File 'app/models/foreman_tasks/task/dynflow_task.rb', line 137

def cli_example
  main_action.cli_example if main_action.respond_to?(:cli_example)
end

#delayed_planObject



73
74
75
# File 'app/models/foreman_tasks/task/dynflow_task.rb', line 73

def delayed_plan
  ForemanTasks.dynflow.world.persistence.load_delayed_plan(external_id) if state == :scheduled
end

#execution_plan(silence_exception = true) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/foreman_tasks/task/dynflow_task.rb', line 53

def execution_plan(silence_exception = true)
  return @execution_plan if defined?(@execution_plan)
  execution_plan = ForemanTasks.dynflow.world.persistence.load_execution_plan(external_id)
  # don't use invalid execution plans for our purposes
  if execution_plan.respond_to?(:valid?) && !execution_plan.valid?
    raise execution_plan.exception
  else
    @execution_plan = execution_plan
  end
  @execution_plan
rescue => e
  Foreman::Logging.exception("Could not load execution plan #{external_id} for task #{id}", e, :logger => 'foreman-tasks')
  raise e unless silence_exception
  nil
end

#execution_plan!Object



77
78
79
# File 'app/models/foreman_tasks/task/dynflow_task.rb', line 77

def execution_plan!
  execution_plan(false)
end

#execution_plan_actionObject



183
184
185
# File 'app/models/foreman_tasks/task/dynflow_task.rb', line 183

def execution_plan_action
  execution_plan.root_plan_step.action(execution_plan)
end

#execution_scheduled?Boolean

Returns:

  • (Boolean)


187
188
189
190
191
# File 'app/models/foreman_tasks/task/dynflow_task.rb', line 187

def execution_scheduled?
  main_action.nil? || main_action.respond_to?(:execution_plan) &&
    main_action.execution_plan.state == :scheduled ||
    execution_plan.state == :scheduled
end

#failed_stepsObject



95
96
97
# File 'app/models/foreman_tasks/task/dynflow_task.rb', line 95

def failed_steps
  execution_plan.try(:steps_in_state, :skipped, :skipping, :error) || []
end

#find_humanize_method_kind(method) ⇒ Object



213
214
215
216
217
218
# File 'app/models/foreman_tasks/task/dynflow_task.rb', line 213

def find_humanize_method_kind(method)
  return method if /humanized_.*/ =~ method
  if [:name, :input, :output, :error].include?(method)
    "humanized_#{method}".to_sym
  end
end

#frozenObject



69
70
71
# File 'app/models/foreman_tasks/task/dynflow_task.rb', line 69

def frozen
  delayed_plan.try(:frozen)
end

#get_humanized(method) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'app/models/foreman_tasks/task/dynflow_task.rb', line 193

def get_humanized(method)
  @humanized_cache ||= {}
  method = find_humanize_method_kind(method)
  Match! method, :humanized_name, :humanized_input, :humanized_output, :humanized_errors
  if method != :humanized_name && execution_scheduled?
    return
  elsif method == :humanized_name && main_action.nil?
    return N_(label)
  end
  @humanized_cache[method] ||= begin
                                 if main_action.respond_to? method
                                   begin
                                     main_action.send method
                                   rescue Exception => error # rubocop:disable Lint/RescueException
                                     "#{error.message} (#{error.class})\n#{error.backtrace.join "\n"}"
                                   end
                                 end
                               end
end

#humanizedObject



130
131
132
133
134
135
# File 'app/models/foreman_tasks/task/dynflow_task.rb', line 130

def humanized
  { action: get_humanized(:humanized_name),
    input:  get_humanized(:humanized_input),
    output: get_humanized(:humanized_output),
    errors: get_humanized(:humanized_errors) }
end

#inputObject



86
87
88
89
# File 'app/models/foreman_tasks/task/dynflow_task.rb', line 86

def input
  return active_job_data['arguments'] if active_job?
  main_action.task_input if main_action.respond_to?(:task_input)
end

#input_output_failed_stepsObject



103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/models/foreman_tasks/task/dynflow_task.rb', line 103

def input_output_failed_steps
  failed_steps.map do |f|
    f_action = f.action(execution_plan)
    {
      error: ({ exception_class: f.error.exception_class, message: f.error.message, backtrace: f.error.backtrace } if f.error),
      action_class: f.action_class.name,
      state: f.state,
      input: f_action.input.pretty_inspect,
      output: f_action.output.pretty_inspect,
    }
  end
end

#input_output_running_stepsObject



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/models/foreman_tasks/task/dynflow_task.rb', line 116

def input_output_running_steps
  running_steps.map do |f|
    f_action = f.action(execution_plan)
    {
      id: f_action.id,
      action_class: f.action_class.name,
      state: f.state,
      input: f_action.input.pretty_inspect,
      output: f_action.output.pretty_inspect,
      cancellable: cancellable_action?(f_action),
    }
  end
end

#labelObject



81
82
83
84
# File 'app/models/foreman_tasks/task/dynflow_task.rb', line 81

def label
  return main_action.class.name if main_action.present?
  self[:label]
end

#main_actionObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'app/models/foreman_tasks/task/dynflow_task.rb', line 141

def main_action
  return @main_action if defined?(@main_action)

  @main_action = execution_plan && execution_plan.root_plan_step.try(:action, execution_plan)
  if active_job?
    job_data = active_job_data
    begin
      @main_action = active_job_action(job_data['job_class'], job_data['arguments'])
    rescue => e
      Foreman::Logging.exception("Failed to load ActiveJob for task #{id}", e, :logger => 'foreman-tasks')
    end
  end
  @main_action
end

#outputObject



91
92
93
# File 'app/models/foreman_tasks/task/dynflow_task.rb', line 91

def output
  main_action.task_output if main_action.respond_to?(:task_output)
end

#progressObject



48
49
50
51
# File 'app/models/foreman_tasks/task/dynflow_task.rb', line 48

def progress
  progress_raw = execution_plan.try(:progress) || 0
  progress_raw.round(2)
end

#resumable?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'app/models/foreman_tasks/task/dynflow_task.rb', line 40

def resumable?
  execution_plan.try(:state) == :paused
end

#running_stepsObject



99
100
101
# File 'app/models/foreman_tasks/task/dynflow_task.rb', line 99

def running_steps
  execution_plan.try(:steps_in_state, :running, :suspended) || []
end

#update_from_dynflow(data) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/models/foreman_tasks/task/dynflow_task.rb', line 8

def update_from_dynflow(data)
  utc_zone = ActiveSupport::TimeZone.new('UTC')
  self.external_id    = data[:id]
  self.result         = map_result(data).to_s
  self.state          = data[:state].to_s
  self.started_at     = string_to_time(utc_zone, data[:started_at]) unless data[:started_at].nil?
  self.ended_at       = string_to_time(utc_zone, data[:ended_at]) unless data[:ended_at].nil?
  self.start_at       = string_to_time(utc_zone, data[:start_at]) if data[:start_at]
  self.start_before   = string_to_time(utc_zone, data[:start_before]) if data[:start_before]
  self.parent_task_id ||= begin
                            if main_action.try(:caller_execution_plan_id)
                              DynflowTask.where(:external_id => main_action.caller_execution_plan_id).first!.id
                            end
                          end
  self[:label]        ||= label
  changes = self.changes
  save!
  changes
end