diff --git a/lib/qu/backend/base.rb b/lib/qu/backend/base.rb index 2adeb30..16ec13d 100644 --- a/lib/qu/backend/base.rb +++ b/lib/qu/backend/base.rb @@ -3,6 +3,7 @@ module Qu module Backend class Base + attr_accessor :connection private diff --git a/lib/qu/backend/mongo.rb b/lib/qu/backend/mongo.rb index 67bfcd0..ae8def8 100644 --- a/lib/qu/backend/mongo.rb +++ b/lib/qu/backend/mongo.rb @@ -3,9 +3,22 @@ module Qu module Backend class Mongo < Base - def database - @database ||= ::Mongo::Connection.new.db('qu') + def connection + @connection ||= begin + uri = URI.parse(ENV['MONGOHQ_URL'].to_s) + database = uri.path.empty? ? 'qu' : uri.path[1..-1] + options = {} + if uri.password + options[:auths] = [{ + 'db_name' => database, + 'username' => uri.user, + 'password' => uri.password + }] + end + ::Mongo::Connection.new(uri.host, uri.port, options).db(database) + end end + alias_method :database, :connection def clear(queue = nil) queue ||= queues + ['failed'] diff --git a/lib/qu/backend/redis.rb b/lib/qu/backend/redis.rb index 51edf43..799ea3c 100644 --- a/lib/qu/backend/redis.rb +++ b/lib/qu/backend/redis.rb @@ -10,10 +10,10 @@ def initialize self.namespace = :qu end - def redis - @redis ||= Qu.connection || - ::Redis::Namespace.new(namespace, :redis => ::Redis.connect(:url => ENV['REDISTOGO_URL'])) + def connection + @connection ||= ::Redis::Namespace.new(namespace, :redis => ::Redis.connect(:url => ENV['REDISTOGO_URL'])) end + alias_method :redis, :connection def enqueue(klass, *args) job = Job.new(SimpleUUID::UUID.new.to_guid, klass, args) diff --git a/lib/qu/backend/spec.rb b/lib/qu/backend/spec.rb index 723049a..927e514 100644 --- a/lib/qu/backend/spec.rb +++ b/lib/qu/backend/spec.rb @@ -242,4 +242,16 @@ def timeout(count = 0.1, &block) subject.workers.first.id.should == worker.id end end + + describe 'connection=' do + it 'should allow setting the connection' do + connection = mock('a connection') + subject.connection = connection + subject.connection.should == connection + end + + it 'should provide a default connection' do + subject.connection.should_not be_nil + end + end end diff --git a/spec/qu/backend/mongo_spec.rb b/spec/qu/backend/mongo_spec.rb index c63e03f..397d7ae 100644 --- a/spec/qu/backend/mongo_spec.rb +++ b/spec/qu/backend/mongo_spec.rb @@ -3,4 +3,21 @@ describe Qu::Backend::Mongo do it_should_behave_like 'a backend' + + describe 'connection' do + it 'should default the qu database' do + subject.connection.should be_instance_of(Mongo::DB) + subject.connection.name.should == 'qu' + end + + it 'should use MONGOHQ_URL from heroku' do + Mongo::Connection.any_instance.stub(:connect) + ENV['MONGOHQ_URL'] = 'mongodb://user:pw@host:10060/quspec' + subject.connection.name.should == 'quspec' + # debugger + subject.connection.connection.host_to_try.should == ['host', 10060] + subject.connection.connection.auths.should == [{'db_name' => 'quspec', 'username' => 'user', 'password' => 'pw'}] + end + + end end diff --git a/spec/qu/backend/redis_spec.rb b/spec/qu/backend/redis_spec.rb index 3d326a3..ed34575 100644 --- a/spec/qu/backend/redis_spec.rb +++ b/spec/qu/backend/redis_spec.rb @@ -27,27 +27,22 @@ end describe 'connection' do - it 'should use the qu connection' do - Qu.connection = mock('a connection') - subject.redis.should == Qu.connection - end - it 'should create default connection if one not provided' do - subject.redis.client.host.should == '127.0.0.1' - subject.redis.client.port.should == 6379 - subject.redis.namespace.should == :qu + subject.connection.client.host.should == '127.0.0.1' + subject.connection.client.port.should == 6379 + subject.connection.namespace.should == :qu end it 'should use REDISTOGO_URL from heroku with namespace' do ENV['REDISTOGO_URL'] = 'redis://0.0.0.0:9876' - subject.redis.client.host.should == '0.0.0.0' - subject.redis.client.port.should == 9876 - subject.redis.namespace.should == :qu + subject.connection.client.host.should == '0.0.0.0' + subject.connection.client.port.should == 9876 + subject.connection.namespace.should == :qu end it 'should allow customizing the namespace' do subject.namespace = :foobar - subject.redis.namespace.should == :foobar + subject.connection.namespace.should == :foobar end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4afd13e..96cbd39 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -5,7 +5,6 @@ RSpec.configure do |config| config.before do - Qu.connection = nil Qu.backend = mock('a backend', :reserve => nil, :failed => nil, :completed => nil, :register_worker => nil, :unregister_worker => nil) end