qu-mongoid 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -0
- data/README.md +14 -4
- data/lib/qu-mongoid/version.rb +1 -1
- data/lib/qu/backend/mongoid.rb +16 -3
- data/spec/qu/backend/mongoid_spec.rb +39 -3
- metadata +27 -24
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -7,8 +7,10 @@ This gem provides a Mongoid 3 / Moped backend for the queueing library [Qu](http
|
|
7
7
|
|
8
8
|
Add this line to your application's Gemfile:
|
9
9
|
|
10
|
-
|
11
|
-
|
10
|
+
```ruby
|
11
|
+
gem 'qu'
|
12
|
+
gem 'qu-mongoid'
|
13
|
+
```
|
12
14
|
|
13
15
|
And then execute:
|
14
16
|
|
@@ -26,11 +28,19 @@ Mongoid version 2 and below uses the mongo driver, so use qu-mongo if you are on
|
|
26
28
|
|
27
29
|
## Configuration
|
28
30
|
|
29
|
-
Qu-Mongoid will automatically connect to the default session configured in mongoid.yml. If a default session is not configured, it will attempt to read from ENV['MONGOHQ_URL'] and ENV['MONGOLAB_URI']
|
31
|
+
Qu-Mongoid will automatically connect to the default session configured in mongoid.yml. If a default session is not configured, it will attempt to read from ```ENV['MONGOHQ_URL']``` and ```ENV['MONGOLAB_URI']```, so it should work on Heroku. If you need to use a different Mongoid session, you should do the following:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
Qu.configure do |c|
|
35
|
+
c.backend.session = :qu
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
You may also configure the Qu connection using the code below, but **WARNING**: if you are running your workers in threads, this configuration is **NOT** recommended. This is because Mongoid uses a separate connection on each thread, so only the original thread will use the configured connection. New threads will use the default Mongoid session as described above.
|
30
40
|
|
31
41
|
``` ruby
|
32
42
|
Qu.configure do |c|
|
33
|
-
c.connection = Mongoid::Sessions.with_name(:qu)
|
43
|
+
c.connection = Mongoid::Sessions.with_name(:qu) # New threads will not use this configured connection.
|
34
44
|
end
|
35
45
|
```
|
36
46
|
|
data/lib/qu-mongoid/version.rb
CHANGED
data/lib/qu/backend/mongoid.rb
CHANGED
@@ -12,27 +12,40 @@ module Qu
|
|
12
12
|
|
13
13
|
# Seconds to wait before looking for more jobs when the queue is empty (default: 5)
|
14
14
|
attr_accessor :poll_frequency
|
15
|
+
|
16
|
+
attr_accessor :session
|
15
17
|
|
16
18
|
def initialize
|
17
19
|
self.max_retries = 5
|
18
20
|
self.retry_frequency = 1
|
19
21
|
self.poll_frequency = 5
|
22
|
+
self.session = :default
|
20
23
|
end
|
21
24
|
|
22
25
|
def connection
|
23
|
-
|
24
|
-
unless ::Mongoid.sessions[
|
26
|
+
Thread.current[self.to_s] ||= begin
|
27
|
+
unless ::Mongoid.sessions[@session]
|
25
28
|
if (uri = (ENV['MONGOHQ_URL'] || ENV['MONGOLAB_URI']).to_s) && !uri.empty?
|
26
29
|
::Mongoid.sessions = {:default => {:uri => uri, :max_retries_on_connection_failure => 4}}
|
27
30
|
else
|
28
31
|
::Mongoid.connect_to('qu')
|
29
32
|
end
|
30
33
|
end
|
31
|
-
::Mongoid::Sessions.
|
34
|
+
::Mongoid::Sessions.with_name(@session)
|
32
35
|
end
|
33
36
|
end
|
34
37
|
alias_method :database, :connection
|
35
38
|
|
39
|
+
def connection=(conn)
|
40
|
+
warn %q(If you are using threads, setting the Qu::Mongoid connection will not work. Configure the session instead.
|
41
|
+
Example:
|
42
|
+
Qu.configure do |c|
|
43
|
+
c.backend.session = :qu
|
44
|
+
end
|
45
|
+
)
|
46
|
+
Thread.current[self.to_s] = conn
|
47
|
+
end
|
48
|
+
|
36
49
|
def clear(queue = nil)
|
37
50
|
queue ||= queues + ['failed']
|
38
51
|
logger.info { "Clearing queues: #{queue.inspect}" }
|
@@ -5,15 +5,51 @@ describe Qu::Backend::Mongoid do
|
|
5
5
|
it_should_behave_like 'a backend'
|
6
6
|
|
7
7
|
describe 'connection' do
|
8
|
-
it 'should
|
8
|
+
it 'should use a separate connection in each thread' do
|
9
|
+
backend = subject
|
10
|
+
connection = subject.connection
|
11
|
+
separate_connection = false
|
12
|
+
Thread.new do
|
13
|
+
separate_connection = (backend.connection != connection)
|
14
|
+
end.join
|
15
|
+
separate_connection.should be_true
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should default to the qu database' do
|
9
19
|
subject.connection.should be_instance_of(Moped::Session)
|
10
20
|
subject.connection.options[:database].should == 'qu'
|
11
21
|
end
|
12
22
|
|
23
|
+
it 'should default to the :default session' do
|
24
|
+
subject.session.should == :default
|
25
|
+
subject.connection.should == ::Mongoid::Sessions.with_name(subject.session)
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should have a configurable session that works with threads' do
|
29
|
+
subject.connection = nil
|
30
|
+
|
31
|
+
::Mongoid.sessions[:qu] = {:uri => 'mongodb://127.0.0.1:27017/quspec', :max_retries_on_connection_failure => 4}
|
32
|
+
Qu.backend = subject
|
33
|
+
Qu.configure do |c|
|
34
|
+
c.backend.session = :qu
|
35
|
+
end
|
36
|
+
subject.connection.should == ::Mongoid::Sessions.with_name(:qu)
|
37
|
+
|
38
|
+
should_have_qu_session_in_new_thread = false
|
39
|
+
Thread.new do
|
40
|
+
should_have_qu_session_in_new_thread = (subject.connection == ::Mongoid::Sessions.with_name(:qu))
|
41
|
+
end.join
|
42
|
+
should_have_qu_session_in_new_thread.should be_true
|
43
|
+
|
44
|
+
# Clean up
|
45
|
+
subject.connection=nil
|
46
|
+
::Mongoid.connect_to('qu')
|
47
|
+
end
|
48
|
+
|
13
49
|
it 'should use MONGOHQ_URL from heroku' do
|
14
50
|
# Clean up from other tests
|
15
51
|
::Mongoid.sessions[:default] = nil
|
16
|
-
subject.
|
52
|
+
subject.connection=nil
|
17
53
|
::Mongoid::Sessions.clear
|
18
54
|
|
19
55
|
ENV['MONGOHQ_URL'] = 'mongodb://127.0.0.1:27017/quspec'
|
@@ -23,7 +59,7 @@ describe Qu::Backend::Mongoid do
|
|
23
59
|
|
24
60
|
# Clean up MONGOHQ stuff
|
25
61
|
ENV.delete('MONGOHQ_URL')
|
26
|
-
subject.
|
62
|
+
subject.connection=nil
|
27
63
|
::Mongoid.connect_to('qu')
|
28
64
|
end
|
29
65
|
end
|
metadata
CHANGED
@@ -1,48 +1,50 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: qu-mongoid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
4
|
+
prerelease:
|
5
|
+
version: 0.2.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- David Butler
|
9
|
-
autorequire:
|
9
|
+
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-11-29 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: mongoid
|
16
|
-
|
17
|
-
none: false
|
16
|
+
version_requirements: !ruby/object:Gem::Requirement
|
18
17
|
requirements:
|
19
18
|
- - ~>
|
20
19
|
- !ruby/object:Gem::Version
|
21
20
|
version: '3.0'
|
22
|
-
type: :runtime
|
23
|
-
prerelease: false
|
24
|
-
version_requirements: !ruby/object:Gem::Requirement
|
25
21
|
none: false
|
22
|
+
requirement: !ruby/object:Gem::Requirement
|
26
23
|
requirements:
|
27
24
|
- - ~>
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '3.0'
|
27
|
+
none: false
|
28
|
+
prerelease: false
|
29
|
+
type: :runtime
|
30
30
|
- !ruby/object:Gem::Dependency
|
31
31
|
name: qu
|
32
|
-
|
33
|
-
none: false
|
32
|
+
version_requirements: !ruby/object:Gem::Requirement
|
34
33
|
requirements:
|
35
34
|
- - ! '>='
|
36
35
|
- !ruby/object:Gem::Version
|
37
|
-
version:
|
38
|
-
|
39
|
-
prerelease: false
|
40
|
-
version_requirements: !ruby/object:Gem::Requirement
|
36
|
+
version: !binary |-
|
37
|
+
MA==
|
41
38
|
none: false
|
39
|
+
requirement: !ruby/object:Gem::Requirement
|
42
40
|
requirements:
|
43
41
|
- - ! '>='
|
44
42
|
- !ruby/object:Gem::Version
|
45
|
-
version:
|
43
|
+
version: !binary |-
|
44
|
+
MA==
|
45
|
+
none: false
|
46
|
+
prerelease: false
|
47
|
+
type: :runtime
|
46
48
|
description: Mongoid backend for the Ruby queuing library Qu
|
47
49
|
email:
|
48
50
|
- dwbutler@ucla.edu
|
@@ -64,27 +66,28 @@ files:
|
|
64
66
|
- spec/spec_helper.rb
|
65
67
|
homepage: http://github.com/dwbutler/qu-mongoid
|
66
68
|
licenses: []
|
67
|
-
post_install_message:
|
69
|
+
post_install_message:
|
68
70
|
rdoc_options: []
|
69
71
|
require_paths:
|
70
72
|
- lib
|
71
73
|
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
-
none: false
|
73
74
|
requirements:
|
74
75
|
- - ! '>='
|
75
76
|
- !ruby/object:Gem::Version
|
76
|
-
version:
|
77
|
-
|
77
|
+
version: !binary |-
|
78
|
+
MA==
|
78
79
|
none: false
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
81
|
requirements:
|
80
82
|
- - ! '>='
|
81
83
|
- !ruby/object:Gem::Version
|
82
|
-
version:
|
84
|
+
version: !binary |-
|
85
|
+
MA==
|
86
|
+
none: false
|
83
87
|
requirements: []
|
84
|
-
rubyforge_project:
|
88
|
+
rubyforge_project:
|
85
89
|
rubygems_version: 1.8.24
|
86
|
-
signing_key:
|
90
|
+
signing_key:
|
87
91
|
specification_version: 3
|
88
92
|
summary: Mongoid backend for Qu
|
89
93
|
test_files: []
|
90
|
-
has_rdoc:
|