beetle 0.3.0.rc.12 → 0.3.0.rc.13
Sign up to get free protection for your applications and to get access to all the features.
- data/examples/simple.rb +1 -1
- data/examples/test_publisher.rb +32 -0
- data/lib/beetle/client.rb +33 -12
- data/lib/beetle/version.rb +1 -1
- data/lib/beetle.rb +1 -1
- data/test/beetle/client_test.rb +20 -2
- data/test/beetle/publisher_test.rb +2 -2
- data/test/beetle/subscriber_test.rb +2 -2
- metadata +5 -4
data/examples/simple.rb
CHANGED
@@ -15,7 +15,7 @@ Beetle.config.logger.level = Logger::INFO
|
|
15
15
|
|
16
16
|
# setup client
|
17
17
|
client = Beetle::Client.new
|
18
|
-
client.register_queue(:test)
|
18
|
+
client.register_queue(:test, :arguments => {"x-message-ttl" => 60 * 1000})
|
19
19
|
client.register_message(:test)
|
20
20
|
|
21
21
|
# purge the test queue
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# attempts.rb
|
2
|
+
# this example shows you how to use the exception limiting feature of beetle
|
3
|
+
# it allows you to control the number of retries your handler will go through
|
4
|
+
# with one message before giving up on it
|
5
|
+
#
|
6
|
+
# ! check the examples/README.rdoc for information on starting your redis/rabbit !
|
7
|
+
#
|
8
|
+
# start it with ruby attempts.rb
|
9
|
+
|
10
|
+
require "rubygems"
|
11
|
+
require File.expand_path("../lib/beetle", File.dirname(__FILE__))
|
12
|
+
require "eventmachine"
|
13
|
+
|
14
|
+
# set Beetle log level to info, less noisy than debug
|
15
|
+
Beetle.config.logger.level = Logger::INFO
|
16
|
+
|
17
|
+
# setup client
|
18
|
+
client = Beetle::Client.new
|
19
|
+
client.register_message(:test)
|
20
|
+
|
21
|
+
n = 0
|
22
|
+
EM.run do
|
23
|
+
EM.add_periodic_timer(0.1) do
|
24
|
+
data = (n+=1)
|
25
|
+
client.logger.info "publishing #{data}"
|
26
|
+
client.publish(:test, data)
|
27
|
+
end
|
28
|
+
trap("INT") do
|
29
|
+
client.stop_publishing
|
30
|
+
EM.stop_event_loop
|
31
|
+
end
|
32
|
+
end
|
data/lib/beetle/client.rb
CHANGED
@@ -149,9 +149,10 @@ module Beetle
|
|
149
149
|
|
150
150
|
# this is a convenience method to configure exchanges, queues, messages and handlers
|
151
151
|
# with a common set of options. allows one to call all register methods without the
|
152
|
-
# register_ prefix. returns self.
|
152
|
+
# register_ prefix. returns self. if the passed in block has no parameters, the block
|
153
|
+
# will be evaluated in the context of the client configurator.
|
153
154
|
#
|
154
|
-
# Example:
|
155
|
+
# Example: (block with config argument)
|
155
156
|
# client = Beetle.client.new.configure :exchange => :foobar do |config|
|
156
157
|
# config.queue :q1, :key => "foo"
|
157
158
|
# config.queue :q2, :key => "bar"
|
@@ -160,8 +161,24 @@ module Beetle
|
|
160
161
|
# config.handler :q1 { puts "got foo"}
|
161
162
|
# config.handler :q2 { puts "got bar"}
|
162
163
|
# end
|
163
|
-
|
164
|
-
|
164
|
+
#
|
165
|
+
# Example: (block without config argument)
|
166
|
+
# client = Beetle.client.new.configure :exchange => :foobar do
|
167
|
+
# queue :q1, :key => "foo"
|
168
|
+
# queue :q2, :key => "bar"
|
169
|
+
# message :foo
|
170
|
+
# message :bar
|
171
|
+
# handler :q1 { puts "got foo"}
|
172
|
+
# handler :q2 { puts "got bar"}
|
173
|
+
# end
|
174
|
+
#
|
175
|
+
def configure(options={}, &block)
|
176
|
+
configurator = Configurator.new(self, options)
|
177
|
+
if block.arity == 1
|
178
|
+
yield configurator
|
179
|
+
else
|
180
|
+
configurator.instance_eval &block
|
181
|
+
end
|
165
182
|
self
|
166
183
|
end
|
167
184
|
|
@@ -223,18 +240,22 @@ module Beetle
|
|
223
240
|
end
|
224
241
|
|
225
242
|
# traces queues without consuming them. useful for debugging message flow.
|
226
|
-
def trace(queue_names=self.queues.keys, &block)
|
243
|
+
def trace(queue_names=self.queues.keys, tracer=nil, &block)
|
227
244
|
queues_to_trace = self.queues.slice(*queue_names)
|
228
245
|
queues_to_trace.each do |name, opts|
|
229
246
|
opts.merge! :durable => false, :auto_delete => true, :amqp_name => queue_name_for_tracing(opts[:amqp_name])
|
230
247
|
end
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
248
|
+
tracer ||=
|
249
|
+
lambda do |msg|
|
250
|
+
puts "-----===== new message =====-----"
|
251
|
+
puts "SERVER: #{msg.server}"
|
252
|
+
puts "HEADER: #{msg.header.attributes[:headers].inspect}"
|
253
|
+
puts "EXCHANGE: #{msg.header.method.exchange}"
|
254
|
+
puts "KEY: #{msg.header.method.routing_key}"
|
255
|
+
puts "MSGID: #{msg.msg_id}"
|
256
|
+
puts "DATA: #{msg.data}"
|
257
|
+
end
|
258
|
+
register_handler(queue_names){|msg| tracer.call msg }
|
238
259
|
listen_queues(queue_names, &block)
|
239
260
|
end
|
240
261
|
|
data/lib/beetle/version.rb
CHANGED
data/lib/beetle.rb
CHANGED
@@ -40,7 +40,7 @@ module Beetle
|
|
40
40
|
# AMQP options for exchange creation
|
41
41
|
EXCHANGE_CREATION_KEYS = [:auto_delete, :durable, :internal, :nowait, :passive]
|
42
42
|
# AMQP options for queue creation
|
43
|
-
QUEUE_CREATION_KEYS = [:passive, :durable, :exclusive, :auto_delete, :no_wait]
|
43
|
+
QUEUE_CREATION_KEYS = [:passive, :durable, :exclusive, :auto_delete, :no_wait, :arguments]
|
44
44
|
# AMQP options for queue bindings
|
45
45
|
QUEUE_BINDING_KEYS = [:key, :no_wait]
|
46
46
|
# AMQP options for message publishing
|
data/test/beetle/client_test.rb
CHANGED
@@ -151,6 +151,16 @@ module Beetle
|
|
151
151
|
@client.configure(options) {|config| assert_equal 42, config}
|
152
152
|
end
|
153
153
|
|
154
|
+
test "configure should eval a passed block without arguments in the context of the configurator" do
|
155
|
+
options = {:exchange => :foobar}
|
156
|
+
m = "mock"
|
157
|
+
m.expects(:foo).returns(42)
|
158
|
+
Client::Configurator.expects(:new).with(@client, options).returns(m)
|
159
|
+
value = nil
|
160
|
+
@client.configure(options) { value = foo }
|
161
|
+
assert_equal 42, value
|
162
|
+
end
|
163
|
+
|
154
164
|
test "a configurator should forward all known registration methods to the client" do
|
155
165
|
options = {:foo => :bar}
|
156
166
|
config = Client::Configurator.new(@client, options)
|
@@ -353,7 +363,7 @@ module Beetle
|
|
353
363
|
client = Client.new
|
354
364
|
client.register_queue("test")
|
355
365
|
sub = client.send(:subscriber)
|
356
|
-
sub.expects(:register_handler).with(client.queues.keys, {}, nil).yields(
|
366
|
+
sub.expects(:register_handler).with(client.queues.keys, {}, nil).yields(message_stub_for_tracing)
|
357
367
|
sub.expects(:listen_queues)
|
358
368
|
client.stubs(:puts)
|
359
369
|
client.trace
|
@@ -369,11 +379,19 @@ module Beetle
|
|
369
379
|
client.register_queue("test")
|
370
380
|
client.register_queue("irrelevant")
|
371
381
|
sub = client.send(:subscriber)
|
372
|
-
sub.expects(:register_handler).with(["test"], {}, nil).yields(
|
382
|
+
sub.expects(:register_handler).with(["test"], {}, nil).yields(message_stub_for_tracing)
|
373
383
|
sub.expects(:listen_queues).with(["test"])
|
374
384
|
client.stubs(:puts)
|
375
385
|
client.trace(["test"])
|
376
386
|
end
|
377
387
|
|
388
|
+
def message_stub_for_tracing
|
389
|
+
header_stub = stub_everything("header")
|
390
|
+
header_stub.stubs(:method).returns(stub_everything("method"))
|
391
|
+
header_stub.stubs(:attributes).returns(stub_everything("attributes"))
|
392
|
+
msg_stub = stub_everything("message")
|
393
|
+
msg_stub.stubs(:header).returns(header_stub)
|
394
|
+
msg_stub
|
395
|
+
end
|
378
396
|
end
|
379
397
|
end
|
@@ -224,12 +224,12 @@ module Beetle
|
|
224
224
|
end
|
225
225
|
|
226
226
|
test "binding a queue should create it using the config and bind it to the exchange with the name specified" do
|
227
|
-
@client.register_queue("some_queue", :exchange => "some_exchange", :key => "haha.#")
|
227
|
+
@client.register_queue("some_queue", :exchange => "some_exchange", :key => "haha.#", :arguments => {"foo" => "fighter"})
|
228
228
|
@pub.expects(:exchange).with("some_exchange").returns(:the_exchange)
|
229
229
|
q = mock("queue")
|
230
230
|
q.expects(:bind).with(:the_exchange, {:key => "haha.#"})
|
231
231
|
m = mock("Bunny")
|
232
|
-
m.expects(:queue).with("some_queue", :durable => true, :passive => false, :auto_delete => false, :exclusive => false).returns(q)
|
232
|
+
m.expects(:queue).with("some_queue", :durable => true, :passive => false, :auto_delete => false, :exclusive => false, :arguments => {"foo" => "fighter"}).returns(q)
|
233
233
|
@pub.expects(:bunny).returns(m)
|
234
234
|
|
235
235
|
@pub.send(:queue, "some_queue")
|
@@ -128,12 +128,12 @@ module Beetle
|
|
128
128
|
end
|
129
129
|
|
130
130
|
test "binding a queue should create it using the config and bind it to the exchange with the name specified" do
|
131
|
-
@client.register_queue("some_queue", "durable" => true, "exchange" => "some_exchange", "key" => "haha.#")
|
131
|
+
@client.register_queue("some_queue", "durable" => true, "exchange" => "some_exchange", "key" => "haha.#", "arguments" => {"schmu" => 5})
|
132
132
|
@sub.expects(:exchange).with("some_exchange").returns(:the_exchange)
|
133
133
|
q = mock("queue")
|
134
134
|
q.expects(:bind).with(:the_exchange, {:key => "haha.#"})
|
135
135
|
m = mock("MQ")
|
136
|
-
m.expects(:queue).with("some_queue", :durable => true, :passive => false, :auto_delete => false, :exclusive => false).returns(q)
|
136
|
+
m.expects(:queue).with("some_queue", :durable => true, :passive => false, :auto_delete => false, :exclusive => false, :arguments => {"schmu" => 5}).returns(q)
|
137
137
|
@sub.expects(:channel).returns(m)
|
138
138
|
|
139
139
|
@sub.send(:queue, "some_queue")
|
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: beetle
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 3659109081
|
5
5
|
prerelease: true
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 3
|
9
9
|
- 0
|
10
10
|
- rc
|
11
|
-
-
|
12
|
-
version: 0.3.0.rc.
|
11
|
+
- 13
|
12
|
+
version: 0.3.0.rc.13
|
13
13
|
platform: ruby
|
14
14
|
authors:
|
15
15
|
- Stefan Kaes
|
@@ -21,7 +21,7 @@ autorequire:
|
|
21
21
|
bindir: bin
|
22
22
|
cert_chain: []
|
23
23
|
|
24
|
-
date: 2012-
|
24
|
+
date: 2012-03-06 00:00:00 +01:00
|
25
25
|
default_executable: beetle
|
26
26
|
dependencies:
|
27
27
|
- !ruby/object:Gem::Dependency
|
@@ -284,6 +284,7 @@ files:
|
|
284
284
|
- examples/redundant.rb
|
285
285
|
- examples/rpc.rb
|
286
286
|
- examples/simple.rb
|
287
|
+
- examples/test_publisher.rb
|
287
288
|
- lib/beetle/base.rb
|
288
289
|
- lib/beetle/client.rb
|
289
290
|
- lib/beetle/commands/configuration_client.rb
|