From 0a38626f0a32cc17f9c9b6f1af36632d66196653 Mon Sep 17 00:00:00 2001 From: Luke Baker Date: Tue, 26 Apr 2011 14:52:31 -0400 Subject: [PATCH] catchup fix --- app/models/choice.rb | 9 +++++++++ app/models/question.rb | 19 ++++++++++++++++--- spec/models/choice_spec.rb | 5 +++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/app/models/choice.rb b/app/models/choice.rb index bbb40bd..82df0b1 100644 --- a/app/models/choice.rb +++ b/app/models/choice.rb @@ -16,6 +16,7 @@ class Choice < ActiveRecord::Base named_scope :inactive, :conditions => { :active => false} after_save :update_questions_counter + after_save :update_prompt_queue attr_protected :prompts_count, :wins, :losses, :score, :prompts_on_the_right_count, :prompts_on_the_left_count attr_readonly :question_id @@ -23,6 +24,14 @@ class Choice < ActiveRecord::Base def update_questions_counter self.question.update_attribute(:inactive_choices_count, self.question.choices.inactive.length) end + + # if changing a choice to active, we want to regenerate prompts + def update_prompt_queue + if self.changed.include?('active') && self.active? + self.question.mark_prompt_queue_for_refill + self.question.choose_prompt + end + end def before_create unless self.score diff --git a/app/models/question.rb b/app/models/question.rb index 96481ae..c3bdf8f 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -29,6 +29,12 @@ class Question < ActiveRecord::Base attr_readonly :site_id + # regenerate prompts if cache is less than this full + # ideally this prevents active marketplaces from + # regenerating prompts too frequently + @@percent_full = 0.9 + @@num_prompts = 1000 + named_scope :created_by, lambda { |id| {:conditions => { :local_identifier => id } } } @@ -474,13 +480,20 @@ class Question < ActiveRecord::Base $redis.del(self.pq_key) end + + # make prompt queue less than @@precent_full + def mark_prompt_queue_for_refill + # 2 because redis starts indexes at 0 + new_size = (@@num_prompts * @@percent_full - 2).floor + $redis.ltrim(self.pq_key, 0, new_size) + end + def add_prompt_to_queue - num_prompts = 1000 # if less than 90% full, regenerate prompts # we skip generating prompts if more than 90% full to # prevent one busy marketplace for ruling the queue - if $redis.llen(self.pq_key) < num_prompts * 0.9 - prompts = self.catchup_choose_prompt(num_prompts) + if $redis.llen(self.pq_key) < @@num_prompts * @@percent_full + prompts = self.catchup_choose_prompt(@@num_prompts) # clear list $redis.ltrim(self.pq_key, 0, 0) $redis.lpop(self.pq_key) diff --git a/spec/models/choice_spec.rb b/spec/models/choice_spec.rb index 41d9da9..3d4a6a9 100644 --- a/spec/models/choice_spec.rb +++ b/spec/models/choice_spec.rb @@ -86,6 +86,11 @@ describe Choice do choice1.should be_active end + it "should create a delayed job on activation" do + choice1 = Choice.create!(@valid_attributes.merge(:data => '1234')) + proc { choice1.deactivate! }.should change(Delayed::Job, :count).by(1) + end + it "should update a question's counter cache on deactivation" do prev_inactive = @question.inactive_choices_count choice1 = Choice.create!(@valid_attributes.merge(:data => '1234')) -- libgit2 0.21.2