Class: Katello::Job

Inherits:
Model
  • Object
show all
Includes:
Glue
Defined in:
app/models/katello/job.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Glue

included, logger

Methods inherited from Model

#destroy!

Class Method Details

.refresh_for_owner(owner) ⇒ Object



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

def refresh_for_owner(owner)
  # retrieve any 'in progress' tasks associated with the owner (e.g. system group)
  task_status_table = Katello::TaskStatus.table_name
  job_table = Katello::Job.table_name
  job_task_table = Katello::JobTask.table_name

  tasks = TaskStatus.where("#{task_status_table}.state" => [:waiting, :running]).where(
      "#{job_table}.job_owner_id" => owner.id, "#{job_table}.job_owner_type" => owner.class.name).joins(
      "INNER JOIN #{job_task_table} ON #{job_task_table}.task_status_id = #{task_status_table}.id").joins(
      "INNER JOIN #{job_table} ON #{job_table}.id = #{job_task_table}.job_id")

  ids = tasks.select("#{task_status_table}.id").collect { |row| row[:id] }

  # refresh the tasks via pulp
  refresh_tasks(ids) unless ids.empty?

  # retrieve the jobs for the current owner (e.g. system group)
  Job.where(:job_owner_id => owner.id, :job_owner_type => owner.class.name)
end

.refresh_tasks(ids) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'app/models/katello/job.rb', line 15

def refresh_tasks(ids)
  unless ids.nil? || ids.empty?
    uuids = TaskStatus.where(:id => ids).pluck(:uuid)
    uuids.each do |uuid|
      pulp_task = Katello.pulp_server.resources.task.poll(uuid)
      PulpTaskStatus.dump_state(pulp_task, TaskStatus.find_by(:uuid => pulp_task[:task_id]))
    end
  end
end

Instance Method Details

#as_json(_options = {}) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/models/katello/job.rb', line 77

def as_json(_options = {})
  first_task = self.task_statuses.first
  #check for first task
  if first_task.nil?
    return {:id => self.id, :state => 'error', :status_message => 'No tasks in job.'}
  else
    #since this is a collection of tasks, where
    # the type and parameters will all be the same
    #  lets not return them in each task object, but instead
    #  put them in the job
    tasks = self.task_statuses.collect do |t|
      {
        :id => t.id,
        :result => t.result,
        :progress => t.progress,
        :state => t.state,
        :uuid => t.uuid,
        :start_time => t.start_time,
        :finish_time => t.finish_time
      }
    end
    return {
      :id => self.id,
      :pulp_id => self.pulp_id,
      :created_at => first_task.created_at,
      :task_type => first_task.task_type,
      :parameters => first_task.parameters,
      :tasks => tasks,

      :state => self.state,
      :finish_time => self.finish_time,
      :status_message => self.status_message
    }
  end
end

#create_tasks(organization, pulp_tasks, task_type, parameters) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/models/katello/job.rb', line 46

def create_tasks(organization, pulp_tasks, task_type, parameters)
  # create an array of task status objects

  tasks = []
  pulp_tasks.each do |task|
    # if the task was returned with a UUID belonging to a system, associate that system with the task
    unless task[:call_request_tags].blank?
      uuid = task[:call_request_tags].first.split('pulp:consumer:').last
      system = System.where(:uuid => uuid).first
    end

    task_status = PulpTaskStatus.new(
        :organization => organization,
        :task_owner => system,
        :task_type => task_type,
        :parameters => parameters
    )
    task_status.merge_pulp_task!(task)
    task_status.save!
    tasks.push(task_status)
  end

  # add the task statuses to the job
  unless tasks.empty?
    self.task_statuses = tasks
    self.save!
  end

  tasks
end

#finish_timeObject



130
131
132
# File 'app/models/katello/job.rb', line 130

def finish_time
  self.task_statuses.order('finish_time DESC').last.finish_time
end

#messagesObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/models/katello/job.rb', line 113

def messages
  # Return a hash of the messages that may be rendered to the user to represent the status of the job.
  first_task = self.task_statuses.first
  #check for first task
  if first_task.nil?
    return {:id => self.id}
  else
    return {
      :task_type => TaskStatus::TYPES[first_task.task_type][:english_name],
      :summary_message => summary_message(first_task),
      :requested_action_message => requested_action_message(first_task),
      :pending_action_message => (pending_action_message(first_task) if state == :running),
      :parameters_message => parameters_message(first_task)
    }
  end
end

#pending?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'app/models/katello/job.rb', line 134

def pending?
  self.state == :running || self.state == :waiting
end

#stateObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'app/models/katello/job.rb', line 138

def state
  # determine the overall status of the job by evaluating the status of it's tasks
  # - running (aka installing), if 1 or more tasks are waiting or running
  # - error, if waiting+running is 0 and at least 1 error has occurred
  # - finished, otherwise...
  running = 0
  error = 0
  self.task_statuses.each do |task|
    if task.state == TaskStatus::Status::WAITING.to_s || task.state == TaskStatus::Status::RUNNING.to_s
      running += 1
    elsif task.state == TaskStatus::Status::ERROR.to_s
      error += 1
    end
  end

  state = :finished  # assume the job is finished, by default
  if (running > 0)
    state = :running
  elsif (error > 0)
    state = :error
  end
  state
end

#status_messageObject



162
163
164
165
166
# File 'app/models/katello/job.rb', line 162

def status_message
  first_task = self.task_statuses.first
  details = TaskStatus::TYPES[first_task.task_type]
  details[:event_messages][self.state].first
end