rabbit_jobs 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,7 +7,7 @@ module RabbitJobs
7
7
  def amqp_with_exchange(&block)
8
8
  raise ArgumentError unless block
9
9
 
10
- AMQP.start(host: RabbitJobs.config.host) do |connection|
10
+ AMQP.start(host: RJ.config.host) do |connection|
11
11
  channel = AMQP::Channel.new(connection)
12
12
 
13
13
  channel.on_error do |ch, channel_close|
@@ -15,7 +15,7 @@ module RabbitJobs
15
15
  connection.close { EM.stop }
16
16
  end
17
17
 
18
- exchange = channel.direct(RabbitJobs.config[:exchange], RabbitJobs.config[:exchange_params])
18
+ exchange = channel.direct(RJ.config[:exchange], RJ.config[:exchange_params])
19
19
 
20
20
  # go work
21
21
  block.call(connection, exchange)
@@ -25,7 +25,7 @@ module RabbitJobs
25
25
  def em_amqp_with_exchange(&block)
26
26
  raise ArgumentError unless block
27
27
 
28
- connection = AMQP.connect(host: RabbitJobs.config.host)
28
+ connection = AMQP.connect(host: RJ.config.host)
29
29
  channel = AMQP::Channel.new(connection)
30
30
 
31
31
  channel.on_error do |ch, channel_close|
@@ -33,19 +33,17 @@ module RabbitJobs
33
33
  connection.close { EM.stop }
34
34
  end
35
35
 
36
- exchange = channel.direct(RabbitJobs.config[:exchange], RabbitJobs.config[:exchange_params])
36
+ exchange = channel.direct(RJ.config[:exchange], RJ.config[:exchange_params])
37
37
 
38
38
  # go work
39
39
  block.call(connection, exchange)
40
40
  end
41
41
 
42
42
  def amqp_with_queue(routing_key, &block)
43
-
44
43
  raise ArgumentError unless routing_key && block
45
44
 
46
45
  amqp_with_exchange do |connection, exchange|
47
- queue = exchange.channel.queue(RabbitJobs.config.queue_name(routing_key), RabbitJobs.config[:queues][routing_key])
48
- queue.bind(exchange, :routing_key => routing_key)
46
+ queue = make_queue(exchange, routing_key.to_s)
49
47
 
50
48
  # go work
51
49
  block.call(connection, queue)
@@ -53,7 +51,7 @@ module RabbitJobs
53
51
  end
54
52
 
55
53
  def make_queue(exchange, routing_key)
56
- queue = exchange.channel.queue(RabbitJobs.config.queue_name(routing_key), RabbitJobs.config[:queues][routing_key])
54
+ queue = exchange.channel.queue(RJ.config.queue_name(routing_key), RJ.config[:queues][routing_key])
57
55
  queue.bind(exchange, :routing_key => routing_key)
58
56
  queue
59
57
  end
@@ -26,6 +26,12 @@ module RabbitJobs::Job
26
26
  yield if block_given?
27
27
  else
28
28
  begin
29
+ if defined?(ActiveRecord::Base)
30
+ ActiveRecord::Base.establish_connection
31
+ end
32
+ if defined?(MongoMapper)
33
+ MongoMapper.database.connection.connect_to_master
34
+ end
29
35
  # log 'before perform'
30
36
  self.class.perform(*params)
31
37
  # log 'after perform'
@@ -10,7 +10,7 @@ module RabbitJobs
10
10
  extend AmqpHelpers
11
11
 
12
12
  def publish(klass, opts = {}, *params)
13
- key = RabbitJobs.config.routing_keys.first
13
+ key = RJ.config.routing_keys.first
14
14
  publish_to(key, klass, opts, *params)
15
15
  end
16
16
 
@@ -31,12 +31,12 @@ module RabbitJobs
31
31
  def publish_job_to(routing_key, job)
32
32
  amqp_with_exchange do |connection, exchange|
33
33
 
34
- queue = make_queue(exchange, routing_key)
34
+ queue = make_queue(exchange, routing_key.to_s)
35
35
 
36
36
  job.opts['created_at'] = Time.now.to_i
37
37
 
38
38
  payload = job.payload
