Class: BasicJRPC::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/basicjrpc/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(queue, injected_class) ⇒ Server

Returns a new instance of Server.



4
5
6
7
8
# File 'lib/basicjrpc/server.rb', line 4

def initialize queue, injected_class
  @injected_class = injected_class
  @queue = queue
  @redis = Redis.new(host: "redis")
end

Instance Method Details

#listenObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/basicjrpc/server.rb', line 10

def listen
  puts "Listening..."
  while true
    payload = @redis.blpop(@queue)[1]
    payload = Oj.load(payload)
    puts "Processing message #{payload.method_name} #{payload.method_arguments}" if BasicJRPC::Config.debug
    
    # Should always return a data object
    response = @injected_class.send(payload.method_name, *payload.method_arguments)
      
    # Bounce the response back if response is requested
    @redis.rpush(payload.message_id, Oj.dump(response)) if payload.response
  end
end