Class: Katello::TaskStatus

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

Defined Under Namespace

Classes: Status

Constant Summary

Constants included from Util::TaskStatus

Util::TaskStatus::TYPES

Instance Method Summary collapse

Methods inherited from Model

#destroy!

Constructor Details

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



54
55
56
57
58
59
60
61
62
63
# File 'app/models/katello/task_status.rb', line 54

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

Instance Method Details

#canceled?Boolean



88
89
90
# File 'app/models/katello/task_status.rb', line 88

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

#descriptionObject



205
206
207
208
209
# File 'app/models/katello/task_status.rb', line 205

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

#error?Boolean



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

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

#finished?Boolean



84
85
86
# File 'app/models/katello/task_status.rb', line 84

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

#generate_descriptionObject



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

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



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

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



191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'app/models/katello/task_status.rb', line 191

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



187
188
189
# File 'app/models/katello/task_status.rb', line 187

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

#messageObject

TODO: break up method rubocop:disable Metrics/MethodLength



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
181
182
183
184
185
# File 'app/models/katello/task_status.rb', line 147

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 self.task_type == "package_update"
        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



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

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



80
81
82
# File 'app/models/katello/task_status.rb', line 80

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

#pending_messageObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'app/models/katello/task_status.rb', line 110

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 self.task_type == "package_update"
        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



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

def refresh
  self
end

#result_descriptionObject



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'app/models/katello/task_status.rb', line 211

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



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'app/models/katello/task_status.rb', line 245

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