Commit e42bf0b626c57ea194be93678025c6fa11674ecc

Authored by Dhruv Kapadia
1 parent 47320adc

Setting up redis based prompt_queue to speed up catchup algorithm

app/models/choice.rb
@@ -108,7 +108,7 @@ class Choice < ActiveRecord::Base @@ -108,7 +108,7 @@ class Choice < ActiveRecord::Base
108 108
109 def generate_prompts 109 def generate_prompts
110 #once a choice is added, we need to generate the new prompts (possible combinations of choices) 110 #once a choice is added, we need to generate the new prompts (possible combinations of choices)
111 - #do this in a new process (via delayed jobs) 111 + #do this in a new process (via delayed jobs)? Maybe just for uploaded ideas
112 previous_choices = (self.question.choices - [self]) 112 previous_choices = (self.question.choices - [self])
113 return if previous_choices.empty? 113 return if previous_choices.empty?
114 inserts = [] 114 inserts = []
@@ -127,7 +127,6 @@ class Choice < ActiveRecord::Base @@ -127,7 +127,6 @@ class Choice < ActiveRecord::Base
127 127
128 Question.update_counters(self.question_id, :prompts_count => 2*previous_choices.size) 128 Question.update_counters(self.question_id, :prompts_count => 2*previous_choices.size)
129 129
130 - logger.info("The sql is:::: #{sql}")  
131 130
132 ActiveRecord::Base.connection.execute(sql) 131 ActiveRecord::Base.connection.execute(sql)
133 132
app/models/question.rb
@@ -298,6 +298,27 @@ class Question < ActiveRecord::Base @@ -298,6 +298,27 @@ class Question < ActiveRecord::Base
298 end 298 end
299 end 299 end
300 300
  301 + def pq_key
  302 + @pq_key ||= "#{self.id}_prompt_queue"
  303 + end
  304 +
  305 + def clear_prompt_queue
  306 + $redis.del(self.pq_key)
  307 + end
  308 +
  309 + def add_prompt_to_queue
  310 + prompt = self.catchup_choose_prompt
  311 + $redis.rpush(self.pq_key, prompt.id)
  312 + prompt
  313 + end
  314 +
  315 + def pop_prompt_queue
  316 + prompt_id = $redis.lpop(self.pq_key)
  317 + prompt = prompt_id.nil? ? nil : Prompt.find(prompt_id.to_i)
  318 + end
  319 +
  320 +
  321 +
301 322
302 323
303 end 324 end
config/initializers/redis.rb 0 → 100644
@@ -0,0 +1,2 @@ @@ -0,0 +1,2 @@
  1 +require 'redis'
  2 +$redis = Redis.new
spec/models/question_spec.rb
@@ -84,6 +84,49 @@ describe Question do @@ -84,6 +84,49 @@ describe Question do
84 (sum - 1.0).abs.should < 0.000001 84 (sum - 1.0).abs.should < 0.000001
85 end 85 end
86 86
  87 + it "should allow the prompt queue to be cleared" do
  88 + @catchup_q.add_prompt_to_queue
  89 + @catchup_q.clear_prompt_queue
  90 +
  91 + @catchup_q.pop_prompt_queue.should == nil
  92 + end
  93 + it "should allow a prompt to be added to the prompt queue" do
  94 + @catchup_q.clear_prompt_queue
  95 + @catchup_q.pop_prompt_queue.should == nil
  96 +
  97 + @catchup_q.add_prompt_to_queue
  98 +
  99 + prompt = @catchup_q.pop_prompt_queue
  100 +
  101 + prompt.should_not == nil
  102 + prompt.active?.should == true
  103 + end
  104 + it "should return prompts from the queue in FIFO order" do
  105 + @catchup_q.clear_prompt_queue
  106 + @catchup_q.pop_prompt_queue.should == nil
  107 +
  108 + prompt1 = @catchup_q.add_prompt_to_queue
  109 + prompt2 = @catchup_q.add_prompt_to_queue
  110 + prompt3 = @catchup_q.add_prompt_to_queue
  111 +
  112 + prompt_1 = @catchup_q.pop_prompt_queue
  113 + prompt_2 = @catchup_q.pop_prompt_queue
  114 + prompt_3 = @catchup_q.pop_prompt_queue
  115 +
  116 +
  117 + prompt_1.should == prompt1
  118 + prompt_2.should == prompt2
  119 + prompt_3.should == prompt3
  120 +
  121 + # there is a small probability that the catchup algorithm
  122 + # choose two prompts that are indeed equal
  123 + prompt_1.should_not == prompt_2
  124 + prompt_1.should_not == prompt_3
  125 + prompt_2.should_not == prompt_3
  126 +
  127 +
  128 + @catchup_q.pop_prompt_queue.should == nil
  129 + end
87 130
88 131
89 end 132 end