posts read may enter concurrently, retry

This commit is contained in:
Sam Saffron 2015-06-01 11:55:07 +10:00
parent 9787cb07aa
commit 83559bbf45
1 changed files with 10 additions and 2 deletions

View File

@ -373,13 +373,21 @@ class User < ActiveRecord::Base
create_visit_record!(date) unless visit_record_for(date)
end
def update_posts_read!(num_posts, now=Time.zone.now)
def update_posts_read!(num_posts, now=Time.zone.now, _retry=false)
if user_visit = visit_record_for(now.to_date)
user_visit.posts_read += num_posts
user_visit.save
user_visit
else
create_visit_record!(now.to_date, num_posts)
begin
create_visit_record!(now.to_date, num_posts)
rescue ActiveRecord::RecordNotUnique
if !_retry
update_posts_read!(num_posts, now, _retry=true)
else
raise
end
end
end
end