Class: ForemanTasks::Task::DynflowTask

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ForemanTasks::Task

authorized_resource_name, #owner, #paused?, #pending?, search_by_generic_resource, search_by_owner, #self_and_parents, #username

Class Method Details

.consistency_checkObject



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

def self.consistency_check
  fixed_count = 0
  logger = Foreman::Logging.logger('foreman-tasks')
  self.running.each do |task|
    begin
      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
      Foreman::Logging.exception("Failed at consistency check for task #{task.id}", e, :logger => 'foreman-tasks')
    end
  end
  return fixed_count
end

.new_for_execution_plan(execution_plan_id, data) ⇒ Object



119
120
121
122
123
# File 'app/models/foreman_tasks/task/dynflow_task.rb', line 119

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

Instance Method Details

#cancelObject



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

def cancel
  execution_plan.cancel.any?
end

#cancellable?Boolean

Returns:

  • (Boolean)


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

def cancellable?
  execution_plan.cancellable?
end

#cancellable_action?(action) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#cli_exampleObject



74
75
76
77
78
# File 'app/models/foreman_tasks/task/dynflow_task.rb', line 74

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

#execution_planObject



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

def execution_plan
  @execution_plan ||= ForemanTasks.dynflow.world.persistence.load_execution_plan(external_id)
end

#failed_stepsObject



59
60
61
# File 'app/models/foreman_tasks/task/dynflow_task.rb', line 59

def failed_steps
  execution_plan.steps_in_state(:skipped, :skipping, :error)
end

#get_humanized(method) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/models/foreman_tasks/task/dynflow_task.rb', line 85

def get_humanized(method)
  @humanized_cache ||= {}
  if [:name, :input, :output, :error].include?(method)
    method = "humanized_#{method}".to_sym
  end
  Match! method, :humanized_name, :humanized_input, :humanized_output, :humanized_errors
  @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



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

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

#inputObject



51
52
53
# File 'app/models/foreman_tasks/task/dynflow_task.rb', line 51

def input
  main_action.respond_to?(:task_input) && main_action.task_input
end

#main_actionObject



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

def main_action
  return @main_action if @main_action
  execution_plan.root_plan_step.action execution_plan
end

#outputObject



55
56
57
# File 'app/models/foreman_tasks/task/dynflow_task.rb', line 55

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

#progressObject



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

def progress
  execution_plan.progress
end

#resumable?Boolean

Returns:

  • (Boolean)


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

def resumable?
  execution_plan.state == :paused
end

#running_stepsObject



63
64
65
# File 'app/models/foreman_tasks/task/dynflow_task.rb', line 63

def running_steps
  execution_plan.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
# File 'app/models/foreman_tasks/task/dynflow_task.rb', line 8

def update_from_dynflow(data)
  self.external_id    = data[:id]
  self.started_at     = data[:started_at]
  self.ended_at       = data[:ended_at]
  self.state          = data[:state].to_s
  self.result         = data[:result].to_s
  self.start_at       = data[:start_at] if data[:start_at]
  self.start_before   = data[:start_before] if data[:start_before]
  self.parent_task_id ||= begin
                            if main_action.caller_execution_plan_id
                              DynflowTask.find_by_external_id!(main_action.caller_execution_plan_id).id
                            end
                          end
  self.label          ||= main_action.class.name
  changes             = self.changes
  self.save!
  return changes
end