redis_rpc 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/lib/redis_rpc/client.rb +3 -2
- data/lib/redis_rpc/logic.rb +23 -14
- data/lib/redis_rpc/response.rb +26 -25
- data/lib/redis_rpc/server.rb +2 -1
- data/lib/redis_rpc/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a5a0c8bebe40efe81928d485dbe2f747c5707be39d44bd093a68c23bd7530028
|
4
|
+
data.tar.gz: 5bff08a69ee054753c03df250756dd663bfe8bc268bf4b72bfbcc546b382f35b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4aeacfe6adf23f5e3fcbff5396ceed744fe4c312c4b949537cb1b81cb4a19325691a643880b187980e0c81b8062c5313aac9fa8ba1273f026c490b703820f969
|
7
|
+
data.tar.gz: 6600c450251f5f1f2bb8fe89dcb96f15977cba58ab3a41878e19a1f54518b88d8fca14243fad9b2b789c3a0781288305993af7463940f7607e0e3fb78698f304
|
data/lib/redis_rpc/client.rb
CHANGED
@@ -14,6 +14,7 @@ module RedisRpc
|
|
14
14
|
@redis = Redis.new(url: url)
|
15
15
|
@sub_channel = sub_channel
|
16
16
|
@pub_channel = pub_channel
|
17
|
+
@level = level
|
17
18
|
@timeout = timeout
|
18
19
|
@parser = Parser.new(secret_key)
|
19
20
|
@res = Response.new(Redis.new(url: url), pub_channel, init_log(level), @parser)
|
@@ -30,11 +31,11 @@ module RedisRpc
|
|
30
31
|
end
|
31
32
|
|
32
33
|
on.message do |channel, args|
|
33
|
-
@logger.info("##{channel}: #{args}")
|
34
|
+
@logger.info("##{channel}: #{args}") if @level <= Logger::INFO
|
34
35
|
begin
|
35
36
|
_args = @parser.parse(args)
|
36
37
|
@logger.error(ArgumentError.new("miss method uuid")) and return if _args[:uuid].nil?
|
37
|
-
@res.sync_callback(_args
|
38
|
+
@res.sync_callback(_args) if !@callback.exec_callback(_args)
|
38
39
|
rescue Exception => e
|
39
40
|
@logger.error(e)
|
40
41
|
end
|
data/lib/redis_rpc/logic.rb
CHANGED
@@ -1,23 +1,22 @@
|
|
1
1
|
require "redis_rpc/response.rb"
|
2
2
|
module RedisRpc
|
3
3
|
|
4
|
-
|
4
|
+
class Logic
|
5
5
|
|
6
|
-
|
6
|
+
attr_accessor :res
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
end
|
8
|
+
def initialize(url, callback, channel, logger, parser)
|
9
|
+
@logger = logger
|
10
|
+
@res = ClientResponse.new(Redis.new(url: url), channel, logger, parser)
|
11
|
+
@callback = callback
|
12
|
+
@parser = parser
|
13
|
+
end
|
15
14
|
|
16
|
-
|
15
|
+
def exec(args, timeout)
|
16
|
+
Thread.new do
|
17
17
|
begin
|
18
18
|
_args = @parser.parse(args)
|
19
|
-
logger.error(ArgumentError.new("miss method name or uuid")) and return if _args[:uuid].nil? || _args[:method].nil?
|
20
|
-
|
19
|
+
@logger.error(ArgumentError.new("miss method name or uuid")) and return if _args[:uuid].nil? || _args[:method].nil?
|
21
20
|
result = @callback.send(_args[:method], *_args[:params])
|
22
21
|
@res.publish({uuid: _args[:uuid], _method: _args[:method], result: result}, timeout)
|
23
22
|
rescue Exception => e
|
@@ -27,9 +26,19 @@ module RedisRpc
|
|
27
26
|
@logger.error(e)
|
28
27
|
end
|
29
28
|
end
|
30
|
-
rescue Exception => e
|
31
|
-
@logger.error(e)
|
32
29
|
end
|
30
|
+
rescue Exception => e
|
31
|
+
@logger.error(e)
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
class ClientResponse < Response
|
37
|
+
|
38
|
+
def publish(request, timeout)
|
39
|
+
request_str = @parser.pack(request.to_json)
|
40
|
+
@redis.publish(@channel, request_str)
|
41
|
+
end
|
33
42
|
|
34
43
|
end
|
35
44
|
|
data/lib/redis_rpc/response.rb
CHANGED
@@ -11,18 +11,22 @@ module RedisRpc
|
|
11
11
|
@channel = channel
|
12
12
|
@logger = logger
|
13
13
|
@parser = parser
|
14
|
+
@sync_handlers = {}
|
14
15
|
end
|
15
16
|
|
16
17
|
def publish(request, timeout)
|
17
18
|
request_str = @parser.pack(request.to_json)
|
18
19
|
@redis.publish(@channel, request_str)
|
19
|
-
|
20
|
+
@sync_handlers[request[:uuid]] = SyncHandler.new(@parser.secret_key, request[:method], timeout)
|
21
|
+
Thread.new(@sync_handlers, request[:uuid], timeout) {|handlers, uuid, t| sleep(t+3); handlers.delete(uuid) }
|
22
|
+
@sync_handlers[request[:uuid]]
|
20
23
|
end
|
21
24
|
|
22
|
-
def sync_callback(args
|
23
|
-
|
24
|
-
|
25
|
-
|
25
|
+
def sync_callback(args)
|
26
|
+
# {uuid: uuid, _method: method, result: result, error: error}
|
27
|
+
unless (sync_handler = @sync_handlers.delete(args[:uuid])).nil?
|
28
|
+
sync_handler.release(args)
|
29
|
+
end
|
26
30
|
end
|
27
31
|
|
28
32
|
def catch(uuid, e)
|
@@ -35,31 +39,28 @@ module RedisRpc
|
|
35
39
|
|
36
40
|
class SyncHandler
|
37
41
|
|
38
|
-
|
39
|
-
|
40
|
-
def initialize(redis, uuid, _method, secret_key, timeout=30)
|
41
|
-
@redis = redis
|
42
|
-
@uuid = uuid
|
43
|
-
@_method = _method
|
44
|
-
@expires_at = Time.now + timeout
|
42
|
+
def initialize(secret_key, _method, timeout=10)
|
45
43
|
@parser = Parser.new(secret_key)
|
44
|
+
@_method = _method
|
45
|
+
@timeout = timeout
|
46
|
+
@lock = Mutex.new
|
47
|
+
@condition = ConditionVariable.new
|
46
48
|
end
|
47
49
|
|
48
50
|
def sync
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
elsif !_args[:error].nil?
|
57
|
-
raise(FunctionCallbackError.new(_args[:error]))
|
58
|
-
end
|
59
|
-
end
|
60
|
-
sleep SLEEP_TIME
|
51
|
+
@lock.synchronize { @condition.wait(@lock, @timeout) }
|
52
|
+
if @response.nil?
|
53
|
+
raise(Timeout::Error.new("method: #{@_method} wait for timeout #{@timeout}s"))
|
54
|
+
elsif !@response[:result].nil?
|
55
|
+
return @response[:result]
|
56
|
+
elsif !@response[:error].nil?
|
57
|
+
raise(FunctionCallbackError.new(@response[:error]))
|
61
58
|
end
|
62
|
-
|
59
|
+
end
|
60
|
+
|
61
|
+
def release(res)
|
62
|
+
@response = res
|
63
|
+
@lock.synchronize { @condition.signal }
|
63
64
|
end
|
64
65
|
|
65
66
|
end
|
data/lib/redis_rpc/server.rb
CHANGED
@@ -13,6 +13,7 @@ module RedisRpc
|
|
13
13
|
@redis = Redis.new(url: url)
|
14
14
|
@sub_channel = sub_channel
|
15
15
|
@pub_channel = pub_channel
|
16
|
+
@level = level
|
16
17
|
@timeout = timeout
|
17
18
|
@parser = Parser.new(secret_key)
|
18
19
|
@logic = Logic.new(url, front_object, pub_channel, init_log(level), @parser)
|
@@ -33,7 +34,7 @@ module RedisRpc
|
|
33
34
|
end
|
34
35
|
|
35
36
|
on.message do |channel, args|
|
36
|
-
@logger.info("##{channel}: #{args}")
|
37
|
+
@logger.info("##{channel}: #{args}") if @level <= Logger::INFO
|
37
38
|
@logic.exec(args, @timeout)
|
38
39
|
end
|
39
40
|
on.unsubscribe do |channel, subscriptions|
|
data/lib/redis_rpc/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: redis_rpc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- benko
|
@@ -44,7 +44,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
44
44
|
version: '0'
|
45
45
|
requirements: []
|
46
46
|
rubyforge_project:
|
47
|
-
rubygems_version: 2.
|
47
|
+
rubygems_version: 2.7.7
|
48
48
|
signing_key:
|
49
49
|
specification_version: 4
|
50
50
|
summary: redis rpc
|