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
13 14 15 16 17 18 19 20 |
# File 'lib/basicjrpc/client.rb', line 13 def initialize(queue, timeout = 5) @queue = queue @payload = {} @timeout = timeout @instance_id = SecureRandom.uuid #@nsq_producer = Nsq::Producer.new(nsqd: 'dockerlb:4150', topic: @queue) @redis = Redis.new(host: "redis") end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
22 23 24 |
# File 'lib/basicjrpc/client.rb', line 22 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
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/basicjrpc/client.rb', line 26 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)) @redis.rpush(@queue, Oj.dump(payload)) Timeout::timeout(5) { ### OR USE POP AND LLEN Oj.load(@redis.blpop(payload['message_id'])[1]) } rescue Exception => e #terminate raise e end |
#terminate ⇒ Object
This must be called
47 48 49 |
# File 'lib/basicjrpc/client.rb', line 47 def terminate #@nsq_producer.terminate end |