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:

  • Tasos “Zapotek” Laskos <tasos.laskos@gmail.com>

Defined Under Namespace

Classes: Proxy

Constant Summary collapse

RETRIES =
5

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.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/cuboid/rpc/client/instance.rb', line 56

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



105
106
107
# File 'lib/cuboid/rpc/client/instance.rb', line 105

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



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cuboid/rpc/client/instance.rb', line 22

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 )
    tries = 0
    raktr.delay( 0.1 ) do |task|
        client.alive? do |r|
            if r.rpc_exception?
                if tries >= RETRIES
                    client.close
                    block.call
                    next
                end

                raktr.delay( 0.1, &task )
                tries += 1
                next
            end

            client.close

            block.call
        end
    end
end

Instance Method Details

#addressObject



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

def address
    @client.address
end

#clientObject



82
83
84
# File 'lib/cuboid/rpc/client/instance.rb', line 82

def client
    @client
end

#closeObject



86
87
88
# File 'lib/cuboid/rpc/client/instance.rb', line 86

def close
    @client.close
end

#portObject



98
99
100
# File 'lib/cuboid/rpc/client/instance.rb', line 98

def port
    @client.port
end

#tokenObject



78
79
80
# File 'lib/cuboid/rpc/client/instance.rb', line 78

def token
    @token
end

#urlObject



90
91
92
# File 'lib/cuboid/rpc/client/instance.rb', line 90

def url
    @client.url
end

#when_ready(&block) ⇒ Object



74
75
76
# File 'lib/cuboid/rpc/client/instance.rb', line 74

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