qu-mongo 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/qu/backend/mongo.rb +110 -0
  2. data/lib/qu-mongo.rb +4 -0
  3. metadata +112 -0
@@ -0,0 +1,110 @@
1
+ require 'mongo'
2
+
3
+ module Qu
4
+ module Backend
5
+ class Mongo < Base
6
+ def connection
7
+ @connection ||= begin
8
+ uri = URI.parse(ENV['MONGOHQ_URL'].to_s)
9
+ database = uri.path.empty? ? 'qu' : uri.path[1..-1]
10
+ options = {}
11
+ if uri.password
12
+ options[:auths] = [{
13
+ 'db_name' => database,
14
+ 'username' => uri.user,
15
+ 'password' => uri.password
16
+ }]
17
+ end
18
+ ::Mongo::Connection.new(uri.host, uri.port, options).db(database)
19
+ end
20
+ end
21
+ alias_method :database, :connection
22
+
23
+ def clear(queue = nil)
24
+ queue ||= queues + ['failed']
25
+ Array(queue).each do |q|
26
+ jobs(q).drop
27
+ self[:queues].remove({:name => q})
28
+ end
29
+ end
30
+
31
+ def queues
32
+ self[:queues].find.map {|doc| doc['name'] }
33
+ end
34
+
35
+ def length(queue = 'default')
36
+ jobs(queue).count
37
+ end
38
+
39
+ def enqueue(klass, *args)
40
+ job = Qu::Job.new(BSON::ObjectId.new, klass, args)
41
+ jobs(job.queue).insert({:_id => job.id, :class => job.klass.to_s, :args => job.args})
42
+ self[:queues].update({:name => job.queue}, {:name => job.queue}, :upsert => true)
43
+ job
44
+ end
45
+
46
+ def reserve(worker, options = {:block => true})
47
+ worker.queues.each do |queue|
48
+ begin
49
+ doc = jobs(queue).find_and_modify(:remove => true)
50
+ return Job.new(doc['_id'], doc['class'], doc['args'])
51
+ rescue ::Mongo::OperationFailure
52
+ # No jobs in the queue
53
+ end
54
+ end
55
+
56
+ if options[:block]
57
+ sleep 5
58
+ retry
59
+ end
60
+ end
61
+
62
+ def release(job)
63
+ jobs(job.queue).insert({:_id => job.id, :class => job.klass.to_s, :args => job.args})
64
+ end
65
+
66
+ def failed(job, error)
67
+ jobs('failed').insert(:_id => job.id, :class => job.klass.to_s, :args => job.args, :queue => job.queue)
68
+ end
69
+
70
+ def completed(job)
71
+ end
72
+
73
+ def requeue(id)
74
+ doc = jobs('failed').find_and_modify(:query => {:_id => id}, :remove => true)
75
+ jobs(doc.delete('queue')).insert(doc)
76
+ Job.new(doc['_id'], doc['class'], doc['args'])
77
+ rescue ::Mongo::OperationFailure
78
+ false
79
+ end
80
+
81
+ def register_worker(worker)
82
+ self[:workers].insert(worker.attributes.merge(:id => worker.id))
83
+ end
84
+
85
+ def unregister_worker(id)
86
+ self[:workers].remove(:id => id)
87
+ end
88
+
89
+ def workers
90
+ self[:workers].find.map do |doc|
91
+ Qu::Worker.new(doc)
92
+ end
93
+ end
94
+
95
+ def clear_workers
96
+ self[:workers].drop
97
+ end
98
+
99
+ private
100
+
101
+ def jobs(queue)
102
+ self["queue:#{queue}"]
103
+ end
104
+
105
+ def [](name)
106
+ database["qu.#{name}"]
107
+ end
108
+ end
109
+ end
110
+ end
data/lib/qu-mongo.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'qu'
2
+ require 'qu/backend/mongo'
3
+
4
+ Qu.backend = Qu::Backend::Mongo.new
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: qu-mongo
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Brandon Keepers
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-09-23 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: mongo
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: qu
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - "="
42
+ - !ruby/object:Gem::Version
43
+ hash: 27
44
+ segments:
45
+ - 0
46
+ - 1
47
+ - 0
48
+ version: 0.1.0
49
+ type: :runtime
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: bson_ext
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ type: :development
64
+ version_requirements: *id003
65
+ description: Mongo backend for qu
66
+ email:
67
+ - brandon@opensoul.org
68
+ executables: []
69
+
70
+ extensions: []
71
+
72
+ extra_rdoc_files: []
73
+
74
+ files:
75
+ - lib/qu-mongo.rb
76
+ - lib/qu/backend/mongo.rb
77
+ has_rdoc: true
78
+ homepage: http://github.com/bkeepers/qu
79
+ licenses: []
80
+
81
+ post_install_message:
82
+ rdoc_options: []
83
+
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ hash: 3
92
+ segments:
93
+ - 0
94
+ version: "0"
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ hash: 3
101
+ segments:
102
+ - 0
103
+ version: "0"
104
+ requirements: []
105
+
106
+ rubyforge_project:
107
+ rubygems_version: 1.6.1
108
+ signing_key:
109
+ specification_version: 3
110
+ summary: Mongo backend for qu
111
+ test_files: []
112
+