Class: Katello::TaskStatus

Inherits:
Model
  • Object
show all
Includes:
Util::TaskStatus
Defined in:
app/models/katello/task_status.rb

Direct Known Subclasses

PulpTaskStatus

Defined Under Namespace

Classes: Status

Constant Summary

Constants included from Util::TaskStatus

Util::TaskStatus::TYPES

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#destroy!

Constructor Details

#initialize(attrs = nil, _options = {}) ⇒ TaskStatus

Returns a new instance of TaskStatus.



65
66
67
68
69
70
71
72
73
74
# File 'app/models/katello/task_status.rb', line 65

def initialize(attrs = nil, _options = {})
  unless attrs.nil?
    # only keep keys for which we have db columns
    attrs = attrs.reject do |k, _v|
      !self.class.column_defaults.keys.member?(k.to_s) && (!respond_to?(:"#{k.to_s}=") rescue true)
    end
  end

  super(attrs)
end

Class Method Details

.make(system, pulp_task, task_type, parameters) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
329
# File 'app/models/katello/task_status.rb', line 318

def self.make(system, pulp_task, task_type, parameters)
  task_status = PulpTaskStatus.new(
     :organization => system.organization,
     :task_owner => system,
     :task_type => task_type,
     :parameters => parameters,
     :systems => [system]
  )
  task_status.merge_pulp_task!(pulp_task)
  task_status.save!
  task_status
end

.refresh(ids) ⇒ Object



308
309
310
311
312
313
314
315
316
# File 'app/models/katello/task_status.rb', line 308

def self.refresh(ids)
  unless ids.blank?
    uuids = TaskStatus.where(:id => ids).pluck(:uuid)
    ret = Katello.pulp_server.resources.task.poll_all(uuids)
    ret.each do |pulp_task|
      PulpTaskStatus.dump_state(pulp_task, TaskStatus.find_by(:uuid => pulp_task[:task_id]))
    end
  end
end

Instance Method Details

#as_json(_options = {}) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
# File 'app/models/katello/task_status.rb', line 119

def as_json(_options = {})
  json = super :methods => :pending?

  if ('Katello::System' == task_owner_type)
    methods = [:description, :result_description, :overall_status]
    json.merge!(super(:only => methods, :methods => methods))
    json[:system_name] = task_owner.name
  end

  json
end

#canceled?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'app/models/katello/task_status.rb', line 99

def canceled?
  self.state == TaskStatus::Status::CANCELED.to_s
end

#descriptionObject



242
243
244
245
246
# File 'app/models/katello/task_status.rb', line 242

def description
  ret = ""
  ret << humanize_type << ": "
  ret << humanize_parameters
end

#error?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'app/models/katello/task_status.rb', line 103

def error?
  (self.state == TaskStatus::Status::ERROR.to_s)
end

#finished?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'app/models/katello/task_status.rb', line 95

def finished?
  ((self.state != TaskStatus::Status::WAITING.to_s) && (self.state != TaskStatus::Status::RUNNING.to_s))
end

#generate_descriptionObject



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'app/models/katello/task_status.rb', line 265

def generate_description
  ret = []
  task_type = self.task_type.to_s

  if task_type =~ /^package_group/
    action = task_type.include?("remove") ? :removed : :installed
    ret = packages_change_description(result[:details][:package_group], action)
  elsif task_type == "package_install" || task_type == "errata_install"
    ret = packages_change_description(result[:details][:rpm], :installed)
  elsif task_type == "package_update"
    ret = packages_change_description(result[:details][:rpm], :updated)
  elsif task_type == "package_remove"
    ret = packages_change_description(result[:details][:rpm], :removed)
  end
  ret
end

#human_readable_messageObject



131
132
133
134
135
136
137
138
139
# File 'app/models/katello/task_status.rb', line 131

def human_readable_message
  task_template = TaskStatus::TYPES[self.task_type]
  return '' if task_template.nil?
  if task_template[:user_message]
    task_template[:user_message] % self.user.
  else
    task_template[:english_name]
  end
end

#humanize_parametersObject



228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'app/models/katello/task_status.rb', line 228

def humanize_parameters
  humanized_parameters = []
  if packages = self.parameters[:packages]
    humanized_parameters.concat(packages)
  end
  if groups = self.parameters[:groups]
    humanized_parameters.concat(groups.map { |g| g =~ /^@/ ? g : "@#{g}" })
  end
  if errata = self.parameters[:errata_ids]
    humanized_parameters.concat(errata)
  end
  humanized_parameters.join(", ")
end

#humanize_typeObject



224
225
226
# File 'app/models/katello/task_status.rb', line 224

def humanize_type
  TaskStatus::TYPES[self.task_type][:name]
end

#merge_pulp_task!(pulp_task) ⇒ Object



111
112
113
# File 'app/models/katello/task_status.rb', line 111

def merge_pulp_task!(pulp_task)
  PulpTaskStatus.dump_state(pulp_task, self)
end

#messageObject

TODO: break up method rubocop:disable MethodLength



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'app/models/katello/task_status.rb', line 184

def message
  # Retrieve a text message that may be rendered for a task's status.  This is used in various places,
  # such as System Event history.
  details = TaskStatus::TYPES[self.task_type]
  return _("Non-system event") if details.nil?

  case details[:type]
  when :package
    p = self.parameters[:packages]
    first_package = p.first.is_a?(Hash) ? p.first[:name] : p.first
    unless p && p.length > 0
      if "package_update" == self.task_type
        case self.overall_status
        when "running"
          return "updating"
        when "waiting"
          return "updating"
        when "error"
          return _("all packages update failed")
        else
          return _("all packages update")
        end
      end
    end

    msg = details[:event_messages][self.overall_status]
    return n_(msg[1], msg[2], p.length) % { package: first_package, total: p.length - 1 }
  when :candlepin_event
    return self.result
  when :package_group
    p = self.parameters[:groups]
    msg = details[:event_messages][self.overall_status]
    return n_(msg[1], msg[2], p.length) % { group: p.first, total: p.length - 1 }
  when :errata
    p = self.parameters[:errata_ids]
    msg = details[:event_messages][self.overall_status]
    return n_(msg[1], msg[2], p.length) % { errata: p.first, total: p.length - 1 }
  end
end

#overall_statusObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/models/katello/task_status.rb', line 76

def overall_status
  # the overall status of tasks (e.g. associated with a system) are determined by a
  # combination of the task state and the status of the unit within the task.
  unit_status = true
  if (self.result.is_a? Hash) && (self.result.key? :details)
    if self.result[:details].key? :rpm
      unit_status = self.result[:details][:rpm][:succeeded]
    elsif self.result[:details].key? :package_group
      unit_status = self.result[:details][:package_group][:succeeded]
    end
  end

  (self.state.to_s == "error" || !unit_status) ? "error" : self.state
end

#pending?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'app/models/katello/task_status.rb', line 91

def pending?
  self.state.to_s == "waiting" || self.state.to_s == "running"
end

#pending_messageObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'app/models/katello/task_status.rb', line 147

def pending_message
  # Retrieve a text message that may be rendered for a 'pending' task's status.  This is used in various places,
  # such as System Event history.
  details = TaskStatus::TYPES[self.task_type]
  case details[:type]
  when :package
    p = self.parameters[:packages]
    unless p && p.length > 0
      if "package_update" == self.task_type
        return _("all packages")
      end
      return ""
    end
    if p.length == 1
      return p.first
    else
      return  _("%{package} (%{total} other packages)") % {:package => p.first, :total => p.length - 1}
    end
  when :package_group
    p = self.parameters[:groups]
    if p.length == 1
      return p.first
    else
      return  _("%{group} (%{total} other package groups)") % {:group => p.first, :total => p.length - 1}
    end
  when :errata
    p = self.parameters[:errata_ids]
    if p.length == 1
      return p.first
    else
      return  _("%{errata} (%{total} other errata)") % {:errata => p.first, :total => p.length - 1}
    end
  end
end

#refreshObject



107
108
109
# File 'app/models/katello/task_status.rb', line 107

def refresh
  self
end

#refresh_pulpObject



115
116
117
# File 'app/models/katello/task_status.rb', line 115

def refresh_pulp
  PulpTaskStatus.refresh(self)
end

#result_descriptionObject



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'app/models/katello/task_status.rb', line 248

def result_description
  case self.state.to_s
  when "finished"
    # tasks initiated by pulp to the system can have state=finished
    # when the request is fully successful (e.g. all packages installed)
    # as well as if the task is not fully successful (e.g. attempt to
    # install a pkg that does not exist)
    generate_description
  when "error"
    # tasks initiated by pulp to the system will only have state=error
    # if an exception is thrown from the system/agent during remote
    # method invocation
    rmi_error_description
  else ""
  end
end

#rmi_error_descriptionObject



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'app/models/katello/task_status.rb', line 282

def rmi_error_description
  errors, stacktrace = self.result[:errors]
  return "" unless errors
  # Handle not very friendly Pulp message
  if errors =~ /^\(.*\)$/
    if stacktrace.class == Array
      stacktrace.last.split(":").first
    else
      stacktrace.split("(").first
    end
  elsif errors =~ /^\[.*,.*\]$/m
    result = errors.split(",").map do |error|
      error.gsub(/^\W+|\W+$/, "")
    end
    result.join("\n")
  else
    errors
  end
rescue
  if self.result.is_a? Hash
    self.result[:errors].join(' ').to_s
  else
    self.result
  end
end

#system_filter_clauseObject

used by search to filter tasks by systems :)



142
143
144
145
# File 'app/models/katello/task_status.rb', line 142

def system_filter_clause
  system_id = task_owner_id if (task_owner_type == 'Katello::System')
  {:system_id => system_id}
end