brightbox-warren 0.7 → 0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +1 -0
- data/lib/warren/adapters/bunny_adapter.rb +92 -88
- data/lib/warren/connection.rb +2 -2
- data/lib/warren/queue.rb +64 -62
- data/lib/warren.rb +3 -3
- data/readme.rdoc +10 -0
- data/warren.gemspec +1 -1
- metadata +1 -1
data/CHANGELOG
CHANGED
@@ -1,97 +1,101 @@
|
|
1
1
|
require "rubygems"
|
2
2
|
require "bunny"
|
3
3
|
|
4
|
-
|
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
|
4
|
+
module Warren
|
5
|
+
class Queue
|
6
|
+
class BunnyAdapter < Queue
|
27
7
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
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)
|
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
|
44
18
|
|
45
|
-
|
46
|
-
queue
|
47
|
-
|
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
|
48
29
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
return if msg == :queue_empty
|
66
|
-
block.call(Warren::MessageFilter.unpack(msg))
|
67
|
-
end
|
68
|
-
end
|
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)
|
69
46
|
|
70
|
-
|
47
|
+
do_connect(queue_name, blk) do |queue|
|
48
|
+
queue.publish msg.to_s
|
49
|
+
end
|
71
50
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
}
|
95
|
-
end
|
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
|
96
73
|
|
97
|
-
|
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(connection_details)
|
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
|
+
def self.connection_details
|
92
|
+
{
|
93
|
+
:user => self.connection.options[:user],
|
94
|
+
:pass => self.connection.options[:pass],
|
95
|
+
:vhost => self.connection.options[:vhost]
|
96
|
+
}
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
data/lib/warren/connection.rb
CHANGED
@@ -17,9 +17,9 @@ module Warren
|
|
17
17
|
#
|
18
18
|
def initialize params = nil
|
19
19
|
if params.nil? || !params.is_a?(Hash)
|
20
|
-
file ||= WARREN_ROOT
|
20
|
+
file ||= "#{WARREN_ROOT}/config/warren.yml"
|
21
21
|
raise InvalidConnectionDetails, "Config file not found: #{file}" unless File.exists?(file)
|
22
|
-
opts = YAML.
|
22
|
+
opts = YAML.load_file(file)
|
23
23
|
end
|
24
24
|
opts ||= params
|
25
25
|
|
data/lib/warren/queue.rb
CHANGED
@@ -1,69 +1,71 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
module Warren
|
2
|
+
class Queue
|
3
|
+
@@connection = nil
|
4
|
+
@@adapter = nil
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
#
|
7
|
+
# Raised if no connection has been defined yet.
|
8
|
+
#
|
9
|
+
NoConnectionDetails = Class.new(Exception)
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
#
|
16
|
-
# Raised if an adapter isn't set
|
17
|
-
#
|
18
|
-
NoAdapterSet = Class.new(Exception)
|
11
|
+
#
|
12
|
+
# Raised if a block is expected by the method but none is given.
|
13
|
+
#
|
14
|
+
NoBlockGiven = Class.new(Exception)
|
19
15
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
@@connection = (conn.is_a?(Warren::Connection) ? conn : Warren::Connection.new(conn) )
|
25
|
-
end
|
26
|
-
|
27
|
-
#
|
28
|
-
# Returns the current connection details
|
29
|
-
#
|
30
|
-
def self.connection
|
31
|
-
@@connection ||= Warren::Connection.new
|
32
|
-
end
|
16
|
+
#
|
17
|
+
# Raised if an adapter isn't set
|
18
|
+
#
|
19
|
+
NoAdapterSet = Class.new(Exception)
|
33
20
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
#
|
42
|
-
# Sets the adapter manually
|
43
|
-
#
|
44
|
-
def self.adapter= klass
|
45
|
-
@@adapter = klass
|
46
|
-
end
|
21
|
+
#
|
22
|
+
# Sets the current connection
|
23
|
+
#
|
24
|
+
def self.connection= conn
|
25
|
+
@@connection = (conn.is_a?(Warren::Connection) ? conn : Warren::Connection.new(conn) )
|
26
|
+
end
|
47
27
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
28
|
+
#
|
29
|
+
# Returns the current connection details
|
30
|
+
#
|
31
|
+
def self.connection
|
32
|
+
@@connection ||= Warren::Connection.new
|
33
|
+
end
|
54
34
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
35
|
+
#
|
36
|
+
# Sets the adapter when this class is subclassed.
|
37
|
+
#
|
38
|
+
def self.inherited klass
|
39
|
+
@@adapter = klass
|
40
|
+
end
|
41
|
+
|
42
|
+
#
|
43
|
+
# Sets the adapter manually
|
44
|
+
#
|
45
|
+
def self.adapter= klass
|
46
|
+
@@adapter = klass
|
47
|
+
end
|
48
|
+
|
49
|
+
#
|
50
|
+
# Returns the current adapter or raises NoAdapterSet exception
|
51
|
+
#
|
52
|
+
def self.adapter
|
53
|
+
@@adapter || raise(NoAdapterSet)
|
54
|
+
end
|
68
55
|
|
69
|
-
|
56
|
+
#
|
57
|
+
# Publishes the message to the queue
|
58
|
+
#
|
59
|
+
def self.publish *args, &blk
|
60
|
+
self.adapter.publish(*args, &blk)
|
61
|
+
end
|
62
|
+
|
63
|
+
#
|
64
|
+
# Sends the subscribe message to the adapter class
|
65
|
+
#
|
66
|
+
def self.subscribe *args, &blk
|
67
|
+
self.adapter.subscribe(*args, &blk)
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
data/lib/warren.rb
CHANGED
@@ -14,10 +14,10 @@ module Warren
|
|
14
14
|
end
|
15
15
|
|
16
16
|
WARREN_ENV = (defined?(RAILS_ENV) ? RAILS_ENV : "development") unless defined?(WARREN_ENV)
|
17
|
-
WARREN_ROOT = File.dirname($0)
|
18
|
-
WARREN_LIB_ROOT = File.expand_path(File.
|
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
19
|
|
20
20
|
# Require everything in the lib folder
|
21
|
-
Dir["#{WARREN_LIB_ROOT}/
|
21
|
+
Dir["#{WARREN_LIB_ROOT}/warren/*.rb"].each do |file|
|
22
22
|
require file
|
23
23
|
end
|
data/readme.rdoc
CHANGED
@@ -21,6 +21,16 @@ Start by looking at examples/ to see how to use it, and then lib/warren/adapters
|
|
21
21
|
|
22
22
|
# See examples/ for more
|
23
23
|
|
24
|
+
== Rails
|
25
|
+
|
26
|
+
Add this to your environment.rb
|
27
|
+
|
28
|
+
config.gem "brightbox-warren", :lib => "warren", :version => ">= 0.8"
|
29
|
+
|
30
|
+
And then in an initializer file (or bottom of environment.rb) require the adapter you want to use (for rabbitmq I suggest bunny - amqp uses eventmachine and was giving me issues under passenger.) And then any filters you want to use.
|
31
|
+
|
32
|
+
require "warren/adapters/bunny_adapter"
|
33
|
+
|
24
34
|
== License
|
25
35
|
|
26
36
|
Licensed under the MIT license. See LICENSE for more details.
|
data/warren.gemspec
CHANGED