cf-message-bus 0.0.1 → 0.0.2
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 +26 -19
- data/lib/cf_message_bus/version.rb +1 -1
- metadata +2 -2
@@ -14,10 +14,10 @@ module CfMessageBus
|
|
14
14
|
@recovery_callback = lambda {}
|
15
15
|
end
|
16
16
|
|
17
|
-
def subscribe(subject,
|
18
|
-
@subscriptions[subject] = [
|
17
|
+
def subscribe(subject, options = {}, &block)
|
18
|
+
@subscriptions[subject] = [options, block]
|
19
19
|
|
20
|
-
subscribe_on_reactor(subject,
|
20
|
+
subscribe_on_reactor(subject, options) do |parsed_data, inbox|
|
21
21
|
EM.defer do
|
22
22
|
run_handler(block, parsed_data, inbox, subject, 'subscription')
|
23
23
|
end
|
@@ -35,35 +35,42 @@ module CfMessageBus
|
|
35
35
|
end
|
36
36
|
|
37
37
|
def request(subject, data = nil, options = {}, &block)
|
38
|
-
|
38
|
+
response_timeout = options.delete(:timeout)
|
39
|
+
result_count = options.delete(:result_count)
|
40
|
+
options[:max] = result_count if result_count
|
41
|
+
|
42
|
+
subscription_id = internal_bus.request(subject, encode(data), options) do |payload, inbox|
|
39
43
|
process_message(payload, inbox) do |parsed_data, inbox|
|
40
44
|
run_handler(block, parsed_data, inbox, subject, 'response')
|
41
45
|
end
|
42
46
|
end
|
47
|
+
|
48
|
+
if response_timeout
|
49
|
+
internal_bus.timeout(subscription_id, response_timeout, expected: result_count || 1) do
|
50
|
+
run_handler(block, {timeout: true}, nil, subject, 'timeout')
|
51
|
+
end
|
52
|
+
end
|
53
|
+
subscription_id
|
43
54
|
end
|
44
55
|
|
45
|
-
def synchronous_request(subject, data = nil,
|
46
|
-
|
47
|
-
|
56
|
+
def synchronous_request(subject, data = nil, options = {})
|
57
|
+
options[:result_count] ||= 1
|
58
|
+
result_count = options[:result_count]
|
48
59
|
|
49
60
|
return [] if result_count <= 0
|
50
61
|
|
51
|
-
|
62
|
+
EM.schedule_sync do |promise|
|
52
63
|
results = []
|
53
64
|
|
54
|
-
|
55
|
-
|
56
|
-
promise.deliver(results) if results.size == result_count
|
57
|
-
end
|
58
|
-
|
59
|
-
if timeout >= 0
|
60
|
-
internal_bus.timeout(sid, timeout, expected: result_count) do
|
65
|
+
request(subject, encode(data), options) do |response|
|
66
|
+
if response[:timeout]
|
61
67
|
promise.deliver(results)
|
68
|
+
else
|
69
|
+
results << response
|
70
|
+
promise.deliver(results) if results.size == result_count
|
62
71
|
end
|
63
72
|
end
|
64
73
|
end
|
65
|
-
|
66
|
-
response
|
67
74
|
end
|
68
75
|
|
69
76
|
def unsubscribe(subscription_id)
|
@@ -95,9 +102,9 @@ module CfMessageBus
|
|
95
102
|
end
|
96
103
|
end
|
97
104
|
|
98
|
-
def subscribe_on_reactor(subject,
|
105
|
+
def subscribe_on_reactor(subject, options = {}, &blk)
|
99
106
|
EM.schedule do
|
100
|
-
internal_bus.subscribe(subject,
|
107
|
+
internal_bus.subscribe(subject, options) do |msg, inbox|
|
101
108
|
process_message(msg, inbox, &blk)
|
102
109
|
end
|
103
110
|
end
|
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.0.2
|
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-07-
|
12
|
+
date: 2013-07-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|