Class: Katello::PulpTaskStatus

Inherits:
TaskStatus show all
Defined in:
app/models/katello/pulp_task_status.rb

Direct Known Subclasses

PulpSyncStatus

Constant Summary collapse

WAIT_TIMES =
[0.5, 1, 2, 4, 8, 16]
WAIT_TIME_STEP =
5

Constants included from Util::TaskStatus

Util::TaskStatus::TYPES

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from TaskStatus

#as_json, #canceled?, #description, #error?, #finished?, #generate_description, #human_readable_message, #humanize_parameters, #humanize_type, #initialize, make, #merge_pulp_task!, #message, #overall_status, #pending?, #pending_message, #refresh_pulp, #result_description, #rmi_error_description, #system_filter_clause

Methods inherited from Model

#destroy!

Constructor Details

This class inherits a constructor from Katello::TaskStatus

Class Method Details

.any_task_running(async_tasks) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/models/katello/pulp_task_status.rb', line 88

def self.any_task_running(async_tasks)
  async_tasks.each do |t|
    t.refresh
    sleep 0.5 # do not overload backend engines
    if !t.finished?
      return true
    elsif t.error?
      fail t.as_json
    end
  end
  return false
end

.dump_state(pulp_status, task_status) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/models/katello/pulp_task_status.rb', line 62

def self.dump_state(pulp_status, task_status)
  if !pulp_status.key?(:state) && pulp_status[:result] == "success"
    # Note: if pulp_status doesn't contain a state, the status is coming from pulp sync history
    pulp_status[:state] = Status::FINISHED.to_s
  end

  task_status.attributes = {
    :uuid => pulp_status[:task_id],
    :state => pulp_status[:state] || pulp_status[:result],
    :start_time => pulp_status[:start_time] || pulp_status[:start_time],
    :finish_time => pulp_status[:finish_time],
    :progress => pulp_status,
    :result => pulp_status[:result].nil? ? {:errors => [pulp_status[:exception], pulp_status[:traceback]]} : pulp_status[:result]
  }
  task_status.save! unless task_status.new_record?
  task_status
end

.poll_wait_time(attempts) ⇒ Object



101
102
103
104
105
106
107
# File 'app/models/katello/pulp_task_status.rb', line 101

def self.poll_wait_time(attempts)
  if attempts >= WAIT_TIMES.length * WAIT_TIME_STEP
    WAIT_TIMES.last
  else
    WAIT_TIMES[(attempts.to_i / WAIT_TIME_STEP)]
  end
end

.refresh(task_status) ⇒ Object



80
81
82
83
84
85
86
# File 'app/models/katello/pulp_task_status.rb', line 80

def self.refresh(task_status)
  pulp_task = Katello.pulp_server.resources.task.poll(task_status.uuid)

  self.dump_state(pulp_task, task_status)
  task_status.after_refresh
  task_status
end

.using_pulp_task(pulp_status) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/models/katello/pulp_task_status.rb', line 49

def self.using_pulp_task(pulp_status)
  if pulp_status.is_a? TaskStatus
    pulp_status
  else
    task_id = pulp_status[:task_id] || pulp_status[:spawned_tasks].first[:task_id]
    pulp_status = Katello.pulp_server.resources.task.poll(task_id)

    task_status = TaskStatus.find_by(:uuid => task_id)
    task_status = self.new { |t| yield t if block_given? } if task_status.nil?
    PulpTaskStatus.dump_state(pulp_status, task_status)
  end
end

.wait_for_tasks(async_tasks) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/models/katello/pulp_task_status.rb', line 24

def self.wait_for_tasks(async_tasks)
  async_tasks = async_tasks.collect do |t|
    unless t.nil?
      PulpTaskStatus.using_pulp_task(t)
    end
  end

  timeout_count = 0
  attempts = 0
  loop do
    begin
      break unless any_task_running(async_tasks)
      timeout_count = 0
      attempts += 1
    rescue RestClient::RequestTimeout => e
      timeout_count += 1
      Rails.logger.error "Timeout in pulp occurred: #{timeout_count}"
      raise e if timeout_count >= 10 #10 timeouts in a row, lets bail
      sleep 50 #if we got a timeout, lets backoff and let it catchup
    end
    sleep poll_wait_time(attempts)
  end
  async_tasks
end

Instance Method Details

#affected_unitsObject



16
17
18
# File 'app/models/katello/pulp_task_status.rb', line 16

def affected_units
  self.result['num_changes']
end

#after_refreshObject



12
13
14
# File 'app/models/katello/pulp_task_status.rb', line 12

def after_refresh
  #potentially used by child class, see PulpSyncStatus for example
end

#errorObject



20
21
22
# File 'app/models/katello/pulp_task_status.rb', line 20

def error
  self.result[:errors][0] if self.error? && self.result[:errors]
end

#refreshObject



8
9
10
# File 'app/models/katello/pulp_task_status.rb', line 8

def refresh
  PulpTaskStatus.refresh(self)
end