Class: Cuboid::State::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/cuboid/state/application.rb

Overview

State information for Framework.

Author:

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApplication

Returns a new instance of Application.



39
40
41
42
43
44
45
46
# File 'lib/cuboid/state/application.rb', line 39

def initialize
    @running = false
    @pre_pause_status = nil

    @pause_signals = Set.new

    @status_messages = []
end

Instance Attribute Details

#runningBool

Returns:

  • (Bool)


32
33
34
# File 'lib/cuboid/state/application.rb', line 32

def running
  @running
end

#runtimeObject

Returns the value of attribute runtime.



37
38
39
# File 'lib/cuboid/state/application.rb', line 37

def runtime
  @runtime
end

#statusSymbol

Returns:

  • (Symbol)


29
30
31
# File 'lib/cuboid/state/application.rb', line 29

def status
  @status
end

#status_messagesArray<String> (readonly)

Returns:



35
36
37
# File 'lib/cuboid/state/application.rb', line 35

def status_messages
  @status_messages
end

Class Method Details

.load(directory) ⇒ Object



291
292
293
294
295
# File 'lib/cuboid/state/application.rb', line 291

def self.load( directory )
    application = new
    application.runtime = Cuboid::Application.serializer.load( IO.binread( "#{directory}/runtime" ) )
    application
end

Instance Method Details

#abortBool

Returns ‘true` if the abort request was successful, `false` if the system is already #suspended? or is #suspending?.

Parameters:

  • block (Bool)

    ‘true` if the method should block until an abortion has completed, `false` otherwise.

Returns:

  • (Bool)

    ‘true` if the abort request was successful, `false` if the system is already #suspended? or is #suspending?.

Raises:

  • (StateNotAbortable)

    When not #running?.



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/cuboid/state/application.rb', line 123

def abort
    return false if aborting? || aborted?

    if !running?
        fail Error::StateNotAbortable, "Cannot abort idle state: #{status}"
    end

    set_status_message :aborting
    @status = :aborting
    @abort = true

    true
end

#abort?Bool

Returns ‘true` if a #abort signal is in place , `false` otherwise.

Returns:

  • (Bool)

    ‘true` if a #abort signal is in place , `false` otherwise.



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

def abort?
    !!@abort
end

#abortedObject

Signals a completed abort operation.



144
145
146
147
148
# File 'lib/cuboid/state/application.rb', line 144

def aborted
    @abort = false
    @status = :aborted
    nil
end

#aborted?Bool

Returns ‘true` if the system has been aborted, `false` otherwise.

Returns:

  • (Bool)

    ‘true` if the system has been aborted, `false` otherwise.



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

def aborted?
    @status == :aborted
end

#aborting?Bool

Returns ‘true` if the system is being aborted, `false` otherwise.

Returns:

  • (Bool)

    ‘true` if the system is being aborted, `false` otherwise.



158
159
160
# File 'lib/cuboid/state/application.rb', line 158

def aborting?
    @status == :aborting
end

#add_status_message(message, *sprintf) ⇒ Object

Pushes a message to #status_messages.

Parameters:

  • message (String, Symbol)

    Status message. If ‘Symbol`, it will be grabbed from #available_status_messages.

  • sprintf (String, Numeric)

    ‘sprintf` arguments.



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/cuboid/state/application.rb', line 82

def add_status_message( message, *sprintf )
    if message.is_a? Symbol
        if !available_status_messages.include?( message )
            fail Error::InvalidStatusMessage,
                 "Could not find status message for: '#{message}'"
        end

        message = available_status_messages[message] % sprintf
    end

    @status_messages << message.to_s
end

#available_status_messagesHash{Symbol=>String}

Returns All possible #status_messages by type.

Returns:



56
57
58
59
60
61
62
63
64
# File 'lib/cuboid/state/application.rb', line 56

def available_status_messages
    {
        suspending:        'Will suspend as soon as the current page is audited.',
        saving_snapshot:   'Saving snapshot at: %s',
        snapshot_location: 'Snapshot location: %s',
        aborting:          'Aborting the scan.',
        timed_out:         'Scan timed out.'
    }
end

#clearObject



297
298
299
300
301
302
303
304
# File 'lib/cuboid/state/application.rb', line 297

def clear
    @pause_signals.clear

    @running = false
    @pre_pause_status = nil

    @runtime = nil
end

#clear_status_messagesObject



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

def clear_status_messages
    @status_messages.clear
end

#done?Bool

Returns ‘true` if the system has completed successfully, `false` otherwise.

Returns:

  • (Bool)

    ‘true` if the system has completed successfully, `false` otherwise.



164
165
166
# File 'lib/cuboid/state/application.rb', line 164

def done?
    @status == :done
end

#dump(directory) ⇒ Object



284
285
286
287
288
289
# File 'lib/cuboid/state/application.rb', line 284

def dump( directory )
    FileUtils.mkdir_p( directory )

    d = Cuboid::Application.serializer.dump( @runtime )
    IO.binwrite( "#{directory}/runtime", d )
end

#pauseTrueClass

Returns Pauses the framework on a best effort basis, might take a while to take effect.

Parameters:

  • block (Bool)

    ‘true` if the method should block until the pause has completed, `false` otherwise.

