Class: Cuboid::Processes::Instances
- Includes:
- Utilities, Singleton
- Defined in:
- lib/cuboid/processes/instances.rb
Overview
Helper for managing RPC::Server::Instance processes.
Instance Attribute Summary collapse
-
#list ⇒ Array<String>
readonly
URLs and tokens of all running Instances.
Class Method Summary collapse
Instance Method Summary collapse
-
#agent_spawn ⇒ RPC::Client::Instance
Starts RPC::Server::Agent and returns an Instance.
-
#connect(url, token = nil) ⇒ RPC::Client::Instance
Connects to a Instance by URL.
- #each(&block) ⇒ Object
-
#grid_spawn(options = {}) ⇒ RPC::Client::Instance
Starts RPC::Server::Agent grid and returns a high-performance Instance.
-
#initialize ⇒ Instances
constructor
A new instance of Instances.
- #kill(url) ⇒ Object
-
#killall ⇒ Object
Kills all #list.
-
#spawn(options = {}, &block) ⇒ RPC::Client::Instance, Integer
Spawns an RPC::Server::Instance process.
-
#token_for(client_or_url) ⇒ String
Cached authentication token for the given Instance.
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
#initialize ⇒ Instances
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
#list ⇒ Array<String> (readonly)
Returns 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
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_spawn ⇒ RPC::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.
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( = {} ) [:grid_size] ||= 3 last_member = nil [: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 |
#killall ⇒ Object
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( = {}, &block ) = .dup token = .delete(:token) || Utilities.generate_token fork = .delete(:fork) daemonize = .delete(:daemonize) port_range = .delete( :port_range ) [:ssl] ||= { server: {}, client: {} } = { rpc: { server_socket: [:socket], server_port: [:port] || Utilities.available_port( port_range ), server_address: [:address] || '127.0.0.1', ssl_ca: [:ssl][:ca], server_ssl_private_key: [:ssl][:server][:private_key], server_ssl_certificate: [:ssl][:server][:certificate], client_ssl_private_key: [:ssl][:client][:private_key], client_ssl_certificate: [:ssl][:client][:certificate], }, paths: { application: [:application] || Options.paths.application } } url = nil if [:rpc][:server_socket] url = [:rpc][:server_socket] [:rpc].delete :server_address [:rpc].delete :server_port else url = "#{[:rpc][:server_address]}:#{[:rpc][:server_port]}" end pid = Manager.spawn( :instance, 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
Returns 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 |