queueing_rabbit 0.3.7 → 0.3.8

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml CHANGED
@@ -4,9 +4,9 @@ before_install:
4
4
  - gem --version
5
5
  rvm:
6
6
  - "1.8.7"
7
- - "1.9.2"
8
7
  - "1.9.3"
9
8
  - "2.0.0"
9
+ - "2.1.0"
10
10
  services:
11
11
  - rabbitmq
12
12
  script: bundle exec rake spec
@@ -141,6 +141,22 @@ module QueueingRabbit
141
141
  raise NotImplementedError
142
142
  end
143
143
 
144
+ def purge_queue(queue)
145
+ queue.purge do
146
+ yield if block_given?
147
+ end
148
+ end
149
+
150
+ def next_tick(&block)
151
+ EM.next_tick(&block)
152
+ end
153
+
154
+ def begin_worker_loop
155
+ EM.run do
156
+ yield if block_given?
157
+ end
158
+ end
159
+
144
160
  private
145
161
 
146
162
  def setup_callbacks
@@ -87,6 +87,11 @@ module QueueingRabbit
87
87
  end
88
88
  end
89
89
 
90
+ def purge_queue(queue)
91
+ queue.purge
92
+ yield if block_given?
93
+ end
94
+
90
95
  def close
91
96
  @connection.close
92
97
  yield if block_given?
@@ -97,11 +102,23 @@ module QueueingRabbit
97
102
  @connection.open?
98
103
  end
99
104
 
105
+ def next_tick(&block)
106
+ if @continue_worker_loop
107
+ @actions_queue << block
108
+ else
109
+ block.call
110
+ end
111
+ end
112
+
100
113
  def begin_worker_loop
101
- yield
114
+ yield if block_given?
115
+ @actions_queue = []
102
116
  @continue_worker_loop = true
103
117
  # We may need to add signal handling here
104
- sleep 2 while @continue_worker_loop
118
+ while @continue_worker_loop
119
+ @actions_queue.take_while { |block| block.call || true }
120
+ sleep 1
121
+ end
105
122
  end
106
123
 
107
124
  private
@@ -1,3 +1,3 @@
1
1
  module QueueingRabbit
2
- VERSION = "0.3.7"
2
+ VERSION = "0.3.8"
3
3
  end
@@ -58,13 +58,15 @@ module QueueingRabbit
58
58
  end
59
59
 
60
60
  def stop
61
- return unless QueueingRabbit.connection
61
+ connection = QueueingRabbit.connection
62
62
 
63
- QueueingRabbit.connection.close {
64
- info "gracefully shutting down the worker #{self}"
65
- remove_pidfile
66
- QueueingRabbit.trigger_event(:consuming_done)
67
- }
63
+ connection.next_tick do
64
+ connection.close do
65
+ info "gracefully shutting down the worker #{self}"
66
+ remove_pidfile
67
+ QueueingRabbit.trigger_event(:consuming_done)
68
+ end
69
+ end
68
70
  end
69
71
 
70
72
  private
@@ -122,8 +122,7 @@ module QueueingRabbit
122
122
  def purge_queue(job)
123
123
  connection.open_channel(job.channel_options) do |c, _|
124
124
  connection.define_queue(c, job.queue_name, job.queue_options) do |q|
125
- q.purge(:nowait => true)
126
- c.close
125
+ connection.purge_queue(q) { c.close }
127
126
  end
128
127
  end
129
128
  true
@@ -238,5 +238,26 @@ describe QueueingRabbit::Client::AMQP do
238
238
  client.enqueue(exchange, payload, options)
239
239
  end
240
240
  end
241
+
242
+ describe '#next_tick' do
243
+
244
+ it 'schedules given action to be performed at the next EM tick' do
245
+ EM.should_receive(:next_tick)
246
+ client.next_tick {}
247
+ end
248
+
249
+ end
250
+
251
+ describe '#purge_queue' do
252
+
253
+ let(:queue) { mock }
254
+
255
+ it 'purges the queue and fires the provided callback when done' do
256
+ queue.should_receive(:purge).and_yield
257
+ expect { |b| client.purge_queue(queue, &b) }.to yield_control
258
+ end
259
+
260
+ end
261
+
241
262
  end
242
263
  end
@@ -144,5 +144,39 @@ describe QueueingRabbit::Client::Bunny do
144
144
 
145
145
  end
146
146
 
147
+ describe '#purge_queue' do
148
+
149
+ let(:queue) { mock }
150
+
151
+ it 'purges the queue and fires the provided callback when done' do
152
+ queue.should_receive(:purge)
153
+ expect { |b| client.purge_queue(queue, &b) }.to yield_control
154
+ end
155
+
156
+ end
157
+
158
+ describe '#next_tick' do
159
+
160
+ context 'given the worker loop is running' do
161
+
162
+ it 'performs the action on next tick' do
163
+ client.connection.should_receive(:close)
164
+ Thread.new { sleep 1; client.next_tick { client.close } }
165
+ expect { Timeout.timeout(5) { client.begin_worker_loop } }.
166
+ not_to raise_error(Timeout::Error)
167
+ end
168
+
169
+ end
170
+
171
+ context 'given the worker loop is not running' do
172
+
173
+ it 'performs the action immediately' do
174
+ expect(client.next_tick { 42 }).to eq(42)
175
+ end
176
+
177
+ end
178
+
179
+ end
180
+
147
181
  end
148
182
  end
@@ -176,6 +176,7 @@ describe QueueingRabbit::Worker do
176
176
  end
177
177
 
178
178
  it 'closes the connection, removes the pidfile and reports the event' do
179
+ connection.should_receive(:next_tick).and_yield
179
180
  connection.should_receive(:close).and_yield
180
181
  File.stub(:exists?).with(file_name).and_return(true)
181
182
  File.should_receive(:delete).with(file_name)
@@ -200,7 +200,7 @@ describe QueueingRabbit do
200
200
  connection.should_receive(:define_queue).
201
201
  with(channel, queue_name, queue_options).
202
202
  and_yield(queue)
203
- queue.should_receive(:purge).with(:nowait => true).and_return(true)
203
+ connection.should_receive(:purge_queue).and_yield
204
204
  channel.should_receive(:close)
205
205
  end
206
206
 
@@ -208,4 +208,5 @@ describe QueueingRabbit do
208
208
  subject.purge_queue(job).should be_true
209
209
  end
210
210
  end
211
+
211
212
  end
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.7
4
+ version: 0.3.8
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-21 00:00:00.000000000 Z
12
+ date: 2014-01-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: amqp
@@ -174,7 +174,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
174
174
  version: '0'
175
175
  segments:
176
176
  - 0
177
- hash: 656088827131633906
177
+ hash: -1744328126596891438
178
178
  required_rubygems_version: !ruby/object:Gem::Requirement
179
179
  none: false
180
180
  requirements:
@@ -183,7 +183,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
183
183
  version: '0'
184
184
  segments:
185
185
  - 0
186
- hash: 656088827131633906
186
+ hash: -1744328126596891438
187
187
  requirements: []
188
188
  rubyforge_project:
189
189
  rubygems_version: 1.8.23