beetle 0.2.9.11 → 0.2.12

Sign up to get free protection for your applications and to get access to all the features.
data/beetle.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "beetle"
3
- s.version = "0.2.9.11"
3
+ s.version = "0.2.12"
4
4
  s.required_rubygems_version = ">= 1.3.1"
5
5
  s.authors = ["Stefan Kaes", "Pascal Friederich", "Ali Jelveh", "Sebastian Roebke"]
6
6
  s.date = Time.now.strftime('%Y-%m-%d')
@@ -40,7 +40,9 @@ def first_redis_configuration_client_pid
40
40
  end
41
41
 
42
42
  def system_notification_log_path
43
- tmp_path + "/system_notifications.log"
43
+ log_path = tmp_path + "/system_notifications.log"
44
+ `touch #{log_path}` unless File.exists?(log_path)
45
+ log_path
44
46
  end
45
47
 
46
48
  def tmp_path
@@ -185,5 +185,5 @@ glueoutputbuf yes
185
185
  # WARNING: object sharing is experimental, don't enable this feature
186
186
  # in production before of Redis 1.0-stable. Still please try this feature in
187
187
  # your development environment so that we can test it better.
188
- shareobjects no
189
- shareobjectspoolsize 1024
188
+ # shareobjects no
189
+ # shareobjectspoolsize 1024
data/lib/beetle/base.rb CHANGED
@@ -53,7 +53,7 @@ module Beetle
53
53
  queues[name] ||=
54
54
  begin
55
55
  opts = @client.queues[name]
56
- error("You are trying to bind a queue #{name} which is not configured!") unless opts
56
+ raise UnknownQueue.new("You are trying to bind a queue #{name} which is not configured!") unless opts
57
57
  logger.debug("Beetle: binding queue #{name} with internal name #{opts[:amqp_name]} on server #{@server}")
58
58
  queue_name = opts[:amqp_name]
59
59
  creation_options = opts.slice(*QUEUE_CREATION_KEYS)
data/lib/beetle/client.rb CHANGED
@@ -66,7 +66,7 @@ module Beetle
66
66
  def register_exchange(name, options={})
67
67
  name = name.to_s
68
68
  raise ConfigurationError.new("exchange #{name} already configured") if exchanges.include?(name)
69
- exchanges[name] = options.symbolize_keys.merge(:type => :topic, :durable => true)
69
+ exchanges[name] = options.symbolize_keys.merge(:type => :topic, :durable => true, :queues => [])
70
70
  end
71
71
 
72
72
  # register a durable, non passive, non auto_deleted queue with the given _name_ and an _options_ hash:
@@ -101,7 +101,7 @@ module Beetle
101
101
  key = (opts[:key] || name).to_s
102
102
  (bindings[name] ||= []) << {:exchange => exchange, :key => key}
103
103
  register_exchange(exchange) unless exchanges.include?(exchange)
104
- queues = (exchanges[exchange][:queues] ||= [])
104
+ queues = exchanges[exchange][:queues]
105
105
  queues << name unless queues.include?(name)
106
106
  end
107
107
 
@@ -189,12 +189,17 @@ module Beetle
189
189
  publisher.purge(queue_name)
190
190
  end
191
191
 
192
- # start listening to a list of messages (default to all registered messages).
192
+ # start listening to all registered queues. Calls #listen_queues internally
193
193
  # runs the given block before entering the eventmachine loop.
194
- def listen(messages=self.messages.keys, &block)
195
- messages = messages.map(&:to_s)
196
- messages.each{|m| raise UnknownMessage.new("unknown message #{m}") unless self.messages.include?(m)}
197
- subscriber.listen(messages, &block)
194
+ def listen(_deprecated_messages=nil, &block)
195
+ raise Error.new("Beetle::Client#listen no longer works with arguments. Please use #listen_queues(['queue1', 'queue2']) instead") if _deprecated_messages
196
+ listen_queues(&block)
197
+ end
198
+
199
+ # start listening to a list of queues (default to all registered queues).
200
+ # runs the given block before entering the eventmachine loop.
201
+ def listen_queues(queues=self.queues.keys, &block)
202
+ subscriber.listen_queues(queues, &block)
198
203
  end
199
204
 
200
205
  # stops the eventmachine loop
@@ -207,19 +212,20 @@ module Beetle
207
212
  publisher.stop
208
213
  end
209
214
 
210
- # traces messages without consuming them. useful for debugging message flow.
211
- def trace(messages=self.messages.keys, &block)
212
- queues.each do |name, opts|
215
+ # traces queues without consuming them. useful for debugging message flow.
216
+ def trace(queue_names=self.queues.keys, &block)
217
+ queues_to_trace = self.queues.slice(*queue_names)
218
+ queues_to_trace.each do |name, opts|
213
219
  opts.merge! :durable => false, :auto_delete => true, :amqp_name => queue_name_for_tracing(opts[:amqp_name])
214
220
  end
215
- register_handler(queues.keys) do |msg|
221
+ register_handler(queue_names) do |msg|
216
222
  puts "-----===== new message =====-----"
217
223
  puts "SERVER: #{msg.server}"
218
224
  puts "HEADER: #{msg.header.inspect}"
219
225
  puts "MSGID: #{msg.msg_id}"
220
226
  puts "DATA: #{msg.data}"
221
227
  end
222
- listen(messages, &block)
228
+ listen_queues(queue_names, &block)
223
229
  end
224
230
 
225
231
  # evaluate the ruby files matching the given +glob+ pattern in the context of the client instance.
@@ -14,26 +14,25 @@ module Beetle
14
14
  @mqs = {}
15
15
  end
16
16
 
17
- # the client calls this method to subscribe to all queues on all servers which have
18
- # handlers registered for the given list of messages. this method does the following
19
- # things:
17
+ # the client calls this method to subscribe to a list of queues.
18
+ # this method does the following things:
20
19
  #
21
- # * creates all exchanges which have been registered for the given messages
22
- # * creates and binds queues which have been registered for the exchanges
20
+ # * creates all exchanges which have been registered for the given queues
21
+ # * creates and binds each listed queue queues
23
22
  # * subscribes the handlers for all these queues
24
23
  #
25
24
  # yields before entering the eventmachine loop (if a block was given)
26
- def listen(messages) #:nodoc:
25
+ def listen_queues(queues) #:nodoc:
27
26
  EM.run do
28
- exchanges = exchanges_for_messages(messages)
27
+ exchanges = exchanges_for_queues(queues)
29
28
  create_exchanges(exchanges)
30
- queues = queues_for_exchanges(exchanges)
31
29
  bind_queues(queues)
32
30
  subscribe_queues(queues)
33
31
  yield if block_given?
34
32
  end
35
33
  end
36
34
 
35
+
37
36
  # closes all AMQP connections and stops the eventmachine loop
38
37
  def stop! #:nodoc:
39
38
  if @amqp_connections.empty?
@@ -53,6 +52,10 @@ module Beetle
53
52
 
54
53
  private
55
54
 
55
+ def exchanges_for_queues(queues)
56
+ @client.bindings.slice(*queues).map{|_, opts| opts.map{|opt| opt[:exchange]}}.flatten.uniq
57
+ end
58
+
56
59
  def exchanges_for_messages(messages)
57
60
  @client.messages.slice(*messages).map{|_, opts| opts[:exchange]}.uniq
58
61
  end
data/lib/beetle.rb CHANGED
@@ -2,6 +2,7 @@ $:.unshift(File.expand_path('..', __FILE__))
2
2
  require 'bunny-ext'
3
3
  require 'uuid4r'
4
4
  require 'active_support'
5
+ require 'active_support/core_ext'
5
6
  require 'redis'
6
7
 
7
8
  module Beetle
@@ -40,7 +40,7 @@ module Beetle
40
40
  test "registering an exchange should store it in the configuration with symbolized option keys and force a topic queue and durability" do
41
41
  opts = {"durable" => false, "type" => "fanout"}
42
42
  @client.register_exchange("some_exchange", opts)
43
- assert_equal({:durable => true, :type => :topic}, @client.exchanges["some_exchange"])
43
+ assert_equal({:durable => true, :type => :topic, :queues => []}, @client.exchanges["some_exchange"])
44
44
  end
45
45
 
46
46
  test "should convert exchange name to a string when registering an exchange" do
@@ -53,6 +53,12 @@ module Beetle
53
53
  assert_raises(ConfigurationError){ @client.register_exchange("some_exchange") }
54
54
  end
55
55
 
56
+ test "registering an exchange should initialize the list of queues bound to it" do
57
+ @client.register_exchange("some_exchange")
58
+ assert_equal [], @client.exchanges["some_exchange"][:queues]
59
+ assert_raises(ConfigurationError){ @client.register_exchange("some_exchange") }
60
+ end
61
+
56
62
  test "registering a queue should automatically register the corresponding exchange if it doesn't exist yet" do
57
63
  @client.register_queue("some_queue", "durable" => false, "exchange" => "some_exchange")
58
64
  assert @client.exchanges.include?("some_exchange")
@@ -119,14 +125,14 @@ module Beetle
119
125
  test "registering a message should register a corresponding exchange if it hasn't been registered yet" do
120
126
  opts = { "exchange" => "some_exchange" }
121
127
  @client.register_message("some_message", opts)
122
- assert_equal({:durable => true, :type => :topic}, @client.exchanges["some_exchange"])
128
+ assert_equal({:durable => true, :type => :topic, :queues => []}, @client.exchanges["some_exchange"])
123
129
  end
124
130
 
125
131
  test "registering a message should not fail if the exchange has already been registered" do
126
132
  opts = { "exchange" => "some_exchange" }
127
133
  @client.register_exchange("some_exchange")
128
134
  @client.register_message("some_message", opts)
129
- assert_equal({:durable => true, :type => :topic}, @client.exchanges["some_exchange"])
135
+ assert_equal({:durable => true, :type => :topic, :queues => []}, @client.exchanges["some_exchange"])
130
136
  end
131
137
 
132
138
  test "should convert message name to a string when registering a message" do
@@ -255,18 +261,26 @@ module Beetle
255
261
 
256
262
  test "should delegate listening to the subscriber instance" do
257
263
  client = Client.new
258
- client.register_queue(:a)
259
- client.register_message(:a)
260
- client.register_queue(:b)
261
- client.register_message(:b)
262
- client.send(:subscriber).expects(:listen).with(["a", "b"]).yields
263
- x = 0
264
- client.listen([:a, "b"]) { x = 5 }
265
- assert_equal 5, x
264
+ client.register_queue("b_queue")
265
+ client.register_queue("a_queue")
266
+ client.send(:subscriber).expects(:listen_queues).with {|value| value.include?("a_queue") && value.include?("b_queue")}.yields
267
+ client.listen
268
+ end
269
+
270
+ test "trying to listen to a message is no longer supported and should raise an exception" do
271
+ assert_raises(Error) { Client.new.listen([:a])}
272
+ end
273
+
274
+ test "should delegate listening to queues to the subscriber instance" do
275
+ client = Client.new
276
+ client.register_queue(:test)
277
+ client.send(:subscriber).expects(:listen_queues).with([:test]).yields
278
+ client.listen_queues([:test])
266
279
  end
267
280
 
268
- test "trying to listen to an unknown message should raise an exception" do
269
- assert_raises(UnknownMessage) { Client.new.listen([:a])}
281
+ test "trying to listen to an unknown queue should raise an exception" do
282
+ client = Client.new
283
+ assert_raises(UnknownQueue) { Client.new.listen_queues([:foobar])}
270
284
  end
271
285
 
272
286
  test "should delegate stop_listening to the subscriber instance" do
@@ -308,7 +322,7 @@ module Beetle
308
322
  client.register_queue("test")
309
323
  sub = client.send(:subscriber)
310
324
  sub.expects(:register_handler).with(client.queues.keys, {}, nil).yields(stub_everything("message"))
311
- sub.expects(:listen)
325
+ sub.expects(:listen_queues)
312
326
  client.stubs(:puts)
313
327
  client.trace
314
328
  test_queue_opts = client.queues["test"]
@@ -318,5 +332,16 @@ module Beetle
318
332
  assert !test_queue_opts[:durable]
319
333
  end
320
334
 
335
+ test "limiting tracing to some queues" do
336
+ client = Client.new
337
+ client.register_queue("test")
338
+ client.register_queue("irrelevant")
339
+ sub = client.send(:subscriber)
340
+ sub.expects(:register_handler).with(["test"], {}, nil).yields(stub_everything("message"))
341
+ sub.expects(:listen_queues).with(["test"])
342
+ client.stubs(:puts)
343
+ client.trace(["test"])
344
+ end
345
+
321
346
  end
322
347
  end
@@ -290,7 +290,7 @@ module Beetle
290
290
 
291
291
  test "accessing a given exchange should create it using the config. further access should return the created exchange" do
292
292
  m = mock("Bunny")
293
- m.expects(:exchange).with("some_exchange", :type => :topic, :durable => true).returns(42)
293
+ m.expects(:exchange).with("some_exchange", :type => :topic, :durable => true, :queues => []).returns(42)
294
294
  @client.register_exchange("some_exchange", :type => :topic, :durable => true)
295
295
  @pub.expects(:bunny).returns(m)
296
296
  ex = @pub.send(:exchange, "some_exchange")
@@ -249,14 +249,16 @@ module Beetle
249
249
  assert_raises(Error){ @sub.send(:subscribe, "some_queue") }
250
250
  end
251
251
 
252
- test "listening should use eventmachine. create exchanges. bind queues. install subscribers. and yield." do
253
- @client.register_queue(:a)
254
- @client.register_message(:a)
252
+ test "listeninging on queues should use eventmachine. create exchanges. bind queues. install subscribers. and yield." do
253
+ @client.register_exchange(:an_exchange)
254
+ @client.register_queue(:a_queue, :exchange => :an_exchange)
255
+ @client.register_message(:a_message, :key => "foo", :exchange => :an_exchange)
256
+
255
257
  EM.expects(:run).yields
256
- @sub.expects(:create_exchanges).with(["a"])
257
- @sub.expects(:bind_queues).with(["a"])
258
- @sub.expects(:subscribe_queues).with(["a"])
259
- @sub.listen(["a"]) {}
258
+ @sub.expects(:create_exchanges).with(["an_exchange"])
259
+ @sub.expects(:bind_queues).with(["a_queue"])
260
+ @sub.expects(:subscribe_queues).with(["a_queue"])
261
+ @sub.listen_queues(["a_queue"]) {}
260
262
  end
261
263
  end
262
264
 
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: beetle
3
3
  version: !ruby/object:Gem::Version
4
- hash: 109
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 9
10
- - 11
11
- version: 0.2.9.11
9
+ - 12
10
+ version: 0.2.12
12
11
  platform: ruby
13
12
  authors:
14
13
  - Stefan Kaes
@@ -19,7 +18,7 @@ autorequire:
19
18
  bindir: bin
20
19
  cert_chain: []
21
20
 
22
- date: 2011-01-27 00:00:00 +01:00
21
+ date: 2011-02-17 00:00:00 +01:00
23
22
  default_executable: beetle
24
23
  dependencies:
25
24
  - !ruby/object:Gem::Dependency