redis_rpc 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b57e11a1d1707de2319945e009d0ddb52cef370d
4
+ data.tar.gz: c487af813d9c74f14c1e1a337cb48be74714be75
5
+ SHA512:
6
+ metadata.gz: 3635a11d7b1b7ff4a9e19c9e22ebd2ed59b59b2265021e1e455eeefec3e8cc77f86bc1c160dfd9ba61998788716b08263a07a8afe13a5f1fd1f3e1434a25d509
7
+ data.tar.gz: ab06cdfef10e8ef1618db0cc3220f64a0aa61298cfade95f73a9835d03e066dcf32f045c2bf945aa6e2aed03735b1e0fdff4f68f9aa3c058ededc9e0678ae99c
@@ -0,0 +1,21 @@
1
+ module RedisRpc
2
+
3
+ class Callback
4
+
5
+ def initialize
6
+ @funs = {}
7
+ end
8
+
9
+ def exec_callback(args)
10
+ _args = JSON.parse(args, symbolize_names: true)
11
+ # {uuid: uuid, _method: method, result: result}
12
+ callback = @funs.delete _args[:uuid]
13
+ callback.call(_args[:result]) if !callback.nil?
14
+ end
15
+
16
+ def push(uuid, callback)
17
+ @funs[uuid] = callback
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,80 @@
1
+ require 'redis'
2
+ require 'logger'
3
+ require 'securerandom'
4
+ require "redis_rpc/response.rb"
5
+ require "redis_rpc/callback.rb"
6
+
7
+ module RedisRpc
8
+
9
+ class Client
10
+
11
+ def initialize(url, sub_channel, pub_channel, level: Logger::WARN)
12
+ @redis = Redis.new(url: url)
13
+ @sub_channel = sub_channel
14
+ @pub_channel = pub_channel
15
+ @res = Response.new(Redis.new(url: url), pub_channel)
16
+ @callback = Callback.new
17
+ init_log(level)
18
+ exec
19
+ end
20
+
21
+ def exec
22
+ @thread = Thread.new do
23
+ begin
24
+ @redis.subscribe(@sub_channel, RedisRpc::EXCEPTION_CHANNEL) do |on|
25
+ on.subscribe do |channel, subscriptions|
26
+ @logger.info("Subscribed to ##{channel} (#{subscriptions} subscriptions)")
27
+ end
28
+
29
+ on.message do |channel, args|
30
+ @logger.info("##{channel}: #{args}")
31
+ case channel.to_sym
32
+ when RedisRpc::EXCEPTION_CHANNEL
33
+ @logger.error("exception: #{args}")
34
+ else
35
+ begin
36
+ @callback.exec_callback(args)
37
+ rescue Exception => e
38
+ @logger.error(e.message.to_s)
39
+ end
40
+ end
41
+ end
42
+ on.unsubscribe do |channel, subscriptions|
43
+ @logger.info("Unsubscribed from ##{channel} (#{subscriptions} subscriptions)")
44
+ end
45
+ end
46
+ rescue Redis::BaseConnectionError => error
47
+ @logger.error("#{error}, retrying in 30s")
48
+ sleep 30
49
+ retry
50
+ end
51
+ end
52
+ end
53
+
54
+ def init_log(level)
55
+ if defined?(Rails)
56
+ @logger = Rails.logger
57
+ else
58
+ require 'logger'
59
+ @logger = ::Logger.new(STDOUT)
60
+ @logger.level = level
61
+ end
62
+ @logger
63
+ end
64
+
65
+ def method_missing(m, *args, &block)
66
+ request = {
67
+ method: m,
68
+ params: args,
69
+ uuid: SecureRandom.uuid
70
+ }
71
+ @callback.push(request[:uuid], block)
72
+ @res.publish(request)
73
+ end
74
+
75
+ end
76
+
77
+
78
+
79
+
80
+ end
@@ -0,0 +1,28 @@
1
+ require "redis_rpc/response.rb"
2
+ module RedisRpc
3
+
4
+ class Logic
5
+
6
+ attr_accessor :res
7
+
8
+ def initialize(url, callback, channel)
9
+ @redis = Redis.new(url: url)
10
+ @res = Response.new(@redis, channel)
11
+ @callback = callback
12
+ end
13
+
14
+ def exec(args)
15
+ begin
16
+ _args = JSON.parse(args, symbolize_names: true)
17
+ raise(ArgumentError, "miss callback uuid") if _args[:uuid].nil?
18
+ raise(ArgumentError, "miss method name") if _args[:method].nil?
19
+
20
+ result = @callback.send(_args[:method], *_args[:params])
21
+ @res.publish({uuid: _args[:uuid], _method: _args[:method], result: result})
22
+ rescue Exception => e
23
+ @res.catch(e)
24
+ end
25
+ end
26
+ end
27
+
28
+ end
@@ -0,0 +1,25 @@
1
+ require 'json'
2
+ module RedisRpc
3
+
4
+ class Response
5
+
6
+ def initialize(redis, channel)
7
+ @redis = redis
8
+ @channel = channel
9
+ end
10
+
11
+ def publish(request)
12
+ @redis.publish(@channel, request.to_json)
13
+ end
14
+
15
+ def catch(e)
16
+ publish(RedisRpc::EXCEPTION_CHANNEL, e.message.to_s)
17
+ end
18
+
19
+ def method_missing(m, *args)
20
+ publish(m, args)
21
+ end
22
+
23
+ end
24
+
25
+ end
@@ -0,0 +1,70 @@
1
+ require 'redis'
2
+ require 'logger'
3
+ require "redis_rpc/client.rb"
4
+ require "redis_rpc/version.rb"
5
+ require "redis_rpc/logic.rb"
6
+
7
+ module RedisRpc
8
+
9
+ class Server
10
+
11
+ def initialize(url, sub_channel, pub_channel, front_object, level: Logger::WARN, standalone: true)
12
+ @redis = Redis.new(url: url)
13
+ @sub_channel = sub_channel
14
+ @pub_channel = pub_channel
15
+ @logic = Logic.new(url, front_object, pub_channel)
16
+ init_log(level)
17
+ standalone ? standalone_exec : exec
18
+ end
19
+
20
+ def standalone_exec
21
+ @thread = Thread.new do
22
+ exec
23
+ end
24
+ end
25
+
26
+ def exec
27
+ begin
28
+ @redis.subscribe(@sub_channel, RedisRpc::EXCEPTION_CHANNEL) do |on|
29
+ on.subscribe do |channel, subscriptions|
30
+ @logger.info("Subscribed to ##{channel} (#{subscriptions} subscriptions)")
31
+ end
32
+
33
+ on.message do |channel, args|
34
+ @logger.info("##{channel}: #{args}")
35
+ case channel.to_sym
36
+ when RedisRpc::EXCEPTION_CHANNEL
37
+ @logger.error("exception: #{args}")
38
+ else
39
+ begin
40
+ @logic.exec(args)
41
+ rescue Exception => e
42
+ @logger.error(e.message.to_s)
43
+ end
44
+ end
45
+ end
46
+ on.unsubscribe do |channel, subscriptions|
47
+ @logger.info("Unsubscribed from ##{channel} (#{subscriptions} subscriptions)")
48
+ end
49
+ end
50
+ rescue Redis::BaseConnectionError => error
51
+ @logger.error("#{error}, retrying in 30s")
52
+ sleep 30
53
+ retry
54
+ end
55
+ end
56
+
57
+ def init_log(level)
58
+ if defined?(Rails)
59
+ @logger = Rails.logger
60
+ else
61
+ require 'logger'
62
+ @logger = ::Logger.new(STDOUT)
63
+ @logger.level = level
64
+ end
65
+ @logger
66
+ end
67
+
68
+ end
69
+
70
+ end
@@ -0,0 +1,3 @@
1
+ module RedisRpc
2
+ VERSION = "0.0.1"
3
+ end
data/lib/redis_rpc.rb ADDED
@@ -0,0 +1,20 @@
1
+ require "redis_rpc/callback.rb"
2
+ require "redis_rpc/client.rb"
3
+ require "redis_rpc/logic.rb"
4
+ require "redis_rpc/response.rb"
5
+ require "redis_rpc/server.rb"
6
+ require "redis_rpc/version.rb"
7
+
8
+ module RedisRpc
9
+
10
+ EXCEPTION_CHANNEL = :redis_rpc_exception
11
+
12
+ class << self
13
+
14
+ def new
15
+
16
+ end
17
+
18
+ end
19
+
20
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redis_rpc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - benko
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-11-16 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: use redis sub/pub work as rpc
14
+ email: benko.b@shopperplus.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/redis_rpc.rb
20
+ - lib/redis_rpc/callback.rb
21
+ - lib/redis_rpc/client.rb
22
+ - lib/redis_rpc/logic.rb
23
+ - lib/redis_rpc/response.rb
24
+ - lib/redis_rpc/server.rb
25
+ - lib/redis_rpc/version.rb
26
+ homepage:
27
+ licenses: []
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 2.6.11
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: redis rpc
49
+ test_files: []