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

Returns a new instance of Task.



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

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



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

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.debug("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



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

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.debug("Got multiple version_hrefs for pulp task: #{tasks}") if version_hrefs.length > 2
  version_hrefs.last
end

Instance Method Details

#cancelObject



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

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



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

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

#correlation_idObject



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

def correlation_id
  task_data['logging_cid']
end

#done?Boolean

Returns:

  • (Boolean)


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

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

#errorObject



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

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

#pollObject



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

def poll
  task_data(true)
  self
end

#progress_reportsObject



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

def progress_reports
  task_data['progress_reports']
end

#started?Boolean

Returns:

  • (Boolean)


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

def started?
  task_data[:started_at]
end

#task_data(force_refresh = false) ⇒ Object



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

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



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

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