diff --git a/app/models/tag.rb b/app/models/tag.rb index 519a36de453..c3af4180e39 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -1133,6 +1133,9 @@ def after_update end end + # Ensure tags with count cache can be reindexed. + Rails.cache.delete(taggings_count_cache_key) if tag.saved_change_to_taggings_count_cache? + # Reindex immediately to update the unwrangled bin. if tag.saved_change_to_unwrangleable? tag.reindex_document diff --git a/spec/models/tag_spec.rb b/spec/models/tag_spec.rb index 16daf57b9bb..0cb0ca34c0f 100644 --- a/spec/models/tag_spec.rb +++ b/spec/models/tag_spec.rb @@ -82,7 +82,9 @@ let(:tag) { create(:fandom) } let!(:work) { create(:work, fandom_string: tag.name) } - before do + before { run_update_tag_count_job } + + def run_update_tag_count_job RedisJobSpawner.perform_now("TagCountUpdateJob") tag.reload end @@ -105,8 +107,7 @@ def expect_tag_update_flag_in_redis_to_be(flag) tag.taggings_count = 2 expect_tag_update_flag_in_redis_to_be(true) - RedisJobSpawner.perform_now("TagCountUpdateJob") - tag.reload + run_update_tag_count_job # Actual number of taggings has not changed though count cache has. expect(tag.taggings_count_cache).to eq 2 @@ -117,8 +118,7 @@ def expect_tag_update_flag_in_redis_to_be(flag) create(:work, fandom_string: tag.name) expect_tag_update_flag_in_redis_to_be(true) - RedisJobSpawner.perform_now("TagCountUpdateJob") - tag.reload + run_update_tag_count_job expect(tag.taggings_count_cache).to eq 2 expect(tag.taggings_count).to eq 2 @@ -130,16 +130,15 @@ def expect_tag_update_flag_in_redis_to_be(flag) REDIS_GENERAL.set("tag_update_#{tag.id}_value", "") REDIS_GENERAL.sadd("tag_update", tag.id) - RedisJobSpawner.perform_now("TagCountUpdateJob") + run_update_tag_count_job - expect(tag.reload.taggings_count_cache).to eq 1 + expect(tag.taggings_count_cache).to eq 1 end it "triggers reindexing of tags which aren't used much" do create(:work, fandom_string: tag.name) - - expect { RedisJobSpawner.perform_now("TagCountUpdateJob") } - .to add_to_reindex_queue(tag.reload, :main) + expect { run_update_tag_count_job } + .to add_to_reindex_queue(tag, :main) end it "triggers reindexing of tags which are used significantly" do @@ -147,8 +146,12 @@ def expect_tag_update_flag_in_redis_to_be(flag) create(:work, fandom_string: tag.name) end - expect { RedisJobSpawner.perform_now("TagCountUpdateJob") } - .to add_to_reindex_queue(tag.reload, :main) + expect { run_update_tag_count_job } + .to add_to_reindex_queue(tag, :main) + expect_tag_update_flag_in_redis_to_be(false) + + create(:work, fandom_string: tag.name) + expect_tag_update_flag_in_redis_to_be(true) end end end