queueing_rabbit 0.3.6 → 0.3.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -60,7 +60,7 @@ module QueueingRabbit
60
60
  info "enqueueing job #{job}"
61
61
 
62
62
  follow_job_requirements(job) do |channel, exchange, _|
63
- conn.enqueue(exchange, payload, options)
63
+ publish_to_exchange(exchange, payload, options)
64
64
  channel.close
65
65
  end
66
66
 
@@ -70,10 +70,17 @@ module QueueingRabbit
70
70
  def publish(bus, payload = nil, options = {})
71
71
  info "publishing to event bus #{bus}"
72
72
 
73
- follow_bus_requirements(bus) do |ch, ex|
74
- conn.enqueue(ex, payload, options)
75
- ch.close
73
+ follow_bus_requirements(bus) do |channel, exchange|
74
+ publish_to_exchange(exchange, payload, options)
75
+ channel.close
76
76
  end
77
+
78
+ true
79
+ end
80
+
81
+ def publish_to_exchange(exchange, payload = nil, options = {})
82
+ conn.publish(exchange, payload, options)
83
+ true
77
84
  end
78
85
 
79
86
  def begin_worker_loop
@@ -114,8 +121,10 @@ module QueueingRabbit
114
121
 
115
122
  def purge_queue(job)
116
123
  connection.open_channel(job.channel_options) do |c, _|
117
- connection.define_queue(c, job.queue_name, job.queue_options).purge
118
- c.close
124
+ connection.define_queue(c, job.queue_name, job.queue_options) do |q|
125
+ q.purge(:nowait => true)
126
+ c.close
127
+ end
119
128
  end
120
129
  true
121
130
  end
@@ -10,6 +10,8 @@ module QueueingRabbit
10
10
  end
11
11
  end
12
12
 
13
+ attr_reader :shared_exchange
14
+
13
15
  def channel(options = {})
14
16
  @channel_options ||= {}
15
17
  @channel_options.update(options)
@@ -43,8 +45,24 @@ module QueueingRabbit
43
45
  @publishing_defaults || {}
44
46
  end
45
47
 
46
- def publish(payload, options = {})
47
- QueueingRabbit.publish(self, payload, publishing_defaults.merge(options))
48
+ def demand_batch_publishing!
49
+ QueueingRabbit.follow_bus_requirements(self) do |_, exchange|
50
+ @shared_exchange = exchange
51
+ end
52
+ end
53
+
54
+ def batch_publishing?
55
+ !!@shared_exchange
56
+ end
57
+
58
+ def publish(payload, options = {}, method = :publish)
59
+ args = [payload, publishing_defaults.merge(options)]
60
+
61
+ if batch_publishing?
62
+ QueueingRabbit.publish_to_exchange(@shared_exchange, *args)
63
+ else
64
+ QueueingRabbit.send(method, self, *args)
65
+ end
48
66
  end
49
67
 
50
68
  protected
@@ -63,8 +63,14 @@ module QueueingRabbit
63
63
  {:routing_key => queue_name.to_s}.merge(@publishing_defaults)
64
64
  end
65
65
 
66
+ def demand_batch_publishing!
67
+ QueueingRabbit.follow_job_requirements(self) do |_, exchange, _|
68
+ @shared_exchange = exchange
69
+ end
70
+ end
71
+
66
72
  def enqueue(payload, options = {})
67
- QueueingRabbit.enqueue(self, payload, publishing_defaults.merge(options))
73
+ publish(payload, options, :enqueue)
68
74
  end
69
75
 
70
76
  end
@@ -1,3 +1,3 @@
1
1
  module QueueingRabbit
2
- VERSION = "0.3.6"
2
+ VERSION = "0.3.7"
3
3
  end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Asynchronous batch publishing" do
