Class: Cuboid::RPC::Client::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/cuboid/rpc/client/instance.rb,
lib/cuboid/rpc/client/instance/service.rb

Overview

RPC client for remote instances spawned by a remote agent

Author:

Defined Under Namespace

Classes: Proxy

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, token = nil, options = nil) ⇒ Instance

Returns a new instance of Instance.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/cuboid/rpc/client/instance.rb', line 46

def initialize( url, token = nil, options = nil )
    @token    = token
    @client   = Base.new( url, token, options )

    @instance = Proxy.new( @client )
    @options  = Toq::Proxy.new( @client, 'options' )

    # map Agent handlers
    Cuboid::Application.application.instance_services.keys.each do |name|
        self.class.send( :attr_reader, name.to_sym )

        instance_variable_set(
          "@#{name}".to_sym,
          Toq::Proxy.new( @client, name )
        )
    end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object (private)

Used to provide the illusion of locality for remote methods



95
96
97
# File 'lib/cuboid/rpc/client/instance.rb', line 95

def method_missing( sym, *args, &block )
    @instance.send( sym, *args, &block )
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/cuboid/rpc/client/instance.rb', line 14

def options
  @options
end

#pidObject

Not always available, set by the parent.



13
14
15
# File 'lib/cuboid/rpc/client/instance.rb', line 13

def pid
  @pid
end

Class Method Details

.when_ready(url, token, &block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cuboid/rpc/client/instance.rb', line 20

def when_ready( url, token, &block )
    options = Cuboid::Options.rpc.to_client_options.merge(
        client_max_retries:   0,
        connection_pool_size: 1
    )

    raktr = Raktr.new
    raktr.run_in_thread

    client = new( url, token, options )
    raktr.delay( 0.1 ) do |task|
        client.alive? do |r|
            if r.rpc_exception?
                raktr.delay( 0.1, &task )
                next
            end

            client.close

            block.call
        end
    end
end

Instance Method Details

#addressObject



84
85
86
# File 'lib/cuboid/rpc/client/instance.rb', line 84

def address
    @client.address
end

#clientObject



72
73
74
# File 'lib/cuboid/rpc/client/instance.rb', line 72

def client
    @client
end

#closeObject



76
77
78
# File 'lib/cuboid/rpc/client/instance.rb', line 76

def close
    @client.close
end

#portObject



88
89
90
# File 'lib/cuboid/rpc/client/instance.rb', line 88

def port
    @client.port
end

#tokenObject



68
69
70
# File 'lib/cuboid/rpc/client/instance.rb', line 68

def token
    @token
end

#urlObject



80
81
82
# File 'lib/cuboid/rpc/client/instance.rb', line 80

def url
    @client.url
end

#when_ready(&block) ⇒ Object



64
65
66
# File 'lib/cuboid/rpc/client/instance.rb', line 64

def when_ready( &block )
    self.class.when_ready( url, token, &block )
end