Class: BasicJRPC::Client
- Inherits:
-
Object
- Object
- BasicJRPC::Client
- Defined in:
- lib/basicjrpc/client.rb
Overview
Responding Client
Direct Known Subclasses
Instance Method Summary collapse
-
#initialize(queue, timeout = 5) ⇒ Client
constructor
A new instance of Client.
- #method_missing(m, *args, &block) ⇒ Object
- #send_request(payload) ⇒ Object
-
#terminate ⇒ Object
This must be called.
Constructor Details
#initialize(queue, timeout = 5) ⇒ Client
Returns a new instance of Client.
13 14 15 16 17 18 19 |
# File 'lib/basicjrpc/client.rb', line 13 def initialize(queue, timeout = 5) @queue = queue @payload = {} @timeout = timeout @instance_id = SecureRandom.uuid @nsq_producer = Krakow::Producer.new(host: 'dockerlb', port: '4150', topic: @queue) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
21 22 23 |
# File 'lib/basicjrpc/client.rb', line 21 def method_missing(m, *args, &block) send_request({ :method_name => m, :method_arguments => args, :callers => caller.first(10) }) end |
Instance Method Details
#send_request(payload) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/basicjrpc/client.rb', line 25 def send_request payload payload[:message_id] = SecureRandom.uuid payload[:instance_id] = @instance_id payload[:response] = true = false puts "Writing Message #{payload[:message_id]} #{payload[:method_name]}" @nsq_producer.write(Oj.dump(payload)) Timeout::timeout(5) { Oj.load(Redis.new(host: "redis").blpop(payload[:message_id])[1]) } rescue Exception => e terminate raise e end |
#terminate ⇒ Object
This must be called
44 45 46 |
# File 'lib/basicjrpc/client.rb', line 44 def terminate @nsq_producer.terminate end |