Skip to content

Commit

Permalink
System task checking caused duplicate task
Browse files Browse the repository at this point in the history
Previously whenever you called system.tasks all tasks
from candlepin were duplicated within the db every time.
It was not checking properly that the tasks already existed.
Also the logic was confusing.  This should clear it up a good bit
  • Loading branch information
jlsherrill committed Aug 20, 2013
1 parent 1cb2e8b commit 64afe91
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
14 changes: 14 additions & 0 deletions app/models/glue/candlepin/consumer.rb
Expand Up @@ -540,6 +540,20 @@ def product_compliance_color product_id
return 'yellow' if self.compliance['partiallyCompliantProducts'].include? product_id
return 'red'
end

def import_candlepin_tasks
self.events.each do |event|
event_status = {:task_id => event[:id],
:state => event[:type],
:start_time => event[:timestamp],
:finish_time => event[:timestamp],
:progress => "100",
:result => event[:messageText]}
unless self.task_statuses.where('task_statuses.uuid' => event_status[:task_id]).exists?
TaskStatus.make(self, event_status, :candlepin_event, :event => event)
end
end
end
end

module ClassMethods
Expand Down
10 changes: 9 additions & 1 deletion app/models/system.rb
Expand Up @@ -213,7 +213,9 @@ def init_default_custom_info
end

def tasks
TaskStatus.refresh_for_system(self)
refresh_running_tasks
import_candlepin_tasks
self.task_statuses
end

# A rollback occurred while attempting to create the system; therefore, perform necessary cleanup.
Expand All @@ -225,6 +227,12 @@ def rollback_on_create
end

private

def refresh_running_tasks
ids = self.task_statuses.where(:state => [:waiting, :running]).pluck(:id)
TaskStatus.refresh(ids)
end

def save_task_status pulp_task, task_type, parameters_type, parameters
TaskStatus.make(self, pulp_task, task_type, parameters_type => parameters)
end
Expand Down
24 changes: 2 additions & 22 deletions app/models/task_status.rb
Expand Up @@ -178,6 +178,7 @@ 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]
Expand Down Expand Up @@ -305,7 +306,7 @@ def self.refresh_for_distributor(distributor_id)
end

def self.refresh(ids)
unless ids.nil? || ids.empty?
unless ids.blank?
uuids = TaskStatus.where(:id=>ids).pluck(:uuid)
ret = Katello.pulp_server.resources.task.poll_all(uuids)
ret.each do |pulp_task|
Expand Down Expand Up @@ -375,25 +376,4 @@ def packages_change_description(data, action)
ret
end

def self.refresh_for_candlepin_consumer(owner_type, consumer_id, consumer)
query = TaskStatus.select(:id).where(:task_owner_type => owner_type).where(:task_owner_id => consumer_id)
ids = query.where(:state => [:waiting, :running]).collect {|row| row[:id]}
refresh(ids)
statuses = TaskStatus.where("task_statuses.id in (#{query.to_sql})")

# Since Candlepin events are not recorded as tasks, fetch them for this system or distributor
# and add them here. The alternative to this lazy loading of Candlepin tasks would be to have
# each API call that Katello passes through to Candlepin record the task explicitly.
consumer.events.each {|event|
event_status = {:task_id => event[:id], :state => event[:type],
:start_time => event[:timestamp], :finish_time => event[:timestamp],
:progress => "100", :result => event[:messageText]}
# Find or create task
task = statuses.where("#{TaskStatus.table_name}.uuid" => event_status[:id]).first
task ||= TaskStatus.make(consumer, event_status, :candlepin_event, :event => event)
}

statuses = TaskStatus.where("task_statuses.id in (#{query.to_sql})")
end

end

0 comments on commit 64afe91

Please sign in to comment.