Class: Cuboid::Processes::Instances

Inherits:
Object
  • Object
show all
Includes:
Utilities, Singleton
Defined in:
lib/cuboid/processes/instances.rb

Overview

Helper for managing RPC::Server::Instance processes.

Author:

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utilities

#available_port, available_port_mutex, #bytes_to_kilobytes, #bytes_to_megabytes, #caller_name, #caller_path, #exception_jail, #generate_token, #hms_to_seconds, #port_available?, #rand_port, #random_seed, #regexp_array_match, #remove_constants, #seconds_to_hms

Constructor Details

#initializeInstances

Returns a new instance of Instances.



16
17
18
19
# File 'lib/cuboid/processes/instances.rb', line 16

def initialize
    @list = {}
    @instance_connections = {}
end

Instance Attribute Details

#listArray<String> (readonly)

Returns URLs and tokens of all running Instances.

Returns:

  • (Array<String>)

    URLs and tokens of all running Instances.



14
15
16
# File 'lib/cuboid/processes/instances.rb', line 14

def list
  @list
end

Class Method Details

.method_missing(sym, *args, &block) ⇒ Object



209
210
211
212
213
214
215
# File 'lib/cuboid/processes/instances.rb', line 209

def self.method_missing( sym, *args, &block )
    if instance.respond_to?( sym )
        instance.send( sym, *args, &block )
    else
        super( sym, *args, &block )
    end
end

.respond_to?(m) ⇒ Boolean

Returns:

  • (Boolean)


217
218
219
# File 'lib/cuboid/processes/instances.rb', line 217

def self.respond_to?( m )
    super( m ) || instance.respond_to?( m )
end

Instance Method Details

#agent_spawnRPC::Client::Instance

Starts RPC::Server::Agent and returns an Instance.



163
164
165
166
# File 'lib/cuboid/processes/instances.rb', line 163

def agent_spawn
    info = Agents.spawn.spawn
    connect( info['url'], info['token'] )
end

#connect(url, token = nil) ⇒ RPC::Client::Instance

Connects to a Instance by URL.

Parameters:

  • url (String)

    URL of the Agent.

  • token (String) (defaults to: nil)

    Authentication token – only need be provided once.

Returns:



30
31
32
33
34
35
36
37
# File 'lib/cuboid/processes/instances.rb', line 30

def connect( url, token = nil )
    Raktr.global.run_in_thread if !Raktr.global.running?

    token ||= @list[url]
    @list[url] ||= token

    @instance_connections[url] ||= RPC::Client::Instance.new( url, token )
end

#each(&block) ⇒ Object

Parameters:

  • block (Block)

    Block to pass an RPC client for each Instance.



40
41
42
43
44
# File 'lib/cuboid/processes/instances.rb', line 40

def each( &block )
    @list.keys.each do |url|
        block.call connect( url )
    end
end

#grid_spawn(options = {}) ⇒ RPC::Client::Instance

Starts RPC::Server::Agent grid and returns a high-performance Instance.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :grid_size (Integer) — default: 3

    Amount of Agents to spawn.

Returns:



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/cuboid/processes/instances.rb', line 143

def grid_spawn(options = {} )
    options[:grid_size] ||= 3

    last_member = nil
    options[:grid_size].times do |i|
        last_member = Agents.spawn(
            peer: last_member ? last_member.url : last_member,
            pipe_id:   Utilities.available_port.to_s + Utilities.available_port.to_s
        )
    end

    info = nil
    info = last_member.spawn while !info && sleep( 0.1 )

    connect( info['url'], info['token'] )
end

#kill(url) ⇒ Object



168
169
170
171
172
173
174
175
176
177
# File 'lib/cuboid/processes/instances.rb', line 168

def kill( url )
    service = connect( url )

    pids = service.consumed_pids

    service.shutdown rescue nil
    Manager.kill_many pids

    @list.delete url
end

#killallObject

Kills all #list.



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/cuboid/processes/instances.rb', line 180

def killall
    pids = []
    each do |instance|
        begin
            Timeout.timeout 5 do
                pids |= instance.consumed_pids
            end
        rescue => e
            #ap e
            #ap e.backtrace
        end
    end

    each do |instance|
        begin
            Timeout.timeout 5 do
                instance.shutdown
            end
        rescue => e
            #ap e
            #ap e.backtrace
        end
    end

    @list.clear
    @instance_connections.clear
    Manager.kill_many pids
end

#spawn(options = {}, &block) ⇒ RPC::Client::Instance, Integer

Spawns an RPC::Server::Instance process.

Parameters:

  • options (Hash) (defaults to: {})

    To be passed to Options#set. Allows ‘address` instead of `rpc_server_address` and `port` instead of `rpc_port`.

Returns:



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/cuboid/processes/instances.rb', line 63

def spawn( options = {}, &block )
    options = options.dup
    token = options.delete(:token) || Utilities.generate_token
    fork  = options.delete(:fork)

    daemonize  = options.delete(:daemonize)
    detached   = options.delete(:detached)
    port_range = options.delete( :port_range )

    options[:ssl] ||= {
      server: {},
      client: {}
    }

    options = {
        rpc:    {
            server_socket:  options[:socket],
            server_port:    options[:port]    || Utilities.available_port( port_range ),
            server_address: options[:address] || '127.0.0.1',

            ssl_ca:                 options[:ssl][:ca],
            server_ssl_private_key: options[:ssl][:server][:private_key],
            server_ssl_certificate: options[:ssl][:server][:certificate],
            client_ssl_private_key: options[:ssl][:client][:private_key],
            client_ssl_certificate: options[:ssl][:client][:certificate],
        },
        paths: {
            application: options[:application]  || Options.paths.application
        }
    }

    url = nil
    if options[:rpc][:server_socket]
        url = options[:rpc][:server_socket]

        options[:rpc].delete :server_address
        options[:rpc].delete :server_port
    else
        url = "#{options[:rpc][:server_address]}:#{options[:rpc][:server_port]}"
    end

    pid = Manager.spawn(
      :instance,
      options:   options,
      token:     token,
      fork:      fork,
      daemonize: daemonize,
      detached:  detached
    )

    System.slots.use pid

    client = connect( url, token )
    client.pid = pid

    if block_given?
        client.when_ready do
            block.call client
        end
    else
        while sleep( 0.1 )
            begin
                client.alive?
                break
            rescue => e
                # ap "#{e.class}: #{e}"
                # ap e.backtrace
            end
        end

        client
    end
end

#token_for(client_or_url) ⇒ String

Returns Cached authentication token for the given Instance.

Parameters:

Returns:

  • (String)

    Cached authentication token for the given Instance.



51
52
53
# File 'lib/cuboid/processes/instances.rb', line 51

def token_for( client_or_url )
    @list[client_or_url.is_a?( String ) ? client_or_url : client_or_url.url ]
end