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:

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



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

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

Instance Attribute Details

#listArray<String> (readonly)



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

def list
  @list
end

Class Method Details

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



202
203
204
205
206
207
208
# File 'lib/cuboid/processes/instances.rb', line 202

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



210
211
212
# File 'lib/cuboid/processes/instances.rb', line 210

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.



155
156
157
158
# File 'lib/cuboid/processes/instances.rb', line 155

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.



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



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.

Options Hash (options):

  • :grid_size (Integer) — default: 3

    Amount of Agents to spawn.



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/cuboid/processes/instances.rb', line 135

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



160
161
162
163
164
165
166
167
168
169
170
# File 'lib/cuboid/processes/instances.rb', line 160

def kill( url )
    service = connect( url )
    service.consumed_pids do |pids|
        service.shutdown do
            # Make sure....
            Manager.kill_many pids
        end
    end

    @list.delete url
end

#killallObject

Kills all #list.



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/cuboid/processes/instances.rb', line 173

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.



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
# 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)
    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 )

    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



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