Commit 629bf199 authored by Mateus Rambo Strey's avatar Mateus Rambo Strey

add worker and service for score generator

parent d85e5fb4
......@@ -27,3 +27,6 @@ puma.sh
start_puma.sh
stop_puma.sh
portalmec.service
portalmec-sidekiq.service
sidekiq.sh
autocomplete-server.service
class ScoreCalculatorWorker
include Sidekiq::Worker
sidekiq_options queue: :score
include RepositoriesProxy
def perform(rid)
object = learning_object_repository.find(rid)
score = 0
# 10 points if it has thumbnail
score += 10 unless object.thumbnail.empty?
# 1 point per like
likes = learning_object_repository.count_likes(object)
score += (likes * 1) unless likes < 1
# 1 point per view
views = learning_object_repository.count_views(object)
score += (likes * 1) unless views < 1
learning_object_repository.update_property(object, 'score', score)
end
end
namespace :score do
desc "Generate Score"
task :learning_object => :environment do
include RepositoriesProxy
# Quantity of items fetched on each iteration
limit = 500
# Start point from where items will be fetched
offset = 0
loop do
begin
# Get items from dspace (from offset to offset+limit)
items = learning_object_repository.all_from_offset_to_limit(offset,limit)
rescue
# Sleeps for a while to wait database's recovery
sleep(30.seconds)
# Goes to next iteration to retry
next
else
# Terminate loop if there are no more items to import
break if items.empty?
items.each do |item|
ScoreCalculatorWorker.perform_async(item.id)
end
# Increment offset, to get new items on next iteration
offset += limit
end
end
end
end
[Unit]
Description=Control sidekiq for portalmec
[Service]
ExecStart=/portalmec/sidekiq.sh start
ExecStop=/portalmec/sidekiq.sh stop
[Install]
WantedBy=multi-user.target
#!/bin/bash
source /home/.rvm/scripts/rvm
export PUMA_APP_DIR=/portalmec
option="${1}"
case ${option} in
start)
cd $PUMA_APP_DIR
bundle exec sidekiq
;;
stop)
cd $PUMA_APP_DIR
bundle exec sidekiqctl shared/pids/sidekiq.pid
;;
*)
echo "`basename ${0}`:usage: [start] | [stop]"
exit 1 # Command to come out of the program with status 1
;;
esac
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment