qu-mongoid 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/.travis.yml +2 -0
- data/Gemfile +14 -0
- data/LICENSE +22 -0
- data/README.md +43 -0
- data/Rakefile +11 -0
- data/lib/qu-mongoid.rb +4 -0
- data/lib/qu-mongoid/version.rb +5 -0
- data/lib/qu/backend/mongoid.rb +131 -0
- data/qu-mongoid.gemspec +19 -0
- data/spec/qu/backend/mongoid_spec.rb +50 -0
- data/spec/spec_helper.rb +14 -0
- metadata +90 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
source :rubygems
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in qu-mongoid.gemspec
|
4
|
+
gemspec
|
5
|
+
|
6
|
+
group :test do
|
7
|
+
gem 'SystemTimer', :platform => :mri_18
|
8
|
+
gem 'ruby-debug', :platform => :mri_18
|
9
|
+
gem 'debugger', :platform => :mri_19
|
10
|
+
gem 'rake'
|
11
|
+
gem 'rspec', '~> 2.0'
|
12
|
+
gem 'guard-rspec'
|
13
|
+
gem 'guard-bundler'
|
14
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 dwbutler
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# Qu-Mongoid
|
2
|
+
[](http://travis-ci.org/dwbutler/qu-mongoid)
|
3
|
+
|
4
|
+
This gem provides a Mongoid 3 / Moped backend for the queueing library [Qu](http://github.com/bkeepers/qu). See the documentation for Qu for more information.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'qu'
|
11
|
+
gem 'qu-mongoid'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install qu-mongoid
|
20
|
+
|
21
|
+
## Why not just use the Mongo backend?
|
22
|
+
|
23
|
+
Starting with version 3, Mongoid uses its own mongoDB driver (Moped) instead of the official 10gen ruby driver (mongo). To avoid loading both drivers, I ported the Mongo backend to Mongoid/Moped.
|
24
|
+
|
25
|
+
Mongoid version 2 and below uses the mongo driver, so use qu-mongo if you are on Mongoid 2.
|
26
|
+
|
27
|
+
## Configuration
|
28
|
+
|
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'], so it should work on Heroku. If you need to use a different Mongoid connection, you can do the following:
|
30
|
+
|
31
|
+
``` ruby
|
32
|
+
Qu.configure do |c|
|
33
|
+
c.connection = Mongoid::Sessions.with_name(:qu)
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
## Contributing
|
38
|
+
|
39
|
+
1. Fork it
|
40
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
41
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
42
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
43
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/qu-mongoid.rb
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
require 'mongoid'
|
2
|
+
|
3
|
+
module Qu
|
4
|
+
module Backend
|
5
|
+
class Mongoid < Base
|
6
|
+
|
7
|
+
# Number of times to retry connection on connection failure (default: 5)
|
8
|
+
attr_accessor :max_retries
|
9
|
+
|
10
|
+
# Seconds to wait before try to reconnect after connection failure (default: 1)
|
11
|
+
attr_accessor :retry_frequency
|
12
|
+
|
13
|
+
# Seconds to wait before looking for more jobs when the queue is empty (default: 5)
|
14
|
+
attr_accessor :poll_frequency
|
15
|
+
|
16
|
+
def initialize
|
17
|
+
self.max_retries = 5
|
18
|
+
self.retry_frequency = 1
|
19
|
+
self.poll_frequency = 5
|
20
|
+
end
|
21
|
+
|
22
|
+
def connection
|
23
|
+
@connection ||= begin
|
24
|
+
unless ::Mongoid.sessions[:default]
|
25
|
+
if (uri = (ENV['MONGOHQ_URL'] || ENV['MONGOLAB_URI']).to_s) && !uri.empty?
|
26
|
+
::Mongoid.sessions = {:default => {:uri => uri, :max_retries_on_connection_failure => 4}}
|
27
|
+
else
|
28
|
+
::Mongoid.connect_to('qu')
|
29
|
+
end
|
30
|
+
end
|
31
|
+
::Mongoid::Sessions.default
|
32
|
+
end
|
33
|
+
end
|
34
|
+
alias_method :database, :connection
|
35
|
+
|
36
|
+
def clear(queue = nil)
|
37
|
+
queue ||= queues + ['failed']
|
38
|
+
logger.info { "Clearing queues: #{queue.inspect}" }
|
39
|
+
Array(queue).each do |q|
|
40
|
+
logger.debug "Clearing queue #{q}"
|
41
|
+
jobs(q).drop
|
42
|
+
self[:queues].where({:name => q}).remove
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def queues
|
47
|
+
self[:queues].find.map {|doc| doc['name'] }
|
48
|
+
end
|
49
|
+
|
50
|
+
def length(queue = 'default')
|
51
|
+
jobs(queue).find.count
|
52
|
+
end
|
53
|
+
|
54
|
+
def enqueue(payload)
|
55
|
+
payload.id = ::Moped::BSON::ObjectId.new
|
56
|
+
jobs(payload.queue).insert({:_id => payload.id, :klass => payload.klass.to_s, :args => payload.args})
|
57
|
+
self[:queues].where({:name => payload.queue}).upsert({:name => payload.queue})
|
58
|
+
logger.debug { "Enqueued job #{payload}" }
|
59
|
+
payload
|
60
|
+
end
|
61
|
+
|
62
|
+
def reserve(worker, options = {:block => true})
|
63
|
+
loop do
|
64
|
+
worker.queues.each do |queue|
|
65
|
+
logger.debug { "Reserving job in queue #{queue}" }
|
66
|
+
|
67
|
+
begin
|
68
|
+
doc = connection.command(:findAndModify => jobs(queue).name, :remove => true)
|
69
|
+
if doc && doc['value']
|
70
|
+
doc = doc['value']
|
71
|
+
doc['id'] = doc.delete('_id')
|
72
|
+
return Payload.new(doc)
|
73
|
+
end
|
74
|
+
rescue ::Moped::Errors::OperationFailure
|
75
|
+
# No jobs in the queue (MongoDB <2)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
if options[:block]
|
80
|
+
sleep poll_frequency
|
81
|
+
else
|
82
|
+
break
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def release(payload)
|
88
|
+
jobs(payload.queue).insert({:_id => payload.id, :klass => payload.klass.to_s, :args => payload.args})
|
89
|
+
end
|
90
|
+
|
91
|
+
def failed(payload, error)
|
92
|
+
jobs('failed').insert(:_id => payload.id, :klass => payload.klass.to_s, :args => payload.args, :queue => payload.queue)
|
93
|
+
end
|
94
|
+
|
95
|
+
def completed(payload)
|
96
|
+
end
|
97
|
+
|
98
|
+
def register_worker(worker)
|
99
|
+
logger.debug "Registering worker #{worker.id}"
|
100
|
+
self[:workers].insert(worker.attributes.merge(:id => worker.id))
|
101
|
+
end
|
102
|
+
|
103
|
+
def unregister_worker(worker)
|
104
|
+
logger.debug "Unregistering worker #{worker.id}"
|
105
|
+
self[:workers].where(:id => worker.id).remove
|
106
|
+
end
|
107
|
+
|
108
|
+
def workers
|
109
|
+
self[:workers].find.map do |doc|
|
110
|
+
Qu::Worker.new(doc)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
def clear_workers
|
115
|
+
logger.info "Clearing workers"
|
116
|
+
self[:workers].drop
|
117
|
+
end
|
118
|
+
|
119
|
+
private
|
120
|
+
|
121
|
+
def jobs(queue)
|
122
|
+
self["queue:#{queue}"]
|
123
|
+
end
|
124
|
+
|
125
|
+
def [](name)
|
126
|
+
database["qu:#{name}"]
|
127
|
+
end
|
128
|
+
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
data/qu-mongoid.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "qu-mongoid/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "qu-mongoid"
|
7
|
+
s.version = Qu::Mongoid::VERSION
|
8
|
+
s.authors = ["David Butler"]
|
9
|
+
s.email = ["dwbutler@ucla.edu"]
|
10
|
+
s.homepage = "http://github.com/dwbutler/qu-mongoid"
|
11
|
+
s.summary = "Mongoid backend for Qu"
|
12
|
+
s.description = "Mongoid backend for the Ruby queuing library Qu"
|
13
|
+
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
s.require_paths = ["lib"]
|
16
|
+
|
17
|
+
s.add_dependency 'mongoid', '~> 3.0'
|
18
|
+
s.add_dependency 'qu'
|
19
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'qu-mongoid'
|
3
|
+
|
4
|
+
describe Qu::Backend::Mongoid do
|
5
|
+
it_should_behave_like 'a backend'
|
6
|
+
|
7
|
+
describe 'connection' do
|
8
|
+
it 'should default the qu database' do
|
9
|
+
subject.connection.should be_instance_of(Moped::Session)
|
10
|
+
subject.connection.options[:database].should == 'qu'
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'should use MONGOHQ_URL from heroku' do
|
14
|
+
# Clean up from other tests
|
15
|
+
::Mongoid.sessions[:default] = nil
|
16
|
+
subject.instance_eval {@connection=nil}
|
17
|
+
::Mongoid::Sessions.clear
|
18
|
+
|
19
|
+
ENV['MONGOHQ_URL'] = 'mongodb://127.0.0.1:27017/quspec'
|
20
|
+
subject.connection.options[:database].should == 'quspec'
|
21
|
+
subject.connection.cluster.nodes.first.resolved_address.should == "127.0.0.1:27017"
|
22
|
+
::Mongoid.sessions[:default][:hosts].should include("127.0.0.1:27017")
|
23
|
+
|
24
|
+
# Clean up MONGOHQ stuff
|
25
|
+
ENV.delete('MONGOHQ_URL')
|
26
|
+
subject.instance_eval {@connection=nil}
|
27
|
+
::Mongoid.connect_to('qu')
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe 'reserve' do
|
32
|
+
let(:worker) { Qu::Worker.new }
|
33
|
+
|
34
|
+
describe "on mongo >=2" do
|
35
|
+
it 'should return nil when no jobs exist' do
|
36
|
+
subject.clear
|
37
|
+
Moped::Session.any_instance.should_receive(:command).and_return(nil)
|
38
|
+
lambda { subject.reserve(worker, :block => false).should be_nil }.should_not raise_error
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe 'on mongo <2' do
|
43
|
+
it 'should return nil when no jobs exist' do
|
44
|
+
subject.clear
|
45
|
+
subject.connection.should_receive(:command).and_raise(Moped::Errors::OperationFailure.new(nil, 'test'))
|
46
|
+
lambda { subject.reserve(worker, :block => false).should be_nil }.should_not raise_error
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler.require :test
|
3
|
+
require 'qu'
|
4
|
+
require 'qu/backend/spec'
|
5
|
+
|
6
|
+
RSpec.configure do |config|
|
7
|
+
config.before do
|
8
|
+
Qu.backend = mock('a backend', :reserve => nil, :failed => nil, :completed => nil,
|
9
|
+
:register_worker => nil, :unregister_worker => nil)
|
10
|
+
Qu.failure = nil
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
Qu.logger = Logger.new('/dev/null')
|
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: qu-mongoid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- David Butler
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-09-18 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: mongoid
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: qu
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: Mongoid backend for the Ruby queuing library Qu
|
47
|
+
email:
|
48
|
+
- dwbutler@ucla.edu
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- .travis.yml
|
55
|
+
- Gemfile
|
56
|
+
- LICENSE
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- lib/qu-mongoid.rb
|
60
|
+
- lib/qu-mongoid/version.rb
|
61
|
+
- lib/qu/backend/mongoid.rb
|
62
|
+
- qu-mongoid.gemspec
|
63
|
+
- spec/qu/backend/mongoid_spec.rb
|
64
|
+
- spec/spec_helper.rb
|
65
|
+
homepage: http://github.com/dwbutler/qu-mongoid
|
66
|
+
licenses: []
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
requirements: []
|
84
|
+
rubyforge_project:
|
85
|
+
rubygems_version: 1.8.24
|
86
|
+
signing_key:
|
87
|
+
specification_version: 3
|
88
|
+
summary: Mongoid backend for Qu
|
89
|
+
test_files: []
|
90
|
+
has_rdoc:
|