cf-message-bus 0.0.4 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/cf_message_bus/message_bus.rb +6 -21
- data/lib/cf_message_bus/mock_message_bus.rb +28 -11
- data/lib/cf_message_bus/version.rb +1 -1
- metadata +12 -6
@@ -8,10 +8,11 @@ module CfMessageBus
|
|
8
8
|
class MessageBus
|
9
9
|
def initialize(config)
|
10
10
|
@logger = config[:logger]
|
11
|
-
|
11
|
+
|
12
|
+
@internal_bus = MessageBusFactory.message_bus(
|
13
|
+
config[:servers] || config[:uris] || config[:uri])
|
14
|
+
|
12
15
|
@subscriptions = {}
|
13
|
-
@internal_bus.on_reconnect { start_internal_bus_recovery }
|
14
|
-
@recovery_callback = lambda {}
|
15
16
|
end
|
16
17
|
|
17
18
|
def subscribe(subject, options = {}, &block)
|
@@ -30,10 +31,6 @@ module CfMessageBus
|
|
30
31
|
end
|
31
32
|
end
|
32
33
|
|
33
|
-
def recover(&block)
|
34
|
-
@recovery_callback = block
|
35
|
-
end
|
36
|
-
|
37
34
|
def request(subject, data = nil, options = {}, &block)
|
38
35
|
response_timeout = options.delete(:timeout)
|
39
36
|
result_count = options.delete(:result_count)
|
@@ -50,6 +47,7 @@ module CfMessageBus
|
|
50
47
|
run_handler(block, {timeout: true}, nil, subject, 'timeout')
|
51
48
|
end
|
52
49
|
end
|
50
|
+
|
53
51
|
subscription_id
|
54
52
|
end
|
55
53
|
|
@@ -93,19 +91,6 @@ module CfMessageBus
|
|
93
91
|
end
|
94
92
|
end
|
95
93
|
|
96
|
-
def start_internal_bus_recovery
|
97
|
-
EM.defer do
|
98
|
-
@logger.info("Reconnected to internal_bus.")
|
99
|
-
|
100
|
-
@recovery_callback.call
|
101
|
-
|
102
|
-
@subscriptions.each do |subject, options|
|
103
|
-
@logger.info("Resubscribing to #{subject}")
|
104
|
-
subscribe(subject, options[0], &options[1])
|
105
|
-
end
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
94
|
def subscribe_on_reactor(subject, options = {}, &blk)
|
110
95
|
EM.schedule do
|
111
96
|
internal_bus.subscribe(subject, options) do |msg, inbox|
|
@@ -115,7 +100,7 @@ module CfMessageBus
|
|
115
100
|
end
|
116
101
|
|
117
102
|
def process_message(msg, inbox, &block)
|
118
|
-
payload = JSON.parse(msg
|
103
|
+
payload = JSON.parse(msg)
|
119
104
|
block.yield(payload, inbox)
|
120
105
|
rescue => e
|
121
106
|
@logger.error "exception parsing json: '#{msg}' '#{e.inspect}'"
|
@@ -1,10 +1,14 @@
|
|
1
1
|
module CfMessageBus
|
2
2
|
class MockMessageBus
|
3
|
+
attr_reader :published_messages, :published_synchronous_messages
|
4
|
+
|
3
5
|
def initialize(config = {})
|
4
6
|
@logger = config[:logger]
|
5
7
|
@subscriptions = Hash.new { |hash, key| hash[key] = [] }
|
6
8
|
@requests = {}
|
9
|
+
@synchronous_requests = {}
|
7
10
|
@published_messages = []
|
11
|
+
@published_synchronous_messages = []
|
8
12
|
end
|
9
13
|
|
10
14
|
def subscribe(subject, opts = {}, &blk)
|
@@ -14,7 +18,7 @@ module CfMessageBus
|
|
14
18
|
|
15
19
|
def publish(subject, message = nil, &callback)
|
16
20
|
@subscriptions[subject].each do |subscription|
|
17
|
-
subscription.call(
|
21
|
+
subscription.call(stringify_keys(message))
|
18
22
|
end
|
19
23
|
|
20
24
|
@published_messages.push({subject: subject, message: message, callback: callback})
|
@@ -22,13 +26,15 @@ module CfMessageBus
|
|
22
26
|
callback.call if callback
|
23
27
|
end
|
24
28
|
|
25
|
-
def request(subject, data=nil,
|
29
|
+
def request(subject, data=nil, _={}, &blk)
|
26
30
|
@requests[subject] = blk
|
27
31
|
publish(subject, data)
|
28
32
|
subject
|
29
33
|
end
|
30
34
|
|
31
35
|
def synchronous_request(subject, data=nil, options={})
|
36
|
+
@published_synchronous_messages.push(subject: subject, data: data, options: options)
|
37
|
+
stringify_keys(@synchronous_requests[subject])
|
32
38
|
end
|
33
39
|
|
34
40
|
def unsubscribe(subscription_id)
|
@@ -44,19 +50,19 @@ module CfMessageBus
|
|
44
50
|
true
|
45
51
|
end
|
46
52
|
|
53
|
+
def respond_to_synchronous_request(request_subject, data)
|
54
|
+
@synchronous_requests[request_subject] = data
|
55
|
+
end
|
56
|
+
|
47
57
|
def respond_to_request(request_subject, data)
|
48
58
|
block = @requests.fetch(request_subject) { lambda { |data| nil } }
|
49
|
-
block.call(
|
59
|
+
block.call(stringify_keys(data))
|
50
60
|
end
|
51
61
|
|
52
62
|
def do_recovery
|
53
63
|
@recovery.call if @recovery
|
54
64
|
end
|
55
65
|
|
56
|
-
def published_messages
|
57
|
-
@published_messages
|
58
|
-
end
|
59
|
-
|
60
66
|
def clear_published_messages
|
61
67
|
@published_messages.clear
|
62
68
|
end
|
@@ -66,18 +72,29 @@ module CfMessageBus
|
|
66
72
|
end
|
67
73
|
|
68
74
|
def has_published_with_message?(subject, message)
|
69
|
-
@published_messages.find
|
75
|
+
@published_messages.find do |publication|
|
76
|
+
publication[:subject] == subject &&
|
77
|
+
publication[:message] == message
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def has_requested_synchronous_messages?(subject, data=nil, options={})
|
82
|
+
@published_synchronous_messages.find do |publication|
|
83
|
+
publication[:subject] == subject &&
|
84
|
+
publication[:data] == data &&
|
85
|
+
publication[:options] == options
|
86
|
+
end
|
70
87
|
end
|
71
88
|
|
72
89
|
private
|
73
90
|
|
74
|
-
def
|
91
|
+
def stringify_keys(object)
|
75
92
|
case object
|
76
93
|
when Array
|
77
|
-
object.map {|k|
|
94
|
+
object.map {|k| stringify_keys(k) }
|
78
95
|
when Hash
|
79
96
|
object.inject({}) do |memo, (key, value)|
|
80
|
-
memo[key.
|
97
|
+
memo[key.to_s] = stringify_keys(value)
|
81
98
|
memo
|
82
99
|
end
|
83
100
|
else
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cf-message-bus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-11-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -64,17 +64,23 @@ dependencies:
|
|
64
64
|
requirement: !ruby/object:Gem::Requirement
|
65
65
|
none: false
|
66
66
|
requirements:
|
67
|
-
- - '
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 0.5.0.beta.12
|
70
|
+
- - <
|
68
71
|
- !ruby/object:Gem::Version
|
69
|
-
version: 0.
|
72
|
+
version: '0.6'
|
70
73
|
type: :runtime
|
71
74
|
prerelease: false
|
72
75
|
version_requirements: !ruby/object:Gem::Requirement
|
73
76
|
none: false
|
74
77
|
requirements:
|
75
|
-
- - '
|
78
|
+
- - ! '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: 0.5.0.beta.12
|
81
|
+
- - <
|
76
82
|
- !ruby/object:Gem::Version
|
77
|
-
version: 0.
|
83
|
+
version: '0.6'
|
78
84
|
- !ruby/object:Gem::Dependency
|
79
85
|
name: vcap-concurrency
|
80
86
|
requirement: !ruby/object:Gem::Requirement
|