Class: Cuboid::Application

Inherits:
Object show all
Includes:
Support::Mixins::Observable, Support::Mixins::Parts, Support::Mixins::SpecInstances, UI::Output, Utilities, Singleton
Defined in:
lib/cuboid/application.rb,
lib/cuboid/application/runtime.rb,
lib/cuboid/application/parts/data.rb,
lib/cuboid/application/parts/state.rb,
lib/cuboid/application/parts/report.rb

Overview

Author:

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

Defined Under Namespace

Modules: Parts, PrependMethods Classes: Error, Runtime

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Support::Mixins::Parts

included

Methods included from Support::Mixins::Observable

included

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

Methods included from Support::Mixins::SpecInstances

prepended

Constructor Details

#initializeApplication

Returns a new instance of Application.



263
264
265
266
267
# File 'lib/cuboid/application.rb', line 263

def initialize
    super

    @runtime = Runtime.new
end

Class Method Details

._spec_instance_cleanup(i) ⇒ Object



306
307
308
309
# File 'lib/cuboid/application.rb', line 306

def self._spec_instance_cleanup( i )
    # i.clean_up
    i.reset
end

.agent_service_for(name, service) ⇒ Object



135
136
137
# File 'lib/cuboid/application.rb', line 135

def agent_service_for( name, service )
    agent_services[name] = service
end

.agent_servicesObject



139
140
141
# File 'lib/cuboid/application.rb', line 139

def agent_services
    @agent_services ||= {}
end

.applicationObject



223
224
225
# File 'lib/cuboid/application.rb', line 223

def application
    @application
end

.application=(app) ⇒ Object



227
228
229
# File 'lib/cuboid/application.rb', line 227

def application=( app )
    @application = app
end

.connect(info) ⇒ Object



211
212
213
214
# File 'lib/cuboid/application.rb', line 211

def connect( info )
    info = info.my_symbolize_keys
    Processes::Instances.connect( info[:url], info[:token] )
end

.handler_for(signal, handler) ⇒ Object



143
144
145
# File 'lib/cuboid/application.rb', line 143

def handler_for( signal, handler )
    signal_handlers[signal] = handler
end

.inherited(application) ⇒ Object



216
217
218
219
220
221
# File 'lib/cuboid/application.rb', line 216

def inherited( application )
    super

    application.prepend PrependMethods
    self.application = application
end

.instance_service_for(name, service) ⇒ Object



119
120
121
# File 'lib/cuboid/application.rb', line 119

def instance_service_for( name, service )
    instance_services[name] = service
end

.instance_servicesObject



123
124
125
# File 'lib/cuboid/application.rb', line 123

def instance_services
    @instance_services ||= {}
end

.max_coresObject



99
100
101
# File 'lib/cuboid/application.rb', line 99

def max_cores
    @max_cores ||= 0
end

.max_diskObject



115
116
117
# File 'lib/cuboid/application.rb', line 115

def max_disk
    @max_disk ||= 0
end

.max_memoryObject



107
108
109
# File 'lib/cuboid/application.rb', line 107

def max_memory
    @max_memory ||= 0
end

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



231
232
233
234
235
236
237
# File 'lib/cuboid/application.rb', line 231

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

.provision_cores(cores) ⇒ Object



95
96
97
# File 'lib/cuboid/application.rb', line 95

def provision_cores( cores )
    @max_cores = cores
end

.provision_disk(disk) ⇒ Object



111
112
113
# File 'lib/cuboid/application.rb', line 111

def provision_disk( disk )
    @max_disk = disk
end

.provision_memory(ram) ⇒ Object



103
104
105
# File 'lib/cuboid/application.rb', line 103

def provision_memory( ram )
    @max_memory = ram
end

.respond_to?(*args) ⇒ Boolean

Returns:

  • (Boolean)


239
240
241
# File 'lib/cuboid/application.rb', line 239

def respond_to?( *args )
    super || instance.respond_to?( *args )
end

.rest_service_for(name, service) ⇒ Object



127
128
129
# File 'lib/cuboid/application.rb', line 127

def rest_service_for( name, service )
    rest_services[name] = service
