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
task create_learning_object_relations: :environment do
desc "Create LearningObject relations based on its metadata"
lo_repo = Portalmec::Application.repository.for(:learning_object)
subject_repo = Portalmec::Application.repository.for(:subject)
attr_repo = Portalmec::Application.repository.for(:attribute)
include RepositoriesProxy
# Quantity of LearningObjects fetched on each iteration
limit = 1000
......@@ -21,11 +19,11 @@ namespace :orientdb do
offset = 0
loop do
p " --> importing LearningObjects from #{offset} to #{offset+limit}"
puts " --> importing LearningObjects from #{offset} to #{offset+limit}"
begin
# 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
# Sleeps for a while to wait database's recovery
sleep(30.seconds)
......@@ -40,30 +38,19 @@ namespace :orientdb do
subjects = metadata["dc.subject.category"]
subjects ||= []
metadata.delete("dc.subject.category")
lo.subjects=[]
lo.attributes=[]
subjects_array=[]
attributes_array=[]
subjects.each do |subject_name|
subject = subject_repo.find_by_name(subject_name)
if subject.nil?
subject = Subject.new(:name => subject_name)
subject_repo.create(subject)
end
lo.subjects << subject
subjects_array << {:name => subject_name}
end
metadata.each do |key, value|
value.each do |v|
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
attributes_array << {:key => key, :value => value}
end
lo_repo.create_relations(lo)
CreateRelationsWorker.perform_async(lo.id, subjects_array, attributes_array)
end
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