roger_rabbit 0.0.1 → 0.0.2

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/README CHANGED
@@ -1,29 +1,22 @@
1
- require 'rubygems'
2
- require 'test/unit'
3
1
  require 'roger_rabbit'
4
2
 
5
- class TestRogerRabbit < Test::Unit::TestCase
6
3
 
7
- # You should have rabbitmq installed and running.
8
- def test_it
9
- queue_name = "test"
10
- 2.times do
11
- RogerRabbit.publish queue_name, :joe => 'cool'
12
- end
4
+ RogerRabbit.publish "queue-name", :joe => "cool"
13
5
 
14
- count = 0
6
+ RogerRabbit.consume "queue-name" do |msg|
7
+ # msg is { :joe => "cool" }
8
+ end
9
+
10
+ RogerRabbit.rpc_listen("another-queue") do |args|
11
+ args.merge({:true => true})
12
+ end
13
+
14
+ # RPC with result directly returned
15
+ result = RogerRabbit.rpc_message("another-queue", :joe => "cool")
16
+ # result is { :joe => "cool", :true => "true" }
15
17
 
16
- # RogerRabbit.consume will loop forever, so we're
17
- # going to break out of it with a Timeout.
18
- assert_raises(Timeout::Error) do
19
- Timeout.timeout(0.05) do
20
- RogerRabbit.consume queue_name do |args|
21
- assert_equal({ "joe" => 'cool' }, args)
22
- count += 1
23
- end
24
- end
25
- end
26
18
 
27
- assert_equal 2, count, "we should have consumed 2 messages"
28
- end
19
+ # RPC with result returned in a block
20
+ RogerRabbit.rpc_message("another-queue", :joe => "cool") do |result|
21
+ # result is { :joe => "cool", :true => "true" }
29
22
  end
@@ -2,33 +2,84 @@ require 'bunny'
2
2
  require 'json'
3
3
  require 'active_support/hash_with_indifferent_access'
4
4
 
5
+ # WARNING: I don't understand yet how rabbitmq works
6
+ # This probably doesn't work at all.
5
7
  module RogerRabbit
6
8
  def self.bunny
7
- if ! @bunny
8
- @bunny = Bunny.new
9
- @bunny.start
10
- @bunny.qos
9
+ if ! Thread.current[:bunny]
10
+ Thread.current[:bunny] = Bunny.new
11
+ Thread.current[:bunny].start
12
+ Thread.current[:bunny].qos
11
13
  end
12
- @bunny
14
+ Thread.current[:bunny]
13
15
  end
14
16
 
15
17
  def self.bunny_queue queue_name
16
- @queues ||= {}
17
- @queues[queue_name] ||= bunny.queue(queue_name)
18
+ Thread.current[:queues] = {}
19
+ Thread.current[:queues][:queue_name] ||= bunny.queue(queue_name)
20
+ end
21
+
22
+ # WARNING: I don't understand yet how RPC works in
23
+ # rabbitmq. Need to do more research. This probably
24
+ # doesn't really work.
25
+ def self.rpc_listen queue_name, &block
26
+ # Exchange definition
27
+ bunny.exchange('reply', :type => :direct)
28
+ bunny.exchange('out', :type => :direct)
29
+
30
+ bunny.queue('rpc').bind('out')
31
+
32
+ # Publish!
33
+ bunny.queue('rpc').subscribe(:header => true) do |msg|
34
+ result = block.call(deserialize(msg))
35
+ bunny.exchange('reply').publish(serialize(result), :key => msg[:header].reply_to)
36
+ end
37
+ end
38
+
39
+ # WARNING: I don't understand yet how RPC works in
40
+ # rabbitmq. Need to do more research. This probably
41
+ # doesn't really work.
42
+ def self.rpc_message queue_name, hash, &block
43
+ # Exchange definition
44
+ bunny.exchange('reply', :type => :direct)
45
+ bunny.exchange('out', :type => :direct)
46
+
47
+ # Create the temporary queue and bind it
48
+ reply = bunny.queue
49
+ reply.bind('reply', :key => reply.name)
50
+
51
+ # Publish!
52
+ bunny.exchange('out').publish(serialize(hash), :reply_to => reply.name)
53
+
54
+ reply.subscribe do |msg|
55
+ msg = deserialize(msg)
56
+ yield msg if block_given?
57
+ reply.unsubscribe
58
+ return msg
59
+ end
18
60
  end
19
61
 
20
62
  def self.consume queue_name, &block
21
63
  bunny_queue(queue_name).subscribe(:ack => true) do |msg|
22
64
  begin
23
- yield ActiveSupport::HashWithIndifferentAccess.new(JSON.parse(msg[:payload]))
65
+ yield deserialize(msg)
24
66
  rescue JSON::ParserError => e
25
67
  puts e.message
26
68
  end
27
69
  end
28
70
  end
29
71
 
30
- def self.publish queue_name, args
31
- args = args.to_json
32
- bunny_queue(queue_name).publish(args)
72
+ def self.publish queue_name, hash
73
+ bunny_queue(queue_name).publish(serialize(hash))
74
+ end
75
+
76
+ private
77
+
78
+ def self.serialize hash
79
+ hash.to_json
80
+ end
81
+
82
+ def self.deserialize hash
83
+ ActiveSupport::HashWithIndifferentAccess.new(JSON.parse(hash[:payload]))
33
84
  end
34
85
  end
@@ -1,3 +1,3 @@
1
1
  module RogerRabbit
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -2,10 +2,10 @@ require 'rubygems'
2
2
  require 'test/unit'
3
3
  require 'roger_rabbit'
4
4
 
5
+ # You should have rabbitmq installed and running.
5
6
  class TestRogerRabbit < Test::Unit::TestCase
6
7
 
7
- # You should have rabbitmq installed and running.
8
- def test_it
8
+ def test_publish_consume
9
9
  queue_name = "test"
10
10
  2.times do
11
11
  RogerRabbit.publish queue_name, :joe => 'cool'
@@ -27,4 +27,34 @@ class TestRogerRabbit < Test::Unit::TestCase
27
27
 
28
28
  assert_equal 2, count, "we should have consumed 2 messages"
29
29
  end
30
+
31
+ def test_rpc
32
+ queue_name = "rpc"
33
+
34
+ t = Thread.new do
35
+ RogerRabbit.rpc_listen(queue_name) do |args|
36
+ result = {}
37
+ args.each do |key, value|
38
+ result[key] = "#{value}est"
39
+ end
40
+ result
41
+ end
42
+ end
43
+
44
+ count = 0
45
+
46
+ # With block
47
+ RogerRabbit.rpc_message(queue_name, :joe => 'cool') do |result|
48
+ assert_equal({"joe" => "coolest"}, result)
49
+ count += 1
50
+ end
51
+
52
+ # Without block
53
+ result = RogerRabbit.rpc_message(queue_name, :joe => 'cool')
54
+ assert_equal({"joe" => "coolest"}, result)
55
+ count += 1
56
+
57
+ sleep 0.1 # Wait for thread to finish
58
+ assert_equal 2, count
59
+ end
30
60
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: roger_rabbit
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Joe Van Dyk