rabbit_jobs 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -21,93 +21,32 @@ module RabbitJobs
21
21
  job = klass.new(*params)
22
22
  job.opts = {'created_at' => Time.now.to_i}
23
23
 
24
- b = Bunny.new(host: RJ.config.host, logging: false)
25
- b.start
26
-
27
- exchange = b.exchange(RJ.config[:exchange], RJ.config[:exchange_params])
28
- queue = b.queue(RJ.config.queue_name(routing_key), RJ.config[:queues][routing_key.to_s])
24
+ exchange = bunny.exchange(RJ.config[:exchange], RJ.config[:exchange_params])
25
+ queue = bunny.queue(RJ.config.queue_name(routing_key), RJ.config[:queues][routing_key.to_s])
29
26
  queue.bind(exchange)
30
27
 
31
28
  exchange.publish(job.payload, Configuration::DEFAULT_MESSAGE_PARAMS.merge({key: routing_key.to_s}))
32
- b.stop
29
+ # b.stop
33
30
  end
34
31
 
35
32
  def purge_queue(*routing_keys)
36
33
  raise ArgumentError unless routing_keys && routing_keys.count > 0
37
34
 
38
- b = Bunny.new(host: RJ.config.host, logging: false)
39
- b.start
40
-
41
35
  messages_count = 0
42
36
  routing_keys.each do |routing_key|
43
- queue = b.queue(RJ.config.queue_name(routing_key), RJ.config[:queues][routing_key.to_s])
37
+ queue = bunny.queue(RJ.config.queue_name(routing_key), RJ.config[:queues][routing_key.to_s])
44
38
  messages_count += queue.status[:message_count]
45
39
  queue.purge
46
40
  end
47
- b.stop
41
+ # b.stop
48
42
  return messages_count
49
43
  end
50
44
 
51
- def publish_to2(routing_key, klass, *params)
52
- raise ArgumentError unless klass && routing_key && klass.is_a?(Class) && (routing_key.is_a?(Hash) || routing_key.is_a?(String))
53
-
54
- job = klass.new(*params)
55
- job.opts = {}
56
-
57
- dont_stop_em = defined?(EM) && EM.reactor_running?
58
- dont_close_connection = AMQP.connection && AMQP.connection.open?
59
-
60
- amqp_with_exchange do |connection, exchange|
61
-
62
- queue = make_queue(exchange, routing_key.to_s)
63
-
64
- job.opts['created_at'] = Time.now.to_i
65
-
66
- payload = job.payload
67
- exchange.publish(job.payload, Configuration::DEFAULT_MESSAGE_PARAMS.merge({routing_key: routing_key.to_s})) {
68
- unless dont_close_connection
69
- connection.close {
70
- EM.stop unless dont_stop_em
71
- }
72
- end
73
- }
74
- end
75
- end
76
-
77
- def purge_queue2(*routing_keys)
78
- raise ArgumentError unless routing_keys && routing_keys.count > 0
79
-
80
- dont_stop_em = defined?(EM) && EM.reactor_running?
81
- dont_close_connection = AMQP.connection && AMQP.connection.open?
82
-
83
- amqp_with_exchange do |connection, exchange|
84
- queues_purged = routing_keys.count
85
-
86
- messages_count = 0
87
- routing_keys.each do |routing_key|
88
- routing_key = routing_key.to_s
89
- queue = exchange.channel.queue(RabbitJobs.config.queue_name(routing_key), RabbitJobs.config[:queues][routing_key])
90
- queue.bind(exchange, :routing_key => routing_key)
91
- queue.status do |number_of_messages, number_of_consumers|
92
- messages_count += number_of_messages
93
- queue.purge {
94
- queues_purged -= 1
95
- if queues_purged == 0
96
-
97
- if dont_close_connection
98
- puts 'return with not closed connection'
99
- return messages_count
100
- else
101
- connection.close {
102
- EM.stop unless dont_stop_em
103
- return messages_count
104
- }
105
- end
106
- end
107
- }
108
- end
109
- end
110
- end
45
+ private
46
+ def bunny
47
+ @bunny ||= Bunny.new(host: RJ.config.host, logging: false)
48
+ @bunny.start unless @bunny.status == :connected
49
+ @bunny
111
50
  end
112
51
  end
113
52
  end
@@ -1,3 +1,3 @@
1
1
  module RabbitJobs
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -1,6 +1,7 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  require 'spec_helper'
3
3
  require 'json'
4
+ require 'benchmark'
4
5
 
5
6
  describe RabbitJobs::Publisher do
6
7
 
@@ -36,4 +37,12 @@ describe RabbitJobs::Publisher do
36
37
  RabbitJobs.publish_to(:rspec_queue, JobWithArgsArray, 'first value', :some_symbol, 123, 'and string')
37
38
  RabbitJobs::Publisher.purge_queue(:rspec_queue).should == 1
38
39
  end
40
+
41
+ it 'should publish 1000 messages in one second' do
42
+ time = Benchmark.measure {
43
+ 1000.times { RabbitJobs.publish_to(:rspec_queue, TestJob) }
44
+ }
45
+ RabbitJobs::Publisher.purge_queue(:rspec_queue, :rspec_queue2, :rspec_queue3).should == 1000
46
+ puts time
47
+ end
39
48
  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.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-03-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: amqp
16
- requirement: &70362685237960 !ruby/object:Gem::Requirement
16
+ requirement: &70149529326900 !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: *70362685237960
24
+ version_requirements: *70149529326900
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: bunny
27
- requirement: &70362685237460 !ruby/object:Gem::Requirement
27
+ requirement: &70149529326400 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0.7'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70362685237460
35
+ version_requirements: *70149529326400
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &70362685237080 !ruby/object:Gem::Requirement
38
+ requirement: &70149529326020 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70362685237080
46
+ version_requirements: *70149529326020
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rufus-scheduler
49
- requirement: &70362685236540 !ruby/object:Gem::Requirement
49
+ requirement: &70149529325480 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '2.0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *70362685236540
57
+ version_requirements: *70149529325480
58
58
  description: Background jobs on RabbitMQ
59
59
  email:
60
60
  - lazureykis@gmail.com