Class: SGS::RPCServer

Inherits:
Object
  • Object
show all
Defined in:
lib/sgs/rpc.rb

Instance Method Summary collapse

Constructor Details

#initialize(channel, klass) ⇒ RPCServer

Returns a new instance of RPCServer.



64
65
66
67
# File 'lib/sgs/rpc.rb', line 64

def initialize(channel, klass)
  @channel = channel.to_s
  @klass = klass
end

Instance Method Details

#startObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/sgs/rpc.rb', line 69

def start
  puts "Starting RPC server for #{@channel}"
  loop do
    channel, request = RedisBase.redis.brpop(@channel)
    request = MessagePack.unpack(request)

    puts "Working on request: #{request['id']}"

    args = request['params'].unshift(request['method'])
    result = @klass.send *args

    reply = {
      'jsonrpc' => '2.0',
      'result' => result,
      'id' => request['id']
    }

    RedisBase.redis.rpush(request['id'], MessagePack.pack(reply))
    RedisBase.redis.expire(request['id'], 30)
  end
end