Browse files

Beginning of Assertion processing via a Resque job queue.

  • Loading branch information...
1 parent 443d3d5 commit 5bbb33c198c06070efc07a3a8e0c7193d52fd0bf @HotFusionMan committed Apr 3, 2012
View
4 HotFusionMan-q_and_a_machine.gemspec
@@ -24,10 +24,12 @@ Gem::Specification.new do |s|
s.executables = s.files.map { |f| f =~ /^bin\/(.*)/ ? $1 : nil }.compact
s.test_files = Dir['spec/**/*']
- s.add_dependency 'rails', '~> 3.2.2'
+ s.add_dependency 'rails', '~> 3.2.3'
+ s.add_dependency 'resque'
s.add_development_dependency 'sqlite3'
s.add_development_dependency 'rspec-rails', '~> 2.5'
s.add_development_dependency 'capybara'
+ s.add_development_dependency 'mock_redis'
end
View
11 app/models/q_and_a_machine/assertion.rb
@@ -1,4 +1,15 @@
module QAndAMachine
class Assertion < ActiveRecord::Base
+ def initialize( _id = object_id )
+ @id = _id
+ end
+
+ # after_save :enqueue_if_new
+
+
+
+ def enqueue
+ ::Resque.enqueue( AssertionProcessor, @id )
+ end
end
end
View
5 app/models/q_and_a_machine/assertion_processor.rb
@@ -0,0 +1,5 @@
+module QAndAMachine
+ class AssertionProcessor
+ @queue = :assertions
+ end
+end
View
9 config/initializers/redis.rb
@@ -0,0 +1,9 @@
+require 'redis'
+require 'mock_redis'
+# require 'redis/objects'
+
+unless ENV['RAILS_ENV'] == 'test'
+ Redis.current = Redis.new( :host => '127.0.0.1', :port => 6379 )
+else
+ Redis.current = MockRedis.new
+end
View
3 config/initializers/resque.rb
@@ -0,0 +1,3 @@
+require 'resque'
+
+Resque.redis = Redis.current # Redis.current was set in config/initializers/redis.rb
View
9 lib/tasks/resque.rake
@@ -0,0 +1,9 @@
+require 'resque/tasks'
+
+namespace :resque do
+ task :setup => :environment do
+ require 'resque'
+
+ Resque.redis = Redis.current # Redis.current was set in config/initializers/redis.rb
+ end
+end
View
29 spec/models/assertion_spec.rb
@@ -0,0 +1,29 @@
+require 'spec_helper'
+# require 'mock_redis'
+require 'json'
+
+describe QAndAMachine::Assertion do
+ # before( :each ) do
+ # Redis.current = MockRedis.new
+ # end
+
+
+ it '#enqueue should queue a job' do
+ assertion_id = 42
+
+ assertion = QAndAMachine::Assertion.new( :id => assertion_id )
+ assertion.enqueue
+
+ # redis_keys = Redis.current.keys( '*' )
+ # redis_keys.each { |key|
+ # puts "#{key}\t#{Redis.current.type(key)}"
+ # }
+
+ queue_name = 'resque:queue:' + QAndAMachine::AssertionProcessor.instance_variable_get( :@queue ).to_s
+ Redis.current.llen( queue_name ).should == 1
+
+ job = JSON.parse( Redis.current.lpop( queue_name ) ) # Use lpop so we return Redis to its initial state.
+ job['args'].size.should == 1
+ job['args'].first['id'].should == assertion_id
+ end
+end
View
5 spec/models/question_spec.rb
@@ -1,5 +0,0 @@
-require 'spec_helper'
-
-describe Question do
- pending "add some examples to (or delete) #{__FILE__}"
-end

0 comments on commit 5bbb33c

Please sign in to comment.