warren 0.8.9
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +14 -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/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 +89 -0
- data/lib/warren.rb +23 -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 +91 -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 +113 -0
data/CHANGELOG
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
v0.8.9. Fixing a StackTooDeep bug.
|
2
|
+
v0.8.8. Bumping for github
|
3
|
+
v0.8.7. Added a TestAdapter
|
4
|
+
v0.8.6. Also added dummy adapter to gemspec
|
5
|
+
v0.8.5. Added dummy adapter for testing purposes
|
6
|
+
v0.8.4. Fix typo in 0.8.3
|
7
|
+
v0.8.3. Can now override WARREN_ENV from the command line.
|
8
|
+
v0.8.2. Added nice error message when no details for current env
|
9
|
+
v0.8.1. Fixed bug in passing params in
|
10
|
+
v0.8. Gem works with rails
|
11
|
+
v0.7. Added adapters
|
12
|
+
v0.6. Added config file
|
13
|
+
v0.5. Added filters
|
14
|
+
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" }
|
@@ -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
|
+
msg = queue.pop
|
67
|
+
return if msg == :queue_empty
|
68
|
+
block.call(Warren::MessageFilter.unpack(msg))
|
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
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
|
3
|
+
class DummyAdapter < Warren::Queue
|
4
|
+
def self.publish queue_name, payload, &blk
|
5
|
+
puts "publishing #{payload.inspect} to #{queue_name}"
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.subscribe queue_name, &block
|
9
|
+
puts "subscribing to #{queue_name}"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
|
3
|
+
class TestAdapter < Warren::Queue
|
4
|
+
ConnectionFailed = Class.new(Exception)
|
5
|
+
#
|
6
|
+
def self.publish queue_name, payload, &blk
|
7
|
+
raise TestAdapter::ConnectionFailed if fail?
|
8
|
+
true
|
9
|
+
end
|
10
|
+
|
11
|
+
#
|
12
|
+
# def self.subscribe queue_name, &block
|
13
|
+
# puts "subscribing to #{queue_name}"
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def self.fail?
|
20
|
+
File.exists?(file_name) && (File.read(file_name, 4) == "FAIL")
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.file_name
|
24
|
+
"#{WARREN_ROOT}/tmp/warren.txt"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
@@ -0,0 +1,140 @@
|
|
1
|
+
require "yaml"
|
2
|
+
module Warren
|
3
|
+
class Connection
|
4
|
+
|
5
|
+
attr_reader :options
|
6
|
+
|
7
|
+
#
|
8
|
+
# Creates a new connection by reading the options from
|
9
|
+
# WARREN_ROOT/config/warren.yml or specify the file to read as an
|
10
|
+
# argument or by passing a hash of connection details in.
|
11
|
+
#
|
12
|
+
# Warren::Connection.new # reads WARREN_ROOT/config/warren.yml
|
13
|
+
# Warren::Connection.new("file.yml") # reads file.yml
|
14
|
+
# Warren::Connection.new({"foo" => "bar"}) # uses the hash
|
15
|
+
#
|
16
|
+
# Reads WARREN_ENV out of the yaml'd hash (just like ActiveRecord)
|
17
|
+
# "development" by default (and RAILS_ENV if running under rails)
|
18
|
+
#
|
19
|
+
# Raises InvalidConnectionDetails if no params are found for the current
|
20
|
+
# environment.
|
21
|
+
#
|
22
|
+
# todo: fail nicely if the env isn't defined
|
23
|
+
#
|
24
|
+
def initialize params = nil
|
25
|
+
get_connection_details(params)
|
26
|
+
# Make the details publically accessible
|
27
|
+
@options = @opts
|
28
|
+
end
|
29
|
+
|
30
|
+
#
|
31
|
+
# Raised if connection details are missing or invalid
|
32
|
+
# Check the error message for more details
|
33
|
+
#
|
34
|
+
InvalidConnectionDetails = Class.new(Exception)
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
#
|
39
|
+
# Short version: This is the brain of this class and sets everything up.
|
40
|
+
#
|
41
|
+
# Long version: Works out where the connection details need to come from
|
42
|
+
# (params or yml) and parses them from there. Then figures out if the details
|
43
|
+
# are there for the current env and raises a nice error if there aren't any
|
44
|
+
# detail for the current env. Then we symblize all the keys and get the adapter
|
45
|
+
# to check its happy with the connection details we have.
|
46
|
+
#
|
47
|
+
def get_connection_details params
|
48
|
+
case params
|
49
|
+
when NilClass
|
50
|
+
# Read from config/warren.yml
|
51
|
+
read_config_file
|
52
|
+
|
53
|
+
when String
|
54
|
+
# See if it exists as a file
|
55
|
+
parse_config_file(params)
|
56
|
+
|
57
|
+
when Hash
|
58
|
+
# Use it as-is
|
59
|
+
@opts = params
|
60
|
+
|
61
|
+
else
|
62
|
+
# See if it responds to :read
|
63
|
+
if respond_to?(:read)
|
64
|
+
# Parse it as yaml
|
65
|
+
parse_config(params)
|
66
|
+
else
|
67
|
+
# Have no idea what to do with it
|
68
|
+
raise InvalidConnectionDetails, "Don't know what to do with the params passed. Please pass a hash, filename or nothing."
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
# Make sure the hash keys are symbols
|
73
|
+
@opts = symbolize_keys(@opts)
|
74
|
+
# Parse out the current environment
|
75
|
+
parse_environment
|
76
|
+
# Call the adapter to figure out if the details are alright
|
77
|
+
check_connection_details
|
78
|
+
end
|
79
|
+
|
80
|
+
#
|
81
|
+
# Reads in either config/warren.yml or the passed argument as a YAML file
|
82
|
+
#
|
83
|
+
def read_config_file filename=nil
|
84
|
+
filename ||= "#{WARREN_ROOT}/config/warren.yml"
|
85
|
+
parse_config_file(filename)
|
86
|
+
end
|
87
|
+
|
88
|
+
#
|
89
|
+
# Parses the config file into a hash from yaml.
|
90
|
+
#
|
91
|
+
def parse_config_file filename
|
92
|
+
if File.exists?(filename)
|
93
|
+
@opts = YAML.load_file(filename)
|
94
|
+
else
|
95
|
+
raise InvalidConnectionDetails, "File not found: '#{filename}'"
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
#
|
100
|
+
# Parses the object using YAML#load
|
101
|
+
#
|
102
|
+
def parse_config obj
|
103
|
+
@opts = YAML.load(obj)
|
104
|
+
end
|
105
|
+
|
106
|
+
#
|
107
|
+
# Modifies the hash into just the details for the
|
108
|
+
# current environment, or raises InvalidConnectionDetails
|
109
|
+
#
|
110
|
+
def parse_environment
|
111
|
+
# keys are symbolified already
|
112
|
+
unless @opts.has_key?(WARREN_ENV.to_sym)
|
113
|
+
raise InvalidConnectionDetails, "No details for current environment '#{WARREN_ENV}'"
|
114
|
+
end
|
115
|
+
@opts = @opts[WARREN_ENV.to_sym]
|
116
|
+
end
|
117
|
+
|
118
|
+
#
|
119
|
+
# Changes all keys into symbols
|
120
|
+
#
|
121
|
+
def symbolize_keys(hash)
|
122
|
+
hash.each do |key, value|
|
123
|
+
hash.delete(key)
|
124
|
+
# Make it recursive
|
125
|
+
hash[key.to_sym] = (value.is_a?(Hash) ? symbolize_keys(value) : value)
|
126
|
+
end
|
127
|
+
hash
|
128
|
+
end
|
129
|
+
|
130
|
+
#
|
131
|
+
# Calls the adapter to check the connection details
|
132
|
+
# Returns true or raises InvalidConnectionDetails
|
133
|
+
#
|
134
|
+
def check_connection_details
|
135
|
+
return true unless Warren::Queue.adapter.respond_to?(:check_connection_details)
|
136
|
+
Warren::Queue.adapter.send(:check_connection_details, @opts)
|
137
|
+
end
|
138
|
+
|
139
|
+
end
|
140
|
+
end
|