39
- exchange.publish(job.payload, Configuration::DEFAULT_MESSAGE_PARAMS.merge({routing_key: routing_key})) {
39
+ exchange.publish(job.payload, Configuration::DEFAULT_MESSAGE_PARAMS.merge({routing_key: routing_key.to_s})) {
40
40
  connection.close {
41
41
  EM.stop
42
42
  }
@@ -46,29 +46,41 @@ module RabbitJobs
46
46
 
47
47
  def em_publish_job_to(routing_key, job)
48
48
  em_amqp_with_exchange do |connection, exchange|
49
- queue = make_queue(exchange, routing_key)
49
+ queue = make_queue(exchange, routing_key.to_s)
50
50
 
51
51
  job.opts['created_at'] = Time.now.to_i
52
52
 
53
53
  payload = job.payload
54
- exchange.publish(job.payload, Configuration::DEFAULT_MESSAGE_PARAMS.merge({routing_key: routing_key})) {
54
+ exchange.publish(job.payload, Configuration::DEFAULT_MESSAGE_PARAMS.merge({routing_key: routing_key.to_s})) {
55
55
  connection.close {
56
56
  }
57
57
  }
58
58
  end
59
59
  end
60
60
 
61
- def purge_queue(routing_key)
62
- raise ArgumentError unless routing_key
61
+ def purge_queue(*routing_keys)
62
+ raise ArgumentError unless routing_keys && routing_keys.count > 0
63
63
 
64
- amqp_with_queue(routing_key) do |connection, queue|
65
- queue.status do |number_of_messages, number_of_consumers|
66
- queue.purge {
67
- connection.close {
68
- EM.stop
69
- return number_of_messages
64
+ amqp_with_exchange do |connection, exchange|
65
+ queues_purged = routing_keys.count
66
+
67
+ messages_count = 0
68
+ routing_keys.each do |routing_key|
69
+ queue = exchange.channel.queue(RabbitJobs.config.queue_name(routing_key), RabbitJobs.config[:queues][routing_key])
70
+ queue.bind(exchange, :routing_key => routing_key)
71
+
72
+ queue.status do |number_of_messages, number_of_consumers|
73
+ messages_count += number_of_messages
74
+ queue.purge {
75
+ queues_purged -= 1
76
+ if queues_purged == 0
77
+ connection.close {
78
+ EM.stop
79
+ return messages_count
80
+ }
81
+ end
70
82
  }
71
- }
83
+ end
72
84
  end
73
85
  end
74
86
  end
@@ -1,3 +1,3 @@
1
1
  module RabbitJobs
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
@@ -54,13 +54,20 @@ module RabbitJobs
54
54
 
55
55
  log "Worker ##{Process.pid} <= #{exchange.name}##{routing_key}"
56
56
 
57
- queue.subscribe(ack: true) do |metadata, payload|
58
- @job = RabbitJobs::Job.parse(payload)
57
+ explicit_ack = !!RJ.config[:queues][routing_key][:ack]
58
+
59
+ queue.subscribe(ack: explicit_ack) do |metadata, payload|
60
+ @job = RJ::Job.parse(payload)
61
+
59
62
  unless @job.expired?
60
63
  @job.run_perform
61
- metadata.ack
62
64
  processed_count += 1
65
+ else
66
+ log "Job expired: #{@job.inspect}"
63
67
  end
68
+
69
+ metadata.ack if explicit_ack
70
+
64
71
  check_shutdown.call
65
72
  end
66
73
  end
data/lib/rabbit_jobs.rb CHANGED
@@ -25,6 +25,4 @@ module RabbitJobs
25
25
  end
26
26
  end
27
27
 
28
- module RJ
29
- include RabbitJobs
30
- end
28
+ RJ = RabbitJobs
@@ -16,4 +16,9 @@ describe RabbitJobs::Publisher do
16
16
  RabbitJobs.publish(TestJob, nil, 'some', 'other', 'params')
17
17
  RabbitJobs::Publisher.purge_queue('rspec_queue').should == 1
18
18
  end
19
+
20
+ it 'should accept symbol as queue name' do
21
+ RabbitJobs.publish_to(:rspec_queue, TestJob)
22
+ RabbitJobs::Publisher.purge_queue('rspec_queue').should == 1
23
+ end
19
24
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rabbit_jobs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-01 00:00:00.000000000 Z
12
+ date: 2012-02-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: amqp
16
- requirement: &70238047574860 !ruby/object:Gem::Requirement
16
+ requirement: &70275283417720 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0.9'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70238047574860
24
+ version_requirements: *70275283417720
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &70238047573560 !ruby/object:Gem::Requirement
27
+ requirement: &70275283417300 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70238047573560
35
+ version_requirements: *70275283417300
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rufus-scheduler
38
- requirement: &70238047572140 !ruby/object:Gem::Requirement
38
+ requirement: &70275283416760 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '2.0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70238047572140
46
+ version_requirements: *70275283416760
47
47
  description: Background jobs on RabbitMQ
48
48
  email:
49
49
  - lazureykis@gmail.com
@@ -105,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
105
105
  version: '0'
106
106
  requirements: []
107
107
  rubyforge_project:
108
- rubygems_version: 1.8.10
108
+ rubygems_version: 1.8.15
109
109
  signing_key:
110
110
  specification_version: 3
111
111
  summary: Background jobs on RabbitMQ