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

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

Overview

Author:

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.



42
43
44
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
# File 'lib/cuboid/rpc/server/instance.rb', line 42

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



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/cuboid/rpc/server/instance.rb', line 248

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


125
126
127
128
# File 'lib/cuboid/rpc/server/instance.rb', line 125

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.



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

def agent_url
    @options.agent.url
end

#alive?true

Returns:

  • (true)


110
111
112
# File 'lib/cuboid/rpc/server/instance.rb', line 110

def alive?
    @server.alive?
end

#applicationObject



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

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.



116
117
118
# File 'lib/cuboid/rpc/server/instance.rb', line 116

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

#consumed_pidsObject



244
245
246
# File 'lib/cuboid/rpc/server/instance.rb', line 244

def consumed_pids
    [Process.pid]
end

#error_test(str) ⇒ Object



239
240
241
# File 'lib/cuboid/rpc/server/instance.rb', line 239

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

#errors(starting_line = 0) ⇒ Object



234
235
236
# File 'lib/cuboid/rpc/server/instance.rb', line 234

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

#generate_reportHash



132
133
134
# File 'lib/cuboid/rpc/server/instance.rb', line 132

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)



188
189
190
# File 'lib/cuboid/rpc/server/instance.rb', line 188

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

#restore!(snapshot) ⇒ Object

See Also:

  • #suspend
  • #snapshot_path


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

def restore!( snapshot )
    @application.restore!( snapshot ).run
    true
end

#run(options = nil) ⇒ Object

Configures and runs a job.



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/cuboid/rpc/server/instance.rb', line 193

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 )

    @application.run
    @run_initializing = false

    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.



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

def scheduler_url
    @options.scheduler.url
end

#shutdown(&block) ⇒ Object

Makes the server go bye-bye…Lights out!



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/cuboid/rpc/server/instance.rb', line 215

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

    print_status 'Shutting down...'

    # 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