kimchi 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/kimchi/version.rb +1 -1
- data/lib/wire.rb +75 -24
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8883424bb826544feaef6c94a6b55f11692b5d08
|
4
|
+
data.tar.gz: f28ce0474a811d495876985354ffc03c489c6457
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 13a423da3a62ff956d1ed2e3eaa9d13db48d0af6070b0328abd4d3833e609a4c4b62d32f670e74be49f9226ce68948816c13919d5e5e00b577768533ee9382c2
|
7
|
+
data.tar.gz: 382c61c84602b4e7591d44611bc5d4abbcd2aa8789a384b3ba269d8fe6ff0d8f946d8294582a55c3f0978a203cc4889525a19bf9ada6622a2abfc757a1d5cbce
|
data/lib/kimchi/version.rb
CHANGED
data/lib/wire.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require "rubygems"
|
2
2
|
require "uuid"
|
3
3
|
require "bunny"
|
4
|
+
require "net/http"
|
4
5
|
|
5
6
|
module Wire
|
6
7
|
|
@@ -12,8 +13,18 @@ module Wire
|
|
12
13
|
@native_type = native_type
|
13
14
|
end
|
14
15
|
|
16
|
+
# ignore this for any real service call, the http endpoint is evidently incapable of getting this from a header
|
17
|
+
def service= service
|
18
|
+
@service=service
|
19
|
+
end
|
20
|
+
|
21
|
+
# ignore this for any real service call, the http endpoint is evidently incapable of getting this from a header
|
22
|
+
def version= version
|
23
|
+
@version=version
|
24
|
+
end
|
25
|
+
|
15
26
|
def to_json
|
16
|
-
"{\"endpoint\": \"#{@name}\"" + ((@signature == nil) ? "" : ", \"signature\": #{@signature}") + ((@result_type == nil) ? "" : ", \"resultType\": \"#{@result_type}\"") + ((@native_type == nil) ? "" : ", \"nativeType\": \"#{@native_type}\"") + "}"
|
27
|
+
"{\"endpoint\": \"#{@name}\"" + ((@signature == nil) ? "" : ", \"signature\": #{@signature}") + ((@result_type == nil) ? "" : ", \"resultType\": \"#{@result_type}\"") + ((@native_type == nil) ? "" : ", \"nativeType\": \"#{@native_type}\"") + ((@service == nil) ? "" : ", \"service\": \"#{@service}\"") + ((@version == nil) ? "" : ", \"version\": \"#{@version}\"") + "}"
|
17
28
|
end
|
18
29
|
end
|
19
30
|
|
@@ -24,8 +35,20 @@ module Wire
|
|
24
35
|
@contexts = contexts
|
25
36
|
end
|
26
37
|
|
38
|
+
def function
|
39
|
+
@function
|
40
|
+
end
|
41
|
+
|
42
|
+
def contexts=contexts
|
43
|
+
@contexts=contexts
|
44
|
+
end
|
45
|
+
|
46
|
+
def contexts
|
47
|
+
@contexts
|
48
|
+
end
|
49
|
+
|
27
50
|
def to_json
|
28
|
-
"{\"address\": #{@function.to_json}, \"body\": #{@arguments.to_json}, \"contexts\": #{@contexts}}"
|
51
|
+
"{\"address\": #{@function.to_json}, \"body\": #{@arguments.to_json}, \"contexts\": #{@contexts.to_json}}"
|
29
52
|
end
|
30
53
|
end
|
31
54
|
|
@@ -55,39 +78,52 @@ module Wire
|
|
55
78
|
end
|
56
79
|
|
57
80
|
class RabbitMQTransport
|
58
|
-
def initialize (host, port
|
81
|
+
def initialize (host, port)
|
59
82
|
@connection = Bunny.new("amqp://#{host}:#{port}")
|
60
83
|
@connection.start
|
61
84
|
|
85
|
+
@semaphore = Mutex.new
|
86
|
+
|
62
87
|
@uuid = UUID.new
|
63
88
|
@caller_id = @uuid.generate
|
64
89
|
|
90
|
+
@topic_hash = {}
|
65
91
|
@message_hash = {}
|
92
|
+
end
|
66
93
|
|
67
|
-
|
68
|
-
@
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
94
|
+
def initialize_service (service_name)
|
95
|
+
@semaphore.synchronize do
|
96
|
+
topic = @topic_hash[service_name]
|
97
|
+
|
98
|
+
if topic == nil
|
99
|
+
request_channel = @connection.create_channel
|
100
|
+
topic = request_channel.topic("#{service_name}.request")
|
101
|
+
|
102
|
+
response_queue_name = @uuid.generate
|
103
|
+
response_channel = @connection.create_channel
|
104
|
+
response_exchange = response_channel.topic("#{service_name}.response")
|
105
|
+
response_queue = response_channel.queue("#{response_queue_name}", {:durable => false, :exclusive => true, :auto_delete => true})
|
106
|
+
|
107
|
+
response_queue.bind(response_exchange, :routing_key => "#{@caller_id}").subscribe do |delivery_info, metadata, payload|
|
108
|
+
if (message_callback = @message_hash[metadata[:correlation_id]]) != nil
|
109
|
+
result_body = JSON.parse(payload)
|
110
|
+
message_callback.set_result(result_body)
|
111
|
+
end
|
112
|
+
end
|
79
113
|
end
|
114
|
+
|
115
|
+
return topic
|
80
116
|
end
|
81
117
|
end
|
82
118
|
|
83
|
-
def transmit_in_only(version, invocation_signal)
|
84
|
-
|
119
|
+
def transmit_in_only(service_name, version, invocation_signal)
|
120
|
+
initialize_service(service_name).publish(invocation_signal.to_json, :routing_key => "#{version}.2", :headers => {"x-opt-callerId" => "#{@caller_id}"}, :content_type => "application/json", :message_id => "#{@uuid.generate}", :timestamp => Time.new.to_i)
|
85
121
|
end
|
86
122
|
|
87
|
-
def transmit(version, invocation_signal, callback, timeout_seconds)
|
123
|
+
def transmit(service_name, version, invocation_signal, callback, timeout_seconds)
|
88
124
|
message_id = "#{@uuid.generate}".freeze
|
89
125
|
@message_hash[message_id] = MessageCallback.new(@message_hash, message_id, callback, timeout_seconds)
|
90
|
-
|
126
|
+
initialize_service(service_name).publish(invocation_signal.to_json, :routing_key => "#{version}.2", :headers => {"x-opt-callerId" => "#{@caller_id}"}, :content_type => "application/json", :message_id => "#{message_id}", :timestamp => Time.new.to_i)
|
91
127
|
end
|
92
128
|
end
|
93
129
|
|
@@ -96,16 +132,31 @@ module Wire
|
|
96
132
|
@host = host
|
97
133
|
@port = port
|
98
134
|
end
|
99
|
-
|
135
|
+
|
136
|
+
def transmit (service_name, version, invocation_signal, callback, timeout_seconds)
|
100
137
|
uri = URI("#{@host}:#{@port}/api/message")
|
101
|
-
headers = {"Content-Type" => "application/json"
|
138
|
+
headers = {"Content-Type" => "application/json"}
|
102
139
|
http = Net::HTTP.new(uri.host, uri.port)
|
140
|
+
http.read_timeout = timeout_seconds
|
141
|
+
|
142
|
+
invocation_signal.function.service = service_name
|
143
|
+
invocation_signal.function.version = version
|
144
|
+
|
145
|
+
context_hash = {}
|
146
|
+
invocation_signal.contexts.each {|context| context.each{|key,value| context_hash[key] = value}}
|
147
|
+
invocation_signal.contexts = context_hash
|
103
148
|
|
104
149
|
res = http.post(uri.path, invocation_signal.to_json, headers)
|
105
|
-
res.code.should == '200'
|
106
150
|
|
107
|
-
|
108
|
-
|
151
|
+
case res
|
152
|
+
when Net::HTTPSuccess
|
153
|
+
response = JSON.parse res.body
|
154
|
+
callback.complete(response)
|
155
|
+
else
|
156
|
+
res.error!
|
157
|
+
end
|
158
|
+
rescue Timeout::Error
|
159
|
+
callback.timeout
|
109
160
|
end
|
110
161
|
end
|
111
162
|
end
|