end

.rest_servicesObject



131
132
133
# File 'lib/cuboid/application.rb', line 131

def rest_services
    @rest_services ||= {}
end

.serialize_with(serializer) ⇒ Object



151
152
153
# File 'lib/cuboid/application.rb', line 151

def serialize_with( serializer)
    @serializer = serializer
end

.serializerObject



155
156
157
158
159
160
# File 'lib/cuboid/application.rb', line 155

def serializer
    @serializer ||= nil

    # Default to JSON for REST API compatibility.
    @serializer || JSON
end

.signal_handlersObject



147
148
149
# File 'lib/cuboid/application.rb', line 147

def signal_handlers
    @signal_handlers ||= {}
end

.source_locationObject



175
176
177
178
179
180
181
182
183
184
185
# File 'lib/cuboid/application.rb', line 175

def source_location
    splits = self.to_s.split ( '::' )
    app    = splits.pop

    last_const = Object
    splits.each do |const_name|
        last_const = last_const.const_get( const_name.to_sym )
    end

    File.expand_path last_const.const_source_location( app.to_sym ).first
end

.spawn(type, options = {}, &block) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/cuboid/application.rb', line 187

def spawn( type, options = {}, &block )
    const = nil

    case type
    when :instance
        const = :Instances
    when :agent
        const = :Agents
    when :scheduler
        const = :Schedulers
    when :rest
        return Processes::Manager.spawn(
            :rest_service,
            options.merge( options: { paths: { application: source_location } } ),
            &block
        )
    end

    Processes.const_get( const ).spawn(
      options.merge( application: source_location ),
      &block
    )
end

.valid_options?(options) ⇒ Boolean

Returns:

  • (Boolean)


166
167
168
169
170
171
172
173
# File 'lib/cuboid/application.rb', line 166

def valid_options?( options )
    @validate_options_with ||= nil
    if @validate_options_with
        return instance.method( @validate_options_with ).call( options )
    end

    true
end

.validate_options_with(handler) ⇒ Object



162
163
164
# File 'lib/cuboid/application.rb', line 162

def validate_options_with( handler )
    @validate_options_with = handler
end

Instance Method Details

#applicationObject



315
316
317
# File 'lib/cuboid/application.rb', line 315

def application
    Cuboid::Application.application
end

#inspectObject



292
293
294
295
296
297
298
# File 'lib/cuboid/application.rb', line 292

def inspect
    stats = statistics

    s = "#<#{self.class} (#{status}) "
    s << "runtime=#{stats[:runtime]} "
    s << '>'
end

#optionsObject



277
278
279
# File 'lib/cuboid/application.rb', line 277

def options
    Options.application
end

#options=(opts) ⇒ Object



273
274
275
# File 'lib/cuboid/application.rb', line 273

def options=( opts )
    Options.application = opts
end

#runtimeObject



269
270
271
# File 'lib/cuboid/application.rb', line 269

def runtime
    @runtime
end

#safe(&block) ⇒ Object

Raises:

  • (ArgumentError)


323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/cuboid/application.rb', line 323

def safe( &block )
    raise ArgumentError, 'Missing block.' if !block_given?

    begin
        block.call self
    ensure
        clean_up
        reset
    end

    nil
end

#serializerObject



319
320
321
# File 'lib/cuboid/application.rb', line 319

def serializer
    self.class.serializer
end

#statisticsHash

Returns Framework statistics:

  • ‘:runtime` – Scan runtime in seconds.

Returns:

  • (Hash)

    Framework statistics:

    • ‘:runtime` – Scan runtime in seconds.



286
287
288
289
290
# File 'lib/cuboid/application.rb', line 286

def statistics
    {
        runtime: @start_datetime ? (@finish_datetime || Time.now) - @start_datetime : 0,
    }
end

#unsafeObject



311
312
313
# File 'lib/cuboid/application.rb', line 311

def unsafe
    self
end

#versionString

Returns the version of the framework.

Returns:

  • (String)

    Returns the version of the framework.



302
303
304
# File 'lib/cuboid/application.rb', line 302

def version
    Cuboid::VERSION
end