Class: ClusteredRpc::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/clustered_rpc/proxy.rb

Instance Method Summary collapse

Constructor Details

#initialize(target, options) ⇒ Proxy

Returns a new instance of Proxy.



3
4
5
6
# File 'lib/clustered_rpc/proxy.rb', line 3

def initialize(target, options)
  @target = target
  @options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, **kwargs) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/clustered_rpc/proxy.rb', line 8

def method_missing(method, *args, **kwargs)
  wait_seconds = (@options[:wait_seconds] || ENV['CLUSTERED_RPC_WAIT_SECONDS'] || 1.0).to_i
  request_id = ::ClusteredRpc.publish({'klass' => @target.name, 'method' => method, 'args' => args, 'kwargs' => kwargs}.merge(@options))
  {request_id: request_id, success: true, results: ::ClusteredRpc.get_result(request_id, wait_seconds)}
rescue => e 
  ClusteredRpc.logger.error "ClusteredRpc::Proxy encountered error: #{e.message}"
  {request_id: "Error", success: false, results: e.message}
end