rabbit_jobs 0.0.8.4 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -22,23 +22,6 @@ module RabbitJobs
22
22
  end
23
23
  end
24
24
 
25
- def em_amqp_with_exchange(&block)
26
- raise ArgumentError unless block
27
-
28
- connection = AMQP.connect(host: RJ.config.host)
29
- channel = AMQP::Channel.new(connection)
30
-
31
- channel.on_error do |ch, channel_close|
32
- puts "Channel-level error: #{channel_close.reply_text}, shutting down..."
33
- connection.close { EM.stop }
34
- end
35
-
36
- exchange = channel.direct(RJ.config[:exchange], RJ.config[:exchange_params])
37
-
38
- # go work
39
- block.call(connection, exchange)
40
- end
41
-
42
25
  def amqp_with_queue(routing_key, &block)
43
26
  raise ArgumentError unless routing_key && block
44
27
 
@@ -15,20 +15,15 @@ module RabbitJobs
15
15
  end
16
16
 
17
17
  def publish_to(routing_key, klass, opts = {}, *params)
18
- raise ArgumentError unless klass && routing_key
18
+ raise ArgumentError unless klass && routing_key && klass.is_a?(Class)# && (routing_key.is_a?(Hash) || routing_key.is_a?(String))
19
19
  opts ||= {}
20
20
 
21
21
  job = klass.new(*params)
22
22
  job.opts = opts
23
23
 
24
- if defined?(EM) && EM.reactor_running?
25
- em_publish_job_to(routing_key, job)
26
- else
27
- publish_job_to(routing_key, job)
28
- end
29
- end
24
+ dont_stop_em = defined?(EM) && EM.reactor_running?
25
+ dont_close_connection = AMQP.connection && AMQP.connection.open?
30
26
 
31
- def publish_job_to(routing_key, job)
32
27
  amqp_with_exchange do |connection, exchange|
33
28
 
34
29
  queue = make_queue(exchange, routing_key.to_s)
@@ -37,23 +32,11 @@ module RabbitJobs
37
32
 
38
33
  payload = job.payload
39
34
  exchange.publish(job.payload, Configuration::DEFAULT_MESSAGE_PARAMS.merge({routing_key: routing_key.to_s})) {
40
- connection.close {
41
- EM.stop
42
- }
43
- }
44
- end
45
- end
46
-
47
- def em_publish_job_to(routing_key, job)
48
- em_amqp_with_exchange do |connection, exchange|
49
- queue = make_queue(exchange, routing_key.to_s)
50
-
51
- job.opts['created_at'] = Time.now.to_i
52
-
53
- payload = job.payload
54
- exchange.publish(job.payload, Configuration::DEFAULT_MESSAGE_PARAMS.merge({routing_key: routing_key.to_s})) {
55
- connection.close {
56
- }
35
+ unless dont_close_connection
36
+ connection.close {
37
+ EM.stop unless dont_stop_em
38
+ }
39
+ end
57
40
  }
58
41
  end
59
42
  end
@@ -61,6 +44,9 @@ module RabbitJobs
61
44
  def purge_queue(*routing_keys)
62
45
  raise ArgumentError unless routing_keys && routing_keys.count > 0
63
46
 
47
+ dont_stop_em = defined?(EM) && EM.reactor_running?
48
+ dont_close_connection = AMQP.connection && AMQP.connection.open?
49
+
64
50
  amqp_with_exchange do |connection, exchange|
65
51
  queues_purged = routing_keys.count
66
52
 
@@ -75,10 +61,16 @@ module RabbitJobs
75
61
  queue.purge {
76
62
  queues_purged -= 1
77
63
  if queues_purged == 0
78
- connection.close {
79
- EM.stop
64
+
65
+ if dont_close_connection
66
+ puts 'return with not closed connection'
80
67
  return messages_count
81
- }
68
+ else
69
+ connection.close {
70
+ EM.stop unless dont_stop_em
71
+ return messages_count
72
+ }
73
+ end
82
74
  end
83
75
  }
84
76
  end
@@ -1,3 +1,3 @@
1
1
  module RabbitJobs
2
- VERSION = "0.0.8.4"
2
+ VERSION = "0.0.9"
3
3
  end
@@ -28,6 +28,19 @@ class ExpiredJob
28
28
  end
29
29
  end
30
30
 
31
+ class JobWithPublish
32
+ include RJ::Job
33
+
34
+ def self.perform(param = 0)
35
+ if param < 5
36
+ puts "publishing job #{param}"
37
+ RJ.publish JobWithPublish, param + 1
38
+ else
39
+ puts "processing job #{param}"
40
+ end
41
+ end
42
+ end
43
+
31
44
  class JobWithErrorHook
32
45
  include RJ::Job
33
46
  on_error :first_hook, lambda { puts "second hook" }, :last_hook
@@ -16,4 +16,16 @@ describe RabbitJobs::Worker do
16
16
 
17
17
  worker.work(1) # work for 1 second
18
18
  end
19
+
20
+ it 'should allow to publish jobs from worker' do
21
+ RabbitJobs.configure do |c|
22
+ c.exchange 'test_durable', auto_delete: false, durable: true
23
+ c.queue 'rspec_durable_queue', auto_delete: false, durable: true, ack: true
24
+ end
25
+
26
+ RabbitJobs.publish(JobWithPublish)
27
+ worker = RabbitJobs::Worker.new
28
+
29
+ worker.work(1) # work for 1 second
30
+ end
19
31
  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.8.4
4
+ version: 0.0.9
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-28 00:00:00.000000000 Z
12
+ date: 2012-03-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: amqp
16
- requirement: &70113688710280 !ruby/object:Gem::Requirement
16
+ requirement: &70108928613220 !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: *70113688710280
24
+ version_requirements: *70108928613220
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &70113690593620 !ruby/object:Gem::Requirement
27
+ requirement: &70108932737200 !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: *70113690593620
35
+ version_requirements: *70108932737200
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rufus-scheduler
38
- requirement: &70113690593080 !ruby/object:Gem::Requirement
38
+ requirement: &70108932736660 !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: *70113690593080
46
+ version_requirements: *70108932736660
47
47
  description: Background jobs on RabbitMQ
48
48
  email:
49
49
  - lazureykis@gmail.com