Skip to content

Commit 43323c5

Browse files
committed
Don't hammer the database on score calculation
1 parent 21cbf6e commit 43323c5

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

app/models/article.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def cacluate_content_quality_score
9191
factor * (weight = 20)
9292
end
9393

94-
def cacluate_score
94+
def calculate_score
9595
return 0 if bad_content?
9696
half_life = 2.days.to_i
9797
# gravity = 1.8 #used to look at upvote_velocity(1.week.ago)
@@ -124,7 +124,7 @@ def public_id_blank?
124124
end
125125

126126
def cache_calculated_score!
127-
self.score = cacluate_score
127+
self.score = calculate_score
128128
end
129129

130130
def display_tags

lib/tasks/cache.rake

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ namespace :cache do
77
namespace :score do
88
task :recalculate => :environment do
99
ActiveRecord::Base.logger.level = Logger::INFO #hide sql output
10-
Protip.order(created_at: :asc).find_each do |p|
11-
score = p.cacluate_score
10+
Protip.order(created_at: :asc).find_each(batch_size: 100) do |p|
11+
score = p.calculate_score
1212
p.update_column(:score, score)
13+
sleep 0.1
1314
end
1415
end
1516
end

0 commit comments

Comments
 (0)