qu-mongo 0.1.0 → 0.1.1

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