From 05ae50f0c53fefe150d3f201414289a89e137e6f Mon Sep 17 00:00:00 2001 From: Dhruv Kapadia Date: Thu, 22 Apr 2010 12:41:42 -0400 Subject: [PATCH] Basic background prompt queue working. Tweaking necessary --- Rakefile | 5 +++++ app/controllers/prompts_controller.rb | 9 +++++++-- app/controllers/questions_controller.rb | 9 ++++++++- config/initializers/delayed_job.rb | 19 ++++++++++++++----- spec/controllers/questions_controller_spec.rb | 63 +++++++++++++++++++++++++++++++++++++++++++++------------------ 5 files changed, 79 insertions(+), 26 deletions(-) diff --git a/Rakefile b/Rakefile index e14dcfa..703ee6a 100644 --- a/Rakefile +++ b/Rakefile @@ -11,3 +11,8 @@ require 'tasks/rails' task :default => [:test, :features] +begin + require 'delayed/tasks' +rescue LoadError + STDERR.puts "Run `rake gems:install` to install delayed_job" +end diff --git a/app/controllers/prompts_controller.rb b/app/controllers/prompts_controller.rb index 76bfa87..b72c3a1 100644 --- a/app/controllers/prompts_controller.rb +++ b/app/controllers/prompts_controller.rb @@ -72,8 +72,13 @@ class PromptsController < InheritedResources::Base if successful #catchup_marketplace_ids = [120, 117, 1, 116] if @question.uses_catchup? - logger.info("Question #{@question.id} is using catchup algorithm!") - next_prompt = @question.catchup_choose_prompt + logger.info("Question #{@question.id} is using catchup algorithm!") + next_prompt = @question.pop_prompt_queue + if next_prompt.nil? + logger.info("Catchup prompt cache miss! Nothing in prompt_queue") + next_prompt = @question.catchup_choose_prompt + end + @question.send_later :add_prompt_to_queue else next_prompt = @question.picked_prompt end diff --git a/app/controllers/questions_controller.rb b/app/controllers/questions_controller.rb index 0badfb4..dfc3365 100644 --- a/app/controllers/questions_controller.rb +++ b/app/controllers/questions_controller.rb @@ -59,7 +59,14 @@ class QuestionsController < InheritedResources::Base unless params[:barebones] if params[:algorithm] && params[:algorithm] == "catchup" logger.info("Question #{@question.id} requested catchup algorithm!") - @p = @question.catchup_choose_prompt + + @p = @question.pop_prompt_queue + if @p.nil? + logger.info("Catchup prompt cache miss! Nothing in prompt_queue") + @p = @question.catchup_choose_prompt + end + @question.send_later :add_prompt_to_queue + else @p = @question.picked_prompt end diff --git a/config/initializers/delayed_job.rb b/config/initializers/delayed_job.rb index c87d3f3..1355b25 100644 --- a/config/initializers/delayed_job.rb +++ b/config/initializers/delayed_job.rb @@ -1,6 +1,15 @@ -# Delayed::Job.destroy_failed_jobs = false +Delayed::Worker.backend = :active_record +Delayed::Worker.destroy_failed_jobs = false +Delayed::Worker.sleep_delay = 5 +Delayed::Worker.max_attempts = 3 +Delayed::Worker.max_run_time = 10.minutes + +#class Delayed::Worker +# alias_method :original_handle_failed_job, :handle_failed_job # -# silence_warnings do -# Delayed::Job.const_set("MAX_ATTEMPTS", 3) -# Delayed::Job.const_set("MAX_RUN_TIME", 5.minutes) -# end +#protected +# def handle_failed_job(job, error) +# HoptoadNotifier.notify(error) +# original_handle_failed_job(job,error) +# end +#end diff --git a/spec/controllers/questions_controller_spec.rb b/spec/controllers/questions_controller_spec.rb index 102d598..f5d8a95 100644 --- a/spec/controllers/questions_controller_spec.rb +++ b/spec/controllers/questions_controller_spec.rb @@ -4,18 +4,26 @@ describe QuestionsController do # integrate_views # - # def sign_in_as(user) - # @controller.current_user = user - # return user - # end + def sign_in_as(user) + @controller.current_user = user + return user + end # - # before(:each) do - # sign_in_as(@user = Factory(:email_confirmed_user)) - # end - # - # def mock_question(stubs={}) - # @mock_question ||= mock_model(Question, stubs) - # end + before(:each) do + sign_in_as(@user = Factory(:email_confirmed_user)) + end + # + def mock_question(stubs={}) + @mock_question ||= mock_model(Question, stubs) + end + + def mock_prompt(stubs={}) + @mock_prompt ||= mock_model(Prompt, stubs) + end + + def mock_appearance(stubs={}) + @mock_appearance||= mock_model(Appearance, stubs) + end # # describe "GET index" do # it "assigns all questions as @questions" do @@ -25,13 +33,32 @@ describe QuestionsController do # end # end # - # describe "GET show" do - # it "assigns the requested question as @question" do - # Question.stub!(:find).with("37").and_return(mock_question) - # get :show, :id => "37" - # assigns[:question].should equal(mock_question) - # end - # end + describe "GET show normal" do + before(:each) do + Question.stub!(:find).with("37").and_return(mock_question) + mock_question.stub!(:picked_prompt).and_return(mock_prompt) + end + + it "assigns the requested question as @question" do + Question.stub!(:find).with("37").and_return(mock_question) + #TODO it shouldn't call this unless we are generating an appearance, right? + + get :show, :id => "37" + assigns[:question].should equal(mock_question) + assigns[:prompt].should equal(mock_prompt) + assigns[:a].should be_nil + end + + #TODO this is not a particularly intutive param to pass in order to create an appearance + it "creates an appearance when a visitor identifier is a param" do + @user.stub!(:record_appearance).with("somelongunique32charstring", mock_prompt).and_return(mock_appearance) + get :show, :id => "37", :visitor_identifier => "somelongunique32charstring" + assigns[:question].should equal(mock_question) + assigns[:prompt].should equal(mock_prompt) + assigns[:a].should equal(mock_appearance) + + end + end # # describe "GET new" do # it "assigns a new question as @question" do -- libgit2 0.21.2