4
+ include_context "Auto-disconnect"
5
+ include_context "Evented spec"
6
+ include_context "StringIO logger"
7
+
8
+ before(:all) { QueueingRabbit.client = QueueingRabbit::Client::AMQP }
9
+ after(:all) { QueueingRabbit.client = QueueingRabbit.default_client }
10
+
11
+ let(:line) { "Hello, world!" }
12
+ let(:connection) { QueueingRabbit.connection }
13
+ let(:job) {
14
+ Class.new(QueueingRabbit::AbstractJob) do
15
+ queue 'asynchronous_batch_publishing'
16
+ end
17
+ }
18
+
19
+ it 'produces 100 jobs in a batch' do
20
+ em {
21
+ @queue_size = 0
22
+
23
+ def request_queue_size
24
+ connection.open_channel do |c, _|
25
+ connection.define_queue(c, job.queue_name, job.queue_options).status do |s, _|
26
+ @queue_size = s
27
+ end
28
+ end
29
+ end
30
+
31
+ delayed(0.5) {
32
+ job.demand_batch_publishing!
33
+ }
34
+
35
+ delayed(1.0) {
36
+ 100.times { job.enqueue(line) }
37
+ }
38
+
39
+ delayed(3.0) {
40
+ request_queue_size
41
+ }
42
+
43
+ delayed(3.5) {
44
+ QueueingRabbit.purge_queue(job)
45
+ }
46
+
47
+ done(4.0) {
48
+ expect(@queue_size).to eq(100)
49
+ }
50
+ }
51
+ end
52
+
53
+ end
54
+
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Synchronous batch publishing" do
4
+ include_context "Auto-disconnect"
5
+ include_context "Evented spec"
6
+ include_context "StringIO logger"
7
+
8
+ before(:all) { QueueingRabbit.client = QueueingRabbit::Client::Bunny }
9
+ after(:all) { QueueingRabbit.client = QueueingRabbit.default_client }
10
+
11
+ let(:line) { "Hello, world!" }
12
+ let(:connection) { QueueingRabbit.connection }
13
+ let(:job) {
14
+ Class.new(QueueingRabbit::AbstractJob) do
15
+ queue 'synchronous_batch_publishing'
16
+ end
17
+ }
18
+
19
+ it 'produces 100 jobs in a batch' do
20
+ em {
21
+ delayed(0.5) {
22
+ job.demand_batch_publishing!
23
+ }
24
+
25
+ delayed(1.0) {
26
+ 100.times { job.enqueue(line) }
27
+ }
28
+
29
+ done(3.0) {
30
+ expect(QueueingRabbit.queue_size(job)).to eq(100)
31
+ QueueingRabbit.purge_queue(job)
32
+ }
33
+ }
34
+ end
35
+
36
+ end
37
+
@@ -21,5 +21,21 @@ describe QueueingRabbit::AbstractBus do
21
21
  its(:exchange_name) { should == 'test_exchange' }
22
22
  its(:exchange_options) { should include(:durable => false) }
23
23
  its(:publishing_defaults) { should include(:routing_key => 'test_queue') }
24
+
25
+ describe '.demand_batch_publishing!' do
26
+
27
+ let(:exchange) { mock }
28
+
29
+ it 'assigns a shared exchange instance to a job class' do
30
+ QueueingRabbit.should_receive(:follow_bus_requirements).
31
+ with(subject).
32
+ and_yield(nil, exchange)
33
+ subject.demand_batch_publishing!
34
+ expect(subject.shared_exchange).to eq(exchange)
35
+ expect(subject.batch_publishing?).to be_true
36
+ expect(QueueingRabbit::AbstractBus.shared_exchange).to be_nil
37
+ end
38
+
39
+ end
24
40
 
25
41
  end
@@ -45,6 +45,22 @@ describe QueueingRabbit::AbstractJob do
45
45
  its(:queue_size) { should == size }
46
46
  end
47
47
 
48
+ describe '.demand_batch_publishing!' do
49
+
50
+ let(:exchange) { mock }
51
+
52
+ it 'assigns a shared exchange instance to a job class' do
53
+ QueueingRabbit.should_receive(:follow_job_requirements).
54
+ with(subject).
55
+ and_yield(nil, exchange, nil)
56
+ subject.demand_batch_publishing!
57
+ expect(subject.shared_exchange).to eq(exchange)
58
+ expect(subject.batch_publishing?).to be_true
59
+ expect(QueueingRabbit::AbstractJob.shared_exchange).to be_nil
60
+ end
61
+
62
+ end
63
+
48
64
  describe '.enqueue' do
49
65
  let(:payload) { mock }
50
66
  let(:options) { {:persistent => true} }
@@ -52,7 +52,7 @@ describe QueueingRabbit do
52
52
  subject.should_receive(:follow_job_requirements).
53
53
  with(job).
54
54
  and_yield(channel, exchange, nil)
55
- connection.should_receive(:enqueue).with(exchange, payload, options)
55
+ connection.should_receive(:publish).with(exchange, payload, options)
56
56
  channel.should_receive(:close)
57
57
  end
58
58
 
@@ -66,6 +66,40 @@ describe QueueingRabbit do
66
66
  end
67
67
  end
68
68
 
69
+ describe '.publish' do
70
+
71
+ let(:bus) { QueueingRabbit::AbstractBus }
72
+ let(:payload) { mock(:to_s => 'payload') }
73
+ let(:options) { mock }
74
+ let(:exchange) { mock }
75
+ let(:channel) { mock }
76
+
77
+ it 'publishes payload to a given bus with options' do
78
+ subject.instance_variable_set(:@connection, connection)
79
+ subject.should_receive(:follow_bus_requirements).
80
+ with(bus).
81
+ and_yield(channel, exchange)
82
+ connection.should_receive(:publish).with(exchange, payload, options)
83
+ channel.should_receive(:close)
84
+ subject.publish(bus, payload, options).should be_true
85
+ end
86
+
87
+ end
88
+
89
+ describe '.publish_to_exchange' do
90
+
91
+ let(:exchange) { mock }
92
+ let(:payload) { mock }
93
+ let(:options) { mock }
94
+
95
+ it 'publishes payload to a given exchange with options' do
96
+ subject.instance_variable_set(:@connection, connection)
97
+ connection.should_receive(:publish).with(exchange, payload, options)
98
+ subject.publish_to_exchange(exchange, payload, options).should be_true
99
+ end
100
+
101
+ end
102
+
69
103
  describe '.begin_worker_loop' do
70
104
 
71
105
  before do
@@ -164,8 +198,9 @@ describe QueueingRabbit do
164
198
  connection.should_receive(:open_channel).
165
199
  with(channel_options).and_yield(channel, nil)
166
200
  connection.should_receive(:define_queue).
167
- with(channel, queue_name, queue_options).and_return(queue)
168
- queue.should_receive(:purge).and_return(true)
201
+ with(channel, queue_name, queue_options).
202
+ and_yield(queue)
203
+ queue.should_receive(:purge).with(:nowait => true).and_return(true)
169
204
  channel.should_receive(:close)
170
205
  end
171
206
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: queueing_rabbit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.6
4
+ version: 0.3.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-01-09 00:00:00.000000000 Z
12
+ date: 2014-01-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: amqp
@@ -123,6 +123,7 @@ files:
123
123
  - lib/queueing_rabbit/worker.rb
124
124
  - lib/tasks/queueing_rabbit.rake
125
125
  - queueing_rabbit.gemspec
126
+ - spec/integration/asynchronous_batch_publishing_spec.rb
126
127
  - spec/integration/asynchronous_publishing_and_consuming_spec.rb
127
128
  - spec/integration/asynchronous_publishing_and_consuming_with_retries_spec.rb
128
129
  - spec/integration/binary_synchronous_publishing_via_bus_and_asynchronous_consuming_via_job_spec.rb
@@ -134,6 +135,7 @@ files:
134
135
  - spec/integration/json_job_synchronous_publishing_and_consuming_spec.rb
135
136
  - spec/integration/json_synchronous_publishing_via_bus_and_asynchronous_consuming_via_job_spec.rb
136
137
  - spec/integration/persistent_asynchronous_publishing_and_consuming_spec.rb
138
+ - spec/integration/synchronous_batch_publishing_spec.rb
137
139
  - spec/integration/synchronous_publishing_and_asynchronous_consuming_spec.rb
138
140
  - spec/integration/synchronous_publishing_and_consuming_spec.rb
139
141
  - spec/integration/synchronous_publishing_and_threaded_consuming_spec.rb
@@ -172,7 +174,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
172
174
  version: '0'
173
175
  segments:
174
176
  - 0
175
- hash: -1747676710658043461
177
+ hash: 656088827131633906
176
178
  required_rubygems_version: !ruby/object:Gem::Requirement
177
179
  none: false
178
180
  requirements:
@@ -181,7 +183,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
181
183
  version: '0'
182
184
  segments:
183
185
  - 0
184
- hash: -1747676710658043461
186
+ hash: 656088827131633906
185
187
  requirements: []
186
188
  rubyforge_project:
187
189
  rubygems_version: 1.8.23
@@ -189,6 +191,7 @@ signing_key:
189
191
  specification_version: 3
190
192
  summary: QueueingRabbit is an AMQP-based queueing system
191
193
  test_files:
194
+ - spec/integration/asynchronous_batch_publishing_spec.rb
192
195
  - spec/integration/asynchronous_publishing_and_consuming_spec.rb
193
196
  - spec/integration/asynchronous_publishing_and_consuming_with_retries_spec.rb
194
197
  - spec/integration/binary_synchronous_publishing_via_bus_and_asynchronous_consuming_via_job_spec.rb
@@ -200,6 +203,7 @@ test_files:
200
203
  - spec/integration/json_job_synchronous_publishing_and_consuming_spec.rb
201
204
  - spec/integration/json_synchronous_publishing_via_bus_and_asynchronous_consuming_via_job_spec.rb
202
205
  - spec/integration/persistent_asynchronous_publishing_and_consuming_spec.rb
206
+ - spec/integration/synchronous_batch_publishing_spec.rb
203
207
  - spec/integration/synchronous_publishing_and_asynchronous_consuming_spec.rb
204
208
  - spec/integration/synchronous_publishing_and_consuming_spec.rb
205
209
  - spec/integration/synchronous_publishing_and_threaded_consuming_spec.rb