add mutex to prevent redundant related tag updates

This commit is contained in:
r888888888
2017-11-15 11:01:39 -08:00
parent 18c8892a74
commit 1597d77d37

View File

@@ -739,6 +739,7 @@ class Tag < ApplicationRecord
module RelationMethods module RelationMethods
def update_related def update_related
return unless should_update_related? return unless should_update_related?
CurrentUser.scoped(User.first, "127.0.0.1") do CurrentUser.scoped(User.first, "127.0.0.1") do
self.related_tags = RelatedTagCalculator.calculate_from_sample_to_array(name).join(" ") self.related_tags = RelatedTagCalculator.calculate_from_sample_to_array(name).join(" ")
end end
@@ -748,20 +749,21 @@ class Tag < ApplicationRecord
end end
def update_related_if_outdated def update_related_if_outdated
if should_update_related? key = Cache.hash(name)
if Cache.get("urt:#{key}").nil? && should_update_related?
if post_count < COSINE_SIMILARITY_RELATED_TAG_THRESHOLD if post_count < COSINE_SIMILARITY_RELATED_TAG_THRESHOLD
delay(:queue => "default").update_related delay(:queue => "default").update_related
elsif post_count >= COSINE_SIMILARITY_RELATED_TAG_THRESHOLD elsif post_count >= COSINE_SIMILARITY_RELATED_TAG_THRESHOLD
key = Cache.hash(name)
cache_check = Cache.get("urt:#{key}") cache_check = Cache.get("urt:#{key}")
if cache_check if cache_check
sqs = SqsService.new(Danbooru.config.aws_sqs_reltagcalc_url) sqs = SqsService.new(Danbooru.config.aws_sqs_reltagcalc_url)
sqs.send_message("calculate #{name}") sqs.send_message("calculate #{name}")
else
Cache.put("urt:#{key}", true, 600)
end end
end end
Cache.put("urt:#{key}", true, 600) # mutex to prevent redundant updates
end end
end end