Skip to content
This repository has been archived by the owner on Jan 8, 2022. It is now read-only.

Saving updated tasks to IC #193

Merged
merged 1 commit into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions db/migrate/20200428090420_add_request_id_to_task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddRequestIdToTask < ActiveRecord::Migration[5.2]
def change
add_column :tasks, :x_rh_insights_request, :string
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2020_04_14_123737) do
ActiveRecord::Schema.define(version: 2020_04_28_090420) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -1190,6 +1190,7 @@
t.string "target_source_ref"
t.string "target_type"
t.bigint "source_id"
t.string "x_rh_insights_request"
t.index ["source_id"], name: "index_tasks_on_source_id"
t.index ["target_type", "target_source_ref"], name: "index_tasks_on_target_type_and_target_source_ref"
t.index ["tenant_id"], name: "index_tasks_on_tenant_id"
Expand Down
2 changes: 1 addition & 1 deletion lib/topological_inventory/core/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module TopologicalInventory
module Core
VERSION = '1.1.1'
VERSION = '1.1.2'
end
end
19 changes: 13 additions & 6 deletions lib/topological_inventory/schema/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,35 +148,42 @@ def service_instance_tasks_custom_save(source, tasks_collection)

# Updating Tasks
# service_instance_tasks_update_by_raw_sql(source)
service_instance_tasks_update_by_activerecord(source, src_refs)
service_instance_tasks_update_by_activerecord(tasks_collection, source, src_refs)
end

def task_update_values(svc_instance_id, external_url, status, task_status, finished_timestamp)
def task_update_values(svc_instance_id, source_ref, external_url, status, task_status, finished_timestamp, source_id)
{
:state => finished_timestamp.blank? ? 'running' : 'completed',
:status => task_status,
:context => {
:service_instance => {
:id => svc_instance_id,
:job_status => status,
:source_id => source_id,
:source_ref => source_ref,
:url => external_url
}
}
}
end

# This method is updating one by one using ActiveRecord
def service_instance_tasks_update_by_activerecord(source, svc_instances_source_ref)
def service_instance_tasks_update_by_activerecord(tasks_collection, source, svc_instances_source_ref)
service_instances = ServiceInstance.where(:source_id => source.id, :source_ref => svc_instances_source_ref)
tasks_by_source_ref = Task.where(:state => 'running', :target_type => 'ServiceInstance', :source_id => source.id, :target_source_ref => service_instances.pluck(:source_ref)).index_by(&:target_source_ref)

service_instances.select(:id, :external_url, :source_ref, :extra).find_in_batches do |group|
ActiveRecord::Base.transaction do
group.each do |svc_instance|
next if tasks_by_source_ref[svc_instance.source_ref].nil?
next if (task = tasks_by_source_ref[svc_instance.source_ref]).nil?

values = task_update_values(svc_instance.id, svc_instance.external_url, svc_instance.extra['status'], svc_instance.extra['task_status'], svc_instance.extra['finished'])
tasks_by_source_ref[svc_instance.source_ref].update(values)
values = task_update_values(svc_instance.id, svc_instance.source_ref, svc_instance.external_url, svc_instance.extra['status'], svc_instance.extra['task_status'], svc_instance.extra['finished'], source.id)
# 1) Updating Task
task.update(values)

# 2) Saving to updated records (will be published in Kafka)
# - see topological_inventory-persister:Workflow.send_task_updates_to_queue!
tasks_collection.updated_records << values.merge(:id => task.id, :x_rh_insights_request => task.x_rh_insights_request)
end
end
end
Expand Down