Returns:

  • (TrueClass)

    Pauses the framework on a best effort basis, might take a while to take effect.



228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/cuboid/state/application.rb', line 228

def pause
    @pre_pause_status ||= @status if !paused? && !pausing?

    if !paused?
        @status = :pausing
    end

    @pause_signals << :nil

    paused if !running?
    true
end

#pause?Bool

Returns ‘true` if the framework should pause, `false` otherwise.

Returns:

  • (Bool)

    ‘true` if the framework should pause, `false` otherwise.



261
262
263
# File 'lib/cuboid/state/application.rb', line 261

def pause?
    @pause_signals.any?
end

#pausedObject

Signals that the system has been paused..



242
243
244
245
# File 'lib/cuboid/state/application.rb', line 242

def paused
    clear_status_messages
    @status = :paused
end

#paused?Bool

Returns ‘true` if the framework is paused.

Returns:

  • (Bool)

    ‘true` if the framework is paused.



249
250
251
# File 'lib/cuboid/state/application.rb', line 249

def paused?
    @status == :paused
end

#pausing?Bool

Returns ‘true` if the system is being paused, `false` otherwise.

Returns:

  • (Bool)

    ‘true` if the system is being paused, `false` otherwise.



255
256
257
# File 'lib/cuboid/state/application.rb', line 255

def pausing?
    @status == :pausing
end

#resumeBool

Resumes a paused system

Returns:

  • (Bool)

    ‘true` if the system is resumed, `false` if there are more #pause signals pending.



270
271
272
273
274
275
# File 'lib/cuboid/state/application.rb', line 270

def resume
    @status = :resuming
    @pause_signals.clear

    true
end

#resumedObject



277
278
279
280
281
282
# File 'lib/cuboid/state/application.rb', line 277

def resumed
    @status = @pre_pause_status
    @pre_pause_status = nil

    true
end

#running?Boolean

Returns:

  • (Boolean)


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

def running?
    !!@running
end

#set_status_message(*args) ⇒ Object

Sets a message as #status_messages.

Parameters:

  • message (String, Symbol)

    Status message. If ‘Symbol`, it will be grabbed from #available_status_messages.

  • sprintf (String, Numeric)

    ‘sprintf` arguments.



70
71
72
73
# File 'lib/cuboid/state/application.rb', line 70

def set_status_message( *args )
    clear_status_messages
    add_status_message( *args )
end

#statisticsObject



48
49
50
51
52
# File 'lib/cuboid/state/application.rb', line 48

def statistics
    {
      runtime: !!@runtime
    }
end

#suspendBool

Returns ‘true` if the suspend request was successful, `false` if the system is already #suspended? or is #suspending?.

Parameters:

  • block (Bool)

    ‘true` if the method should block until a suspend has completed, `false` otherwise.

Returns:

  • (Bool)

    ‘true` if the suspend request was successful, `false` if the system is already #suspended? or is #suspending?.

Raises:



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/cuboid/state/application.rb', line 178

def suspend
    return false if suspending? || suspended?

    if paused? || pausing?
        fail Error::StateNotSuspendable, 'Cannot suspend a paused state.'
    end

    if !running?
        fail Error::StateNotSuspendable, "Cannot suspend idle state: #{status}"
    end

    set_status_message :suspending
    @status = :suspending
    @suspend = true

    true
end

#suspend?Bool

Returns ‘true` if an #abort signal is in place , `false` otherwise.

Returns:

  • (Bool)

    ‘true` if an #abort signal is in place , `false` otherwise.



198
199
200
# File 'lib/cuboid/state/application.rb', line 198

def suspend?
    !!@suspend
end

#suspendedObject

Signals a completed suspension.



203
204
205
206
207
# File 'lib/cuboid/state/application.rb', line 203

def suspended
    @suspend = false
    @status = :suspended
    nil
end

#suspended?Bool

Returns ‘true` if the system has been suspended, `false` otherwise.

Returns:

  • (Bool)

    ‘true` if the system has been suspended, `false` otherwise.



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

def suspended?
    @status == :suspended
end

#suspending?Bool

Returns ‘true` if the system is being suspended, `false` otherwise.

Returns:

  • (Bool)

    ‘true` if the system is being suspended, `false` otherwise.



217
218
219
# File 'lib/cuboid/state/application.rb', line 217

def suspending?
    @status == :suspending
end

#timed_outObject



104
105
106
107
# File 'lib/cuboid/state/application.rb', line 104

def timed_out
    @status = :timed_out
    nil
end

#timed_out?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/cuboid/state/application.rb', line 109

def timed_out?
    @status == :timed_out
end