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, message_id = SecureRandom.uuid) ⇒ 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) }, args[:message_id]) end |
Instance Method Details
#send_request(payload, message_id = SecureRandom.uuid) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/basicjrpc/client.rb', line 26 def send_request payload, =SecureRandom.uuid payload[:message_id] = payload[:instance_id] = @instance_id payload[:response] = true = false puts "Writing Message #{payload[:message_id]} #{payload[:method_name]}" start = Time.now #@nsq_producer.write(Oj.dump(payload)) @redis.rpush(@queue, Oj.dump(payload)) Timeout::timeout(5) { ### OR USE POP AND LLEN r = Oj.load(@redis.blpop(payload[:message_id])[1]) puts "Message Received in #{Time.now - start}" r } rescue Exception => e #terminate raise e end |
#terminate ⇒ Object
This must be called
50 51 52 |
# File 'lib/basicjrpc/client.rb', line 50 def terminate #@nsq_producer.terminate end |