Class: Cuboid::RPC::Server::Instance

Inherits:
Object
  • Object
show all
Includes:
UI::Output, Utilities
Defined in:
lib/cuboid/rpc/server/instance.rb,
lib/cuboid/rpc/server/instance/peers.rb,
lib/cuboid/rpc/server/instance/service.rb

Overview

Author:

Defined Under Namespace

Modules: Service Classes: Peers

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

Methods included from UI::Output

#error_buffer, initialize, #log_error, #output_provider_file, #print_bad, #print_debug, #print_error, #print_info, #print_line, #print_ok, #print_status, #print_verbose, #reroute_to_file, #reroute_to_file?

Methods included from UI::OutputInterface

initialize

Methods included from UI::OutputInterface::Personalization

#included

Methods included from UI::OutputInterface::Controls

#debug?, #debug_level, #debug_level_1?, #debug_level_2?, #debug_level_3?, #debug_level_4?, #debug_off, #debug_on, initialize, #verbose?, #verbose_off, #verbose_on

Methods included from UI::OutputInterface::ErrorLogging

#error_logfile, #has_error_log?, initialize, #set_error_logfile

Methods included from UI::OutputInterface::Implemented

#print_debug_backtrace, #print_debug_exception, #print_debug_level_1, #print_debug_level_2, #print_debug_level_3, #print_debug_level_4, #print_error_backtrace, #print_exception

Methods included from UI::OutputInterface::Abstract

#output_provider_file, #print_bad, #print_debug, #print_error, #print_info, #print_line, #print_ok, #print_status, #print_verbose

Constructor Details

#initialize(options, token) ⇒ Instance

Initializes the RPC interface and the framework.

Parameters:

  • options (Options)
  • token (String)

    Authentication token.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/cuboid/rpc/server/instance.rb', line 45

def initialize( options, token )
    @options = options
    @token   = token

    @application    = Server::ApplicationWrapper.new(
      Cuboid::Application.application
    )
    @active_options = Server::ActiveOptions.new

    @server = Base.new( @options.rpc.to_server_options, token )

    if @options.datastore.log_level
        @server.logger.level = @options.datastore.log_level
    end

    @options.datastore.token = token

    if @options.output.reroute_to_logfile
        reroute_to_file "#{@options.paths.logs}Instance-#{Process.pid}-#{@options.rpc.server_port}.log"
    else
        reroute_to_file false
    end

    set_error_logfile "#{@options.paths.logs}Instance-#{Process.pid}-#{@options.rpc.server_port}.error.log"

    set_handlers( @server )

    # trap interrupts and exit cleanly when required
    %w(QUIT INT).each do |signal|
        next if !Signal.list.has_key?( signal )
        trap( signal ){ shutdown if !@options.datastore.do_not_trap }
    end

    Raktr.global.run do
        _run
    end
end

Class Method Details

.parse_progress_opts(options, key) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/cuboid/rpc/server/instance.rb', line 256

def self.parse_progress_opts( options, key )
    parsed = {}
    [options.delete( key ) || options.delete( key.to_s )].compact.each do |w|
        case w
            when Array
                w.compact.flatten.each do |q|
                    case q
                        when String, Symbol
                            parsed[q.to_sym] = nil

                        when Hash
                            parsed.merge!( q.my_symbolize_keys )
                    end
                end

            when String, Symbol
                parsed[w.to_sym] = nil

            when Hash
                parsed.merge!( w.my_symbolize_keys )
        end
    end

    parsed
end

Instance Method Details

#abort_and_generate_reportHash

Cleans up and returns the report.

Returns:

See Also:

  • #report


129
130
131
132
# File 'lib/cuboid/rpc/server/instance.rb', line 129

def abort_and_generate_report
    @application.abort!
    generate_report
end

#agent_urlString?

Returns Agent URL that provided this Instance, ‘nil` if not provided by a Agent.

Returns:

  • (String, nil)

    Agent URL that provided this Instance, ‘nil` if not provided by a Agent.



96
97
98
# File 'lib/cuboid/rpc/server/instance.rb', line 96

def agent_url
    @options.agent.url
end

#alive?true

Returns:

  • (true)


114
115
116
# File 'lib/cuboid/rpc/server/instance.rb', line 114

def alive?
    @server.alive?
end

#applicationObject



83
84
85
# File 'lib/cuboid/rpc/server/instance.rb', line 83

def application
    Application.application.to_s
end

#busy?Bool

Returns ‘true` if the scan is initializing or running, `false` otherwise.

Returns:

  • (Bool)

    ‘true` if the scan is initializing or running, `false` otherwise.



120
121
122
# File 'lib/cuboid/rpc/server/instance.rb', line 120

def busy?
    @run_initializing || @application.busy?
end

#consumed_pidsObject



252
253
254
# File 'lib/cuboid/rpc/server/instance.rb', line 252

def consumed_pids
    [Process.pid]
end

#error_test(str) ⇒ Object



247
248
249
# File 'lib/cuboid/rpc/server/instance.rb', line 247

def error_test( str )
    @application.error_test( str )
end

#errors(starting_line = 0) ⇒ Object



242
243
244
# File 'lib/cuboid/rpc/server/instance.rb', line 242

def errors( starting_line = 0 )
    @application.errors( starting_line )
end

#generate_reportHash



136
137
138
# File 'lib/cuboid/rpc/server/instance.rb', line 136

def generate_report
    @application.generate_report.to_rpc_data
end

#progress(options = {}) ⇒ Hash

# Recommended usage

Please request from the method only the things you are going to actually
use, otherwise you'll just be wasting bandwidth.
In addition, ask to **not** be served data you already have, like
error messages.

To be kept completely up to date on the progress of a scan (i.e. receive
new issues and error messages asap) in an efficient manner, you will need
to keep track of the error messages you already have and explicitly tell
the method to not send the same data back to you on subsequent calls.

## Retrieving errors (‘:errors` option) without duplicate data

This is done by telling the method how many error messages you already
have and you will be served the errors from the error-log that are past
that line.
So, if you were to use a loop to get fresh progress data it would look
like so:

  error_cnt = 0
  i = 0
  while sleep 1
      # Test method, triggers an error log...
      instance.error_test "BOOM! #{i+=1}"

      # Only request errors we don't already have
      errors = instance.progress( with: { errors: error_cnt } )[:errors]
      error_cnt += errors.size

      # You will only see new errors
      puts errors.join("\n")
  end

Parameters:

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

    Options about what progress data to retrieve and return.

Options Hash (options):

  • :with (Array<Symbol, Hash>)

    Specify data to include:

    • :errors – Errors and the line offset to use for #errors. Pass as a hash, like: ‘{ errors: 10 }`

  • :without (Array<Symbol, Hash>)

    Specify data to exclude:

    • :statistics – Don’t include runtime statistics.

Returns:

  • (Hash)
    • ‘statistics` – General runtime statistics (merged when part of Grid)

      (enabled by default)
      
    • ‘status` – #status

    • ‘busy` – #busy?

    • ‘errors` – #errors (disabled by default)



192
193
194
# File 'lib/cuboid/rpc/server/instance.rb', line 192

def progress( options = {} )
    progress_handler( options.merge( as_hash: true ) )
end

#restore!(snapshot) ⇒ Object

See Also:

  • #suspend
  • #snapshot_path


105
106
107
108
109
110
111
# File 'lib/cuboid/rpc/server/instance.rb', line 105

def restore!( snapshot )
    Thread.new do
        @application.restore!( snapshot ).run
    end

    true
end

#run(options = nil) ⇒ Object

Configures and runs a job.



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/cuboid/rpc/server/instance.rb', line 197

def run( options = nil )
    # If the instance isn't clean bail out now.
    return false if busy? || @called

    if !@application.valid_options?( options )
        fail ArgumentError, 'Invalid options!'
    end

    # There may be follow-up/retry calls by the client in cases of network
    # errors (after the request has reached us) so we need to keep minimal
    # track of state in order to bail out on subsequent calls.
    @called = @run_initializing = true

    @active_options.set( application: options )

    Thread.new do
        @application.run
        @run_initializing = false
    end

    true
end

#scheduler_urlString?

Returns Scheduler URL to which this Instance is attached, ‘nil` if not attached.

Returns:

  • (String, nil)

    Scheduler URL to which this Instance is attached, ‘nil` if not attached.



89
90
91
# File 'lib/cuboid/rpc/server/instance.rb', line 89

def scheduler_url
    @options.scheduler.url
end

#shutdown(&block) ⇒ Object

Makes the server go bye-bye…Lights out!



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/cuboid/rpc/server/instance.rb', line 221

def shutdown( &block )
    if @shutdown
        block.call if block_given?
        return
    end
    @shutdown = true

    print_status 'Shutting down...'

    @application.shutdown

    # We're shutting down services so we need to use a concurrent way but
    # without going through the Reactor.
    Thread.new do
        @server.shutdown
        block.call true if block_given?
    end

    true
end