From 5f8e20aa1de551b7ff01de731eb2f4df5d029264 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Thu, 3 Mar 2016 08:26:45 +0800 Subject: [PATCH] FIX: Topic view not working when Redis is readonly. --- app/services/random_topic_selector.rb | 7 ++++++- spec/services/random_topic_selector_spec.rb | 7 +++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/app/services/random_topic_selector.rb b/app/services/random_topic_selector.rb index 7486311cb6e..705bcd51d13 100644 --- a/app/services/random_topic_selector.rb +++ b/app/services/random_topic_selector.rb @@ -47,7 +47,12 @@ class RandomTopicSelector $redis.ltrim(key, count, -1) end - results = results[0] + if !results.is_a?(Array) # Redis is in readonly mode + results = $redis.lrange(key, 0, count-1) + else + results = results[0] + end + results.map!(&:to_i) left = count - results.length diff --git a/spec/services/random_topic_selector_spec.rb b/spec/services/random_topic_selector_spec.rb index dd2d1cccb39..d57f5519d2b 100644 --- a/spec/services/random_topic_selector_spec.rb +++ b/spec/services/random_topic_selector_spec.rb @@ -11,8 +11,15 @@ describe RandomTopicSelector do $redis.rpush key, t end + expect(RandomTopicSelector.next(0)).to eq([]) expect(RandomTopicSelector.next(2)).to eq([0,1]) + + $redis.expects(:multi).returns(Discourse.received_readonly!) expect(RandomTopicSelector.next(2)).to eq([2,3]) + $redis.unstub(:multi) + + expect(RandomTopicSelector.next(2)).to eq([2,3]) + expect(RandomTopicSelector.next(2)).to eq([]) end it 'can correctly backfill' do