schoefmax-warren 0.8.8
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +13 -0
- data/LICENSE +11 -0
- data/Manifest +32 -0
- data/Rakefile +23 -0
- data/examples/authed/receiver.rb +25 -0
- data/examples/authed/secret.rb +1 -0
- data/examples/authed/sender.rb +40 -0
- data/examples/simple/amqp_mass_sender.rb +21 -0
- data/examples/simple/amqp_receiver.rb +18 -0
- data/examples/simple/amqp_sender.rb +36 -0
- data/examples/simple/bunny_receiver.rb +18 -0
- data/examples/simple/bunny_sender.rb +36 -0
- data/lib/warren.rb +23 -0
- data/lib/warren/adapters/amqp_adapter.rb +87 -0
- data/lib/warren/adapters/bunny_adapter.rb +93 -0
- data/lib/warren/adapters/dummy_adapter.rb +12 -0
- data/lib/warren/adapters/test_adapter.rb +27 -0
- data/lib/warren/connection.rb +140 -0
- data/lib/warren/filters/shared_secret.rb +70 -0
- data/lib/warren/filters/yaml.rb +20 -0
- data/lib/warren/message_filter.rb +76 -0
- data/lib/warren/queue.rb +71 -0
- data/readme.rdoc +46 -0
- data/spec/spec.opts +3 -0
- data/spec/spec_helper.rb +12 -0
- data/spec/warren/adapters/test_adapter_spec.rb +29 -0
- data/spec/warren/connection_spec.rb +122 -0
- data/spec/warren/queue_spec.rb +73 -0
- data/spec/warren/warren_spec.rb +9 -0
- data/tasks/rdoc.rake +7 -0
- data/tasks/rspec.rake +24 -0
- data/warren.gemspec +33 -0
- metadata +111 -0
data/CHANGELOG
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
v0.8.8. Bumping for github
|
2
|
+
v0.8.7. Added a TestAdapter
|
3
|
+
v0.8.6. Also added dummy adapter to gemspec
|
4
|
+
v0.8.5. Added dummy adapter for testing purposes
|
5
|
+
v0.8.4. Fix typo in 0.8.3
|
6
|
+
v0.8.3. Can now override WARREN_ENV from the command line.
|
7
|
+
v0.8.2. Added nice error message when no details for current env
|
8
|
+
v0.8.1. Fixed bug in passing params in
|
9
|
+
v0.8. Gem works with rails
|
10
|
+
v0.7. Added adapters
|
11
|
+
v0.6. Added config file
|
12
|
+
v0.5. Added filters
|
13
|
+
v0.3. Initial release
|
data/LICENSE
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
Released under the MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2008 Brightbox Systems Ltd
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
6
|
+
|
7
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
8
|
+
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
10
|
+
|
11
|
+
See http://www.brightbox.co.uk/ for contact details.
|
data/Manifest
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
CHANGELOG
|
2
|
+
examples/authed/receiver.rb
|
3
|
+
examples/authed/secret.rb
|
4
|
+
examples/authed/sender.rb
|
5
|
+
examples/simple/amqp_mass_sender.rb
|
6
|
+
examples/simple/amqp_receiver.rb
|
7
|
+
examples/simple/amqp_sender.rb
|
8
|
+
examples/simple/bunny_receiver.rb
|
9
|
+
examples/simple/bunny_sender.rb
|
10
|
+
lib/warren/adapters/amqp_adapter.rb
|
11
|
+
lib/warren/adapters/bunny_adapter.rb
|
12
|
+
lib/warren/adapters/dummy_adapter.rb
|
13
|
+
lib/warren/adapters/test_adapter.rb
|
14
|
+
lib/warren/connection.rb
|
15
|
+
lib/warren/filters/shared_secret.rb
|
16
|
+
lib/warren/filters/yaml.rb
|
17
|
+
lib/warren/message_filter.rb
|
18
|
+
lib/warren/queue.rb
|
19
|
+
lib/warren.rb
|
20
|
+
LICENSE
|
21
|
+
Manifest
|
22
|
+
Rakefile
|
23
|
+
readme.rdoc
|
24
|
+
spec/spec.opts
|
25
|
+
spec/spec_helper.rb
|
26
|
+
spec/warren/adapters/test_adapter_spec.rb
|
27
|
+
spec/warren/connection_spec.rb
|
28
|
+
spec/warren/queue_spec.rb
|
29
|
+
spec/warren/warren_spec.rb
|
30
|
+
tasks/rdoc.rake
|
31
|
+
tasks/rspec.rake
|
32
|
+
warren.gemspec
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
# Load in external rakefiles
|
5
|
+
Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each do | rake_file |
|
6
|
+
load rake_file
|
7
|
+
end
|
8
|
+
|
9
|
+
# Gem stuff
|
10
|
+
require 'echoe'
|
11
|
+
Echoe.new('warren') do | gem |
|
12
|
+
gem.author = ["Caius Durling", "David Smalley"]
|
13
|
+
gem.email = 'support@brightbox.co.uk'
|
14
|
+
gem.summary = 'Library for pushing messages onto and off RabbitMQ queues'
|
15
|
+
gem.url = 'http://github.com/brightbox/warren'
|
16
|
+
gem.dependencies = [["amqp", '>= 0.6.0']]
|
17
|
+
end
|
18
|
+
|
19
|
+
desc "Generates the manifest and the gemspec"
|
20
|
+
task :build => [:manifest, :build_gemspec] do
|
21
|
+
puts "Built!"
|
22
|
+
end
|
23
|
+
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + "/../../lib/warren")
|
3
|
+
require File.expand_path(File.dirname(__FILE__) + "/../../lib/warren/adapters/amqp_adapter")
|
4
|
+
require File.expand_path(File.dirname(__FILE__) + "/../../lib/warren/filters/shared_secret")
|
5
|
+
|
6
|
+
Signal.trap("INT") { AMQP.stop { EM.stop } }
|
7
|
+
Signal.trap("TERM") { AMQP.stop { EM.stop } }
|
8
|
+
|
9
|
+
# Listen to the main queue
|
10
|
+
q = "main"
|
11
|
+
puts "Listening to the #{q} queue."
|
12
|
+
|
13
|
+
# Setup the connection directly this time
|
14
|
+
Warren::Queue.connection = {"development" => {:user => "caius", :pass => "caius", :vhost => "/"}}
|
15
|
+
|
16
|
+
# Set the secret key
|
17
|
+
require File.expand_path(File.dirname(__FILE__) + "/secret")
|
18
|
+
Warren::MessageFilter::SharedSecret.key = SUPER_SECRET_KEY
|
19
|
+
# And add the filter
|
20
|
+
Warren::MessageFilter << Warren::MessageFilter::SharedSecret
|
21
|
+
|
22
|
+
# And attach a block for new messages to fire
|
23
|
+
Warren::Queue.subscribe(q) do |msg|
|
24
|
+
p [Time.now, msg]
|
25
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
SUPER_SECRET_KEY = "This is my super secret string"
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + "/../../lib/warren")
|
3
|
+
require File.expand_path(File.dirname(__FILE__) + "/../../lib/warren/adapters/amqp_adapter")
|
4
|
+
require File.expand_path(File.dirname(__FILE__) + "/../../lib/warren/filters/shared_secret")
|
5
|
+
|
6
|
+
Signal.trap("INT") { exit! }
|
7
|
+
Signal.trap("TERM") { exit! }
|
8
|
+
|
9
|
+
# Setup our own connection before generating the queue object
|
10
|
+
conn = Warren::Connection.new("development" => {
|
11
|
+
:user => "caius",
|
12
|
+
:pass => "caius",
|
13
|
+
:vhost => "/",
|
14
|
+
:default_queue => "main"
|
15
|
+
})
|
16
|
+
# Set the connection for the queue
|
17
|
+
Warren::Queue.connection = conn
|
18
|
+
# Generate some data to send
|
19
|
+
data = {
|
20
|
+
:people => [
|
21
|
+
:fred => {
|
22
|
+
:age => 25,
|
23
|
+
:location => "Leeds"
|
24
|
+
},
|
25
|
+
:george => {
|
26
|
+
:age => 32,
|
27
|
+
:location => "London"
|
28
|
+
}
|
29
|
+
]
|
30
|
+
}
|
31
|
+
|
32
|
+
# Set the secret key
|
33
|
+
require File.expand_path(File.dirname(__FILE__) + "/secret")
|
34
|
+
Warren::MessageFilter::SharedSecret.key = SUPER_SECRET_KEY
|
35
|
+
|
36
|
+
# And add the filter
|
37
|
+
Warren::MessageFilter << Warren::MessageFilter::SharedSecret
|
38
|
+
|
39
|
+
# Push a message onto the queue
|
40
|
+
p Warren::Queue.publish(:default, data )
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + "/../lib/warren")
|
3
|
+
|
4
|
+
Signal.trap("INT") { exit! }
|
5
|
+
Signal.trap("TERM") { exit! }
|
6
|
+
|
7
|
+
# Setup our own connection before generating the queue object
|
8
|
+
conn = Warren::Connection.new({
|
9
|
+
:user => "caius",
|
10
|
+
:pass => "caius",
|
11
|
+
:vhost => "/",
|
12
|
+
:default_queue => "main"
|
13
|
+
})
|
14
|
+
# Set the connection for the queue
|
15
|
+
Warren::Queue.connection = conn
|
16
|
+
|
17
|
+
1000.times do | i |
|
18
|
+
puts i
|
19
|
+
sleep 0.1
|
20
|
+
Warren::Queue.publish(:default, "Message no #{i}") { puts "sent ##{i}" }
|
21
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + "/../../lib/warren")
|
3
|
+
# Next line is the only one you need to change to use a different adapter
|
4
|
+
require File.expand_path(File.dirname(__FILE__) + "/../../lib/warren/adapters/amqp_adapter")
|
5
|
+
|
6
|
+
Signal.trap("INT") { AMQP.stop { EM.stop } }
|
7
|
+
Signal.trap("TERM") { AMQP.stop { EM.stop } }
|
8
|
+
|
9
|
+
# Listen to the main queue
|
10
|
+
q = "main"
|
11
|
+
puts "Listening to the #{q} queue."
|
12
|
+
|
13
|
+
Warren::Queue.connection = {"development" => {:user => "caius", :pass => "caius", :vhost => "/"}}
|
14
|
+
|
15
|
+
# And attach a block for new messages to fire
|
16
|
+
Warren::Queue.subscribe(q) do |msg|
|
17
|
+
p [Time.now, msg]
|
18
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + "/../../lib/warren")
|
3
|
+
# Next line is the only one you need to change to use a different adapter
|
4
|
+
require File.expand_path(File.dirname(__FILE__) + "/../../lib/warren/adapters/amqp_adapter")
|
5
|
+
|
6
|
+
Signal.trap("INT") { exit! }
|
7
|
+
Signal.trap("TERM") { exit! }
|
8
|
+
|
9
|
+
# Setup our own connection before generating the queue object
|
10
|
+
conn = Warren::Connection.new("development" => {
|
11
|
+
:user => "caius",
|
12
|
+
:pass => "caius",
|
13
|
+
:vhost => "/",
|
14
|
+
:default_queue => "main"
|
15
|
+
})
|
16
|
+
# Set the connection for the queue
|
17
|
+
Warren::Queue.connection = conn
|
18
|
+
# Generate some data to send
|
19
|
+
data = {
|
20
|
+
:people => [
|
21
|
+
:fred => {
|
22
|
+
:age => 25,
|
23
|
+
:location => "Leeds"
|
24
|
+
},
|
25
|
+
:george => {
|
26
|
+
:age => 32,
|
27
|
+
:location => "London"
|
28
|
+
}
|
29
|
+
]
|
30
|
+
}
|
31
|
+
|
32
|
+
# Push a message onto the queue
|
33
|
+
p Warren::Queue.publish(:default, data )
|
34
|
+
|
35
|
+
# And then push a message onto the queue, returning "foo"
|
36
|
+
# p Warren::Queue.publish(:default, data) { "foo" }
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + "/../../lib/warren")
|
3
|
+
# Next line is the only one you need to change to use a different adapter
|
4
|
+
require File.expand_path(File.dirname(__FILE__) + "/../../lib/warren/adapters/bunny_adapter")
|
5
|
+
|
6
|
+
Signal.trap("INT") { exit! }
|
7
|
+
Signal.trap("TERM") { exit! }
|
8
|
+
|
9
|
+
# Listen to the main queue
|
10
|
+
q = "main"
|
11
|
+
puts "Listening to the #{q} queue."
|
12
|
+
|
13
|
+
Warren::Queue.connection = {"development" => {:user => "caius", :pass => "caius", :vhost => "/"}}
|
14
|
+
|
15
|
+
# And attach a block for new messages to fire
|
16
|
+
Warren::Queue.subscribe(q) do |msg|
|
17
|
+
p [Time.now, msg]
|
18
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + "/../../lib/warren")
|
3
|
+
# Next line is the only one you need to change to use a different adapter
|
4
|
+
require File.expand_path(File.dirname(__FILE__) + "/../../lib/warren/adapters/bunny_adapter")
|
5
|
+
|
6
|
+
Signal.trap("INT") { exit! }
|
7
|
+
Signal.trap("TERM") { exit! }
|
8
|
+
|
9
|
+
# Setup our own connection before generating the queue object
|
10
|
+
conn = Warren::Connection.new("development" => {
|
11
|
+
:user => "caius",
|
12
|
+
:pass => "caius",
|
13
|
+
:vhost => "/",
|
14
|
+
:default_queue => "main"
|
15
|
+
})
|
16
|
+
# Set the connection for the queue
|
17
|
+
Warren::Queue.connection = conn
|
18
|
+
# Generate some data to send
|
19
|
+
data = {
|
20
|
+
:people => [
|
21
|
+
:fred => {
|
22
|
+
:age => 25,
|
23
|
+
:location => "Leeds"
|
24
|
+
},
|
25
|
+
:george => {
|
26
|
+
:age => 32,
|
27
|
+
:location => "London"
|
28
|
+
}
|
29
|
+
]
|
30
|
+
}
|
31
|
+
|
32
|
+
# Push a message onto the queue
|
33
|
+
p Warren::Queue.publish(:default, data )
|
34
|
+
|
35
|
+
# And then push a message onto the queue, returning "foo"
|
36
|
+
# p Warren::Queue.publish(:default, data) { "foo" }
|
data/lib/warren.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require "yaml"
|
2
|
+
|
3
|
+
#
|
4
|
+
# Library for pushing messages onto RabbitMQ queues,
|
5
|
+
# and receiving them at the other end.
|
6
|
+
#
|
7
|
+
# It handles authentication + filtering messages with custom
|
8
|
+
# classes if needed.
|
9
|
+
#
|
10
|
+
# Start with Warren::Queue for details and see also
|
11
|
+
# examples/
|
12
|
+
#
|
13
|
+
module Warren
|
14
|
+
end
|
15
|
+
|
16
|
+
WARREN_ENV = ENV['WARREN_ENV'] || ((defined?(RAILS_ENV) ? RAILS_ENV : "development")) unless defined?(WARREN_ENV)
|
17
|
+
WARREN_ROOT = (defined?(RAILS_ROOT) ? RAILS_ROOT : File.dirname($0)) unless defined?(WARREN_ROOT)
|
18
|
+
WARREN_LIB_ROOT = File.expand_path(File.dirname(__FILE__))
|
19
|
+
|
20
|
+
# Require everything in the lib folder
|
21
|
+
Dir["#{WARREN_LIB_ROOT}/warren/*.rb"].each do |file|
|
22
|
+
require file
|
23
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "mq"
|
3
|
+
|
4
|
+
class Warren::Queue::AMQPAdapter < Warren::Queue
|
5
|
+
|
6
|
+
#
|
7
|
+
# Checks the connection details are correct for this adapter
|
8
|
+
#
|
9
|
+
def self.check_connection_details opts
|
10
|
+
# Check they've passed in the stuff without a default on it
|
11
|
+
unless opts.has_key?(:user) && opts.has_key?(:pass) && opts.has_key?(:vhost)
|
12
|
+
raise Warren::Connection::InvalidConnectionDetails, "Missing a username, password or vhost."
|
13
|
+
end
|
14
|
+
true
|
15
|
+
end
|
16
|
+
|
17
|
+
#
|
18
|
+
# Returns the default queue name or returns InvalidConnectionDetails
|
19
|
+
# if no default queue is defined
|
20
|
+
#
|
21
|
+
def self.queue_name
|
22
|
+
unless self.connection.options.has_key?(:default_queue)
|
23
|
+
raise Warren::Connection::InvalidConnectionDetails, "Missing a default queue name."
|
24
|
+
end
|
25
|
+
self.connection.options[:default_queue]
|
26
|
+
end
|
27
|
+
|
28
|
+
#
|
29
|
+
# Sends a message to a queue. If successfully sent it returns
|
30
|
+
# true, unless callback block is passed (see below)
|
31
|
+
#
|
32
|
+
# Warren::Queue.publish(:queue_name, {:foo => "name"})
|
33
|
+
#
|
34
|
+
# Can also pass a block which is fired after the message
|
35
|
+
# is sent. If a block is passed, then the return value of the block
|
36
|
+
# is returned from this method.
|
37
|
+
#
|
38
|
+
# Warren::Queue.publish(:queue_name, {:foo => "name"}) { puts "foo" }
|
39
|
+
#
|
40
|
+
def self.publish queue_name, payload, &blk
|
41
|
+
queue_name = self.queue_name if queue_name == :default
|
42
|
+
# Create a message object if it isn't one already
|
43
|
+
msg = Warren::MessageFilter.pack(payload)
|
44
|
+
|
45
|
+
do_connect(true, blk) do
|
46
|
+
queue = MQ::Queue.new(MQ.new, queue_name)
|
47
|
+
queue.publish msg.to_s
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
#
|
53
|
+
# Subscribes to a queue and runs the block
|
54
|
+
# for each message received
|
55
|
+
#
|
56
|
+
# Warren::Queue.subscribe("example") {|msg| puts msg }
|
57
|
+
#
|
58
|
+
# Expects a block and raises NoBlockGiven if no block is given.
|
59
|
+
#
|
60
|
+
def self.subscribe queue_name, &block
|
61
|
+
raise NoBlockGiven unless block_given?
|
62
|
+
queue_name = self.queue_name if queue_name == :default
|
63
|
+
# todo: check if its a valid queue?
|
64
|
+
do_connect(false) do
|
65
|
+
queue = MQ::Queue.new(MQ.new, queue_name)
|
66
|
+
queue.subscribe do |msg|
|
67
|
+
msg = Warren::MessageFilter.unpack(msg)
|
68
|
+
block.call(msg)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
#
|
76
|
+
# Connects and does the stuff its told to!
|
77
|
+
#
|
78
|
+
def self.do_connect should_stop = true, callback = nil, &block
|
79
|
+
AMQP.start(self.connection.options) do
|
80
|
+
block.call
|
81
|
+
AMQP.stop { EM.stop_event_loop } if should_stop
|
82
|
+
end
|
83
|
+
# Returns the block return value or true
|
84
|
+
callback.nil? ? true : callback.call
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "bunny"
|
3
|
+
|
4
|
+
module Warren
|
5
|
+
class Queue
|
6
|
+
class BunnyAdapter < Queue
|
7
|
+
|
8
|
+
#
|
9
|
+
# Checks the connection details are correct for this adapter
|
10
|
+
#
|
11
|
+
def self.check_connection_details opts
|
12
|
+
# Check they've passed in the stuff without a default on it
|
13
|
+
unless opts.has_key?(:user) && opts.has_key?(:pass) && opts.has_key?(:vhost)
|
14
|
+
raise Warren::Connection::InvalidConnectionDetails, "Missing a username, password or vhost."
|
15
|
+
end
|
16
|
+
true
|
17
|
+
end
|
18
|
+
|
19
|
+
#
|
20
|
+
# Returns the default queue name or returns InvalidConnectionDetails
|
21
|
+
# if no default queue is defined
|
22
|
+
#
|
23
|
+
def self.queue_name
|
24
|
+
unless self.connection.options.has_key?(:default_queue)
|
25
|
+
raise Warren::Connection::InvalidConnectionDetails, "Missing a default queue name."
|
26
|
+
end
|
27
|
+
self.connection.options[:default_queue]
|
28
|
+
end
|
29
|
+
|
30
|
+
#
|
31
|
+
# Sends a message to a queue. If successfully sent it returns
|
32
|
+
# true, unless callback block is passed (see below)
|
33
|
+
#
|
34
|
+
# Warren::Queue.publish(:queue_name, {:foo => "name"})
|
35
|
+
#
|
36
|
+
# Can also pass a block which is fired after the message
|
37
|
+
# is sent. If a block is passed, then the return value of the block
|
38
|
+
# is returned from this method.
|
39
|
+
#
|
40
|
+
# Warren::Queue.publish(:queue_name, {:foo => "name"}) { puts "foo" }
|
41
|
+
#
|
42
|
+
def self.publish queue_name, payload, &blk
|
43
|
+
queue_name = self.queue_name if queue_name == :default
|
44
|
+
# Create a message object if it isn't one already
|
45
|
+
msg = Warren::MessageFilter.pack(payload)
|
46
|
+
|
47
|
+
do_connect(queue_name, blk) do |queue|
|
48
|
+
queue.publish msg.to_s
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
#
|
54
|
+
# Subscribes to a queue and runs the block
|
55
|
+
# for each message received
|
56
|
+
#
|
57
|
+
# Warren::Queue.subscribe("example") {|msg| puts msg }
|
58
|
+
#
|
59
|
+
# Expects a block and raises NoBlockGiven if no block is given.
|
60
|
+
#
|
61
|
+
def self.subscribe queue_name, &block
|
62
|
+
raise NoBlockGiven unless block_given?
|
63
|
+
queue_name = self.queue_name if queue_name == :default
|
64
|
+
# todo: check if its a valid queue?
|
65
|
+
do_connect(queue_name) do |queue|
|
66
|
+
queue.subscribe do |msg|
|
67
|
+
block.call(Warren::MessageFilter.unpack(msg))
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
#
|
75
|
+
# Connects and does the stuff its told to!
|
76
|
+
#
|
77
|
+
def self.do_connect queue_name, callback = nil, &block
|
78
|
+
# Open a connection
|
79
|
+
b = Bunny.new(self.connection.options)
|
80
|
+
b.start
|
81
|
+
# Create the queue
|
82
|
+
q = b.queue(queue_name)
|
83
|
+
# Run the code on the queue
|
84
|
+
block.call(q)
|
85
|
+
# And stop
|
86
|
+
b.stop
|
87
|
+
# Returns the block return value or true
|
88
|
+
callback.nil? ? true : callback.call
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|