Class: Katello::Pulp3::Task

Inherits:
Object
  • Object
show all
Defined in:
app/services/katello/pulp3/task.rb

Constant Summary collapse

WAITING =

A call report Looks like: “task”:“/pulp/api/v3/tasks/5/”

"pulp_href":"/pulp/api/v3/tasks/4/",
"pulp_created":"2019-02-21T19:50:40.476767Z",
"job_id":"d0359658-d926-47a2-b430-1b2092b3bd86",
"state":"completed",
"name":"pulp_file.app.tasks.publishing.publish",
"started_at":"2019-02-21T19:50:40.556002Z",
"finished_at":"2019-02-21T19:50:40.618397Z",
"non_fatal_errors":[

],
"error":null,
"worker":"/pulp/api/v3/workers/1/",
"parent":null,
"spawned_tasks":[

],
"progress_reports":[

],
"created_resources":[
   "/pulp/api/v3/publications/1/"
]

'waiting'.freeze
SKIPPED =
'skipped'.freeze
RUNNING =
'running'.freeze
COMPLETED =
'completed'.freeze
FAILED =
'failed'.freeze
CANCELED =
'canceled'.freeze
FINISHED_STATES =
[COMPLETED, FAILED, CANCELED, SKIPPED].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(smart_proxy, data) ⇒ Task



44
45
46
47
48
49
50
51
52
53
# File 'app/services/katello/pulp3/task.rb', line 44

def initialize(smart_proxy, data)
  @smart_proxy = smart_proxy
  if (href = data['task'])
    @href = href
  else
    @pulp_data = data.with_indifferent_access
    @href = @pulp_data['pulp_href']
    Rails.logger.error("Got empty pulp_href on #{@pulp_data}") if @href.nil?
  end
end

Instance Attribute Details

#pulp_dataObject (readonly)

needed for serialization in dynflow



41
42
43
# File 'app/services/katello/pulp3/task.rb', line 41

def pulp_data
  @pulp_data
end

Class Method Details

.publication_href(tasks) ⇒ Object



63
64
65
66
67
68
69
# File 'app/services/katello/pulp3/task.rb', line 63

def self.publication_href(tasks)
  tasks = [tasks] unless tasks.is_a?(Array)
  publication_hrefs = tasks.map { |task| task[:created_resources] }.flatten
  publication_hrefs = publication_hrefs.select { |href| ::Katello::Pulp3::Repository.publication_href?(href) }
  Rails.logger.error("Got multiple publication hrefs for pulp task: #{tasks}") if publication_hrefs.length > 2
  publication_hrefs.last #return the last href to workaround https://pulp.plan.io/issues/9098
end

.version_href(tasks) ⇒ Object



55
56
57
58
59
60
61
# File 'app/services/katello/pulp3/task.rb', line 55

def self.version_href(tasks)
  tasks = [tasks] unless tasks.is_a?(Array)
  version_hrefs = tasks.map { |task| task[:created_resources] }.flatten
  version_hrefs = version_hrefs.select { |href| ::Katello::Pulp3::Repository.version_href?(href) }
  Rails.logger.error("Got multiple version_hrefs for pulp task: #{tasks}") if version_hrefs.length > 2
  version_hrefs.last
end

Instance Method Details

#cancelObject



119
120
121
122
123
124
125
# File 'app/services/katello/pulp3/task.rb', line 119

def cancel
  core_api.cancel_task(task_data['pulp_href'])
  #the main task may have completed, so cancel spawned tasks too
  task_data['spawned_tasks']&.each do |spawned|
    core_api.cancel_task(spawned['pulp_href'])
  end
end

#core_apiObject



78
79
80
# File 'app/services/katello/pulp3/task.rb', line 78

def core_api
  ::Katello::Pulp3::Api::Core.new(@smart_proxy)
end

#correlation_idObject



94
95
96
# File 'app/services/katello/pulp3/task.rb', line 94

def correlation_id
  task_data['logging_cid']
end

#done?Boolean



86
87
88
# File 'app/services/katello/pulp3/task.rb', line 86

def done?
  task_data[:finished_at] || FINISHED_STATES.include?(task_data[:state])
end

#errorObject



107
108
109
110
111
112
113
114
115
116
117
# File 'app/services/katello/pulp3/task.rb', line 107

def error
  if task_data[:state] == CANCELED
    _("Task canceled")
  elsif task_data[:state] == FAILED
    if task_data[:error][:description].blank?
      _("Pulp task error")
    else
      task_data[:error][:description]
    end
  end
end

#pollObject



98
99
100
101
# File 'app/services/katello/pulp3/task.rb', line 98

def poll
  task_data(true)
  self
end

#progress_reportsObject



90
91
92
# File 'app/services/katello/pulp3/task.rb', line 90

def progress_reports
  task_data['progress_reports']
end

#started?Boolean



103
104
105
# File 'app/services/katello/pulp3/task.rb', line 103

def started?
  task_data[:started_at]
end

#task_data(force_refresh = false) ⇒ Object



71
72
73
74
# File 'app/services/katello/pulp3/task.rb', line 71

def task_data(force_refresh = false)
  @pulp_data = nil if force_refresh
  @pulp_data ||= tasks_api.read(@href).as_json.with_indifferent_access
end

#task_group_hrefObject



82
83
84
# File 'app/services/katello/pulp3/task.rb', line 82

def task_group_href
  task_data[:task_group] || task_data[:created_resources].find { |href| href.starts_with?("/pulp/api/v3/task-groups/") }
end