From 07a1211183894ebd62bf2c90779d30b773d35938 Mon Sep 17 00:00:00 2001 From: Daniel Waterworth Date: Thu, 12 Jan 2023 14:03:26 -0600 Subject: [PATCH] FIX: Prevent concurrent updates to top_topics (#19854) to prevent lock timeouts --- app/models/top_topic.rb | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/app/models/top_topic.rb b/app/models/top_topic.rb index 975adc8ec72..31fbf4efd5c 100644 --- a/app/models/top_topic.rb +++ b/app/models/top_topic.rb @@ -5,21 +5,25 @@ class TopTopic < ActiveRecord::Base # The top topics we want to refresh often def self.refresh_daily! - transaction do - remove_invisible_topics - add_new_visible_topics + DistributedMutex.synchronize("update_top_topics", validity: 5.minutes) do + transaction do + remove_invisible_topics + add_new_visible_topics - update_counts_and_compute_scores_for(:daily) + update_counts_and_compute_scores_for(:daily) + end end end # We don't have to refresh these as often def self.refresh_older! - older_periods = periods - %i[daily all] + DistributedMutex.synchronize("update_top_topics", validity: 5.minutes) do + older_periods = periods - %i[daily all] - transaction { older_periods.each { |period| update_counts_and_compute_scores_for(period) } } + transaction { older_periods.each { |period| update_counts_and_compute_scores_for(period) } } - compute_top_score_for(:all) + compute_top_score_for(:all) + end end def self.refresh!