Skip to content
Snippets Groups Projects
Commit 1989f622 authored by Bruno Nocera Zanette's avatar Bruno Nocera Zanette
Browse files

Refactor CreateRelation Task to use Wokers

parent e30962e3
No related branches found
No related tags found
No related merge requests found
...@@ -11,9 +11,7 @@ namespace :orientdb do ...@@ -11,9 +11,7 @@ namespace :orientdb do
task create_learning_object_relations: :environment do task create_learning_object_relations: :environment do
desc "Create LearningObject relations based on its metadata" desc "Create LearningObject relations based on its metadata"
lo_repo = Portalmec::Application.repository.for(:learning_object) include RepositoriesProxy
subject_repo = Portalmec::Application.repository.for(:subject)
attr_repo = Portalmec::Application.repository.for(:attribute)
# Quantity of LearningObjects fetched on each iteration # Quantity of LearningObjects fetched on each iteration
limit = 1000 limit = 1000
...@@ -21,11 +19,11 @@ namespace :orientdb do ...@@ -21,11 +19,11 @@ namespace :orientdb do
offset = 0 offset = 0
loop do loop do
p " --> importing LearningObjects from #{offset} to #{offset+limit}" puts " --> importing LearningObjects from #{offset} to #{offset+limit}"
begin begin
# Get LearningObjects from OrientDB (from offset to offset+limit) # Get LearningObjects from OrientDB (from offset to offset+limit)
learning_objects = lo_repo.all_from_offset_to_limit(offset,limit) learning_objects = learning_object_repository.all_from_offset_to_limit(offset,limit)
rescue rescue
# Sleeps for a while to wait database's recovery # Sleeps for a while to wait database's recovery
sleep(30.seconds) sleep(30.seconds)
...@@ -40,30 +38,19 @@ namespace :orientdb do ...@@ -40,30 +38,19 @@ namespace :orientdb do
subjects = metadata["dc.subject.category"] subjects = metadata["dc.subject.category"]
subjects ||= [] subjects ||= []
metadata.delete("dc.subject.category") metadata.delete("dc.subject.category")
lo.subjects=[]
lo.attributes=[] subjects_array=[]
attributes_array=[]
subjects.each do |subject_name| subjects.each do |subject_name|
subject = subject_repo.find_by_name(subject_name) subjects_array << {:name => subject_name}
if subject.nil?
subject = Subject.new(:name => subject_name)
subject_repo.create(subject)
end
lo.subjects << subject
end end
metadata.each do |key, value| metadata.each do |key, value|
value.each do |v| attributes_array << {:key => key, :value => value}
attribute = attr_repo.find_by_key_and_value(key,v).first
if attribute.nil?
attribute = Attribute.new(:key => key, :value => v)
attr_repo.create(attribute)
end
lo.attributes << attribute
end
end end
lo_repo.create_relations(lo) CreateRelationsWorker.perform_async(lo.id, subjects_array, attributes_array)
end end
offset += limit offset += limit
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment