Commit 05ae50f0c53fefe150d3f201414289a89e137e6f

Authored by Dhruv Kapadia
1 parent a60f1d6b

Basic background prompt queue working. Tweaking necessary

Rakefile
... ... @@ -11,3 +11,8 @@ require 'tasks/rails'
11 11  
12 12 task :default => [:test, :features]
13 13  
  14 +begin
  15 + require 'delayed/tasks'
  16 +rescue LoadError
  17 + STDERR.puts "Run `rake gems:install` to install delayed_job"
  18 +end
... ...
app/controllers/prompts_controller.rb
... ... @@ -72,8 +72,13 @@ class PromptsController < InheritedResources::Base
72 72 if successful
73 73 #catchup_marketplace_ids = [120, 117, 1, 116]
74 74 if @question.uses_catchup?
75   - logger.info("Question #{@question.id} is using catchup algorithm!")
76   - next_prompt = @question.catchup_choose_prompt
  75 + logger.info("Question #{@question.id} is using catchup algorithm!")
  76 + next_prompt = @question.pop_prompt_queue
  77 + if next_prompt.nil?
  78 + logger.info("Catchup prompt cache miss! Nothing in prompt_queue")
  79 + next_prompt = @question.catchup_choose_prompt
  80 + end
  81 + @question.send_later :add_prompt_to_queue
77 82 else
78 83 next_prompt = @question.picked_prompt
79 84 end
... ...
app/controllers/questions_controller.rb
... ... @@ -59,7 +59,14 @@ class QuestionsController < InheritedResources::Base
59 59 unless params[:barebones]
60 60 if params[:algorithm] && params[:algorithm] == "catchup"
61 61 logger.info("Question #{@question.id} requested catchup algorithm!")
62   - @p = @question.catchup_choose_prompt
  62 +
  63 + @p = @question.pop_prompt_queue
  64 + if @p.nil?
  65 + logger.info("Catchup prompt cache miss! Nothing in prompt_queue")
  66 + @p = @question.catchup_choose_prompt
  67 + end
  68 + @question.send_later :add_prompt_to_queue
  69 +
63 70 else
64 71 @p = @question.picked_prompt
65 72 end
... ...
config/initializers/delayed_job.rb
1   -# Delayed::Job.destroy_failed_jobs = false
  1 +Delayed::Worker.backend = :active_record
  2 +Delayed::Worker.destroy_failed_jobs = false
  3 +Delayed::Worker.sleep_delay = 5
  4 +Delayed::Worker.max_attempts = 3
  5 +Delayed::Worker.max_run_time = 10.minutes
  6 +
  7 +#class Delayed::Worker
  8 +# alias_method :original_handle_failed_job, :handle_failed_job
2 9 #
3   -# silence_warnings do
4   -# Delayed::Job.const_set("MAX_ATTEMPTS", 3)
5   -# Delayed::Job.const_set("MAX_RUN_TIME", 5.minutes)
6   -# end
  10 +#protected
  11 +# def handle_failed_job(job, error)
  12 +# HoptoadNotifier.notify(error)
  13 +# original_handle_failed_job(job,error)
  14 +# end
  15 +#end
... ...
spec/controllers/questions_controller_spec.rb
... ... @@ -4,18 +4,26 @@ describe QuestionsController do
4 4  
5 5 # integrate_views
6 6 #
7   - # def sign_in_as(user)
8   - # @controller.current_user = user
9   - # return user
10   - # end
  7 + def sign_in_as(user)
  8 + @controller.current_user = user
  9 + return user
  10 + end
11 11 #
12   - # before(:each) do
13   - # sign_in_as(@user = Factory(:email_confirmed_user))
14   - # end
15   - #
16   - # def mock_question(stubs={})
17   - # @mock_question ||= mock_model(Question, stubs)
18   - # end
  12 + before(:each) do
  13 + sign_in_as(@user = Factory(:email_confirmed_user))
  14 + end
  15 + #
  16 + def mock_question(stubs={})
  17 + @mock_question ||= mock_model(Question, stubs)
  18 + end
  19 +
  20 + def mock_prompt(stubs={})
  21 + @mock_prompt ||= mock_model(Prompt, stubs)
  22 + end
  23 +
  24 + def mock_appearance(stubs={})
  25 + @mock_appearance||= mock_model(Appearance, stubs)
  26 + end
19 27 #
20 28 # describe "GET index" do
21 29 # it "assigns all questions as @questions" do
... ... @@ -25,13 +33,32 @@ describe QuestionsController do
25 33 # end
26 34 # end
27 35 #
28   - # describe "GET show" do
29   - # it "assigns the requested question as @question" do
30   - # Question.stub!(:find).with("37").and_return(mock_question)
31   - # get :show, :id => "37"
32   - # assigns[:question].should equal(mock_question)
33   - # end
34   - # end
  36 + describe "GET show normal" do
  37 + before(:each) do
  38 + Question.stub!(:find).with("37").and_return(mock_question)
  39 + mock_question.stub!(:picked_prompt).and_return(mock_prompt)
  40 + end
  41 +
  42 + it "assigns the requested question as @question" do
  43 + Question.stub!(:find).with("37").and_return(mock_question)
  44 + #TODO it shouldn't call this unless we are generating an appearance, right?
  45 +
  46 + get :show, :id => "37"
  47 + assigns[:question].should equal(mock_question)
  48 + assigns[:prompt].should equal(mock_prompt)
  49 + assigns[:a].should be_nil
  50 + end
  51 +
  52 + #TODO this is not a particularly intutive param to pass in order to create an appearance
  53 + it "creates an appearance when a visitor identifier is a param" do
  54 + @user.stub!(:record_appearance).with("somelongunique32charstring", mock_prompt).and_return(mock_appearance)
  55 + get :show, :id => "37", :visitor_identifier => "somelongunique32charstring"
  56 + assigns[:question].should equal(mock_question)
  57 + assigns[:prompt].should equal(mock_prompt)
  58 + assigns[:a].should equal(mock_appearance)
  59 +
  60 + end
  61 + end
35 62 #
36 63 # describe "GET new" do
37 64 # it "assigns a new question as @question" do
... ...