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



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

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?.

Returns:

  • (Bool)

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

Raises:

  • (StateNotAbortable)

    When not #running?.



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/cuboid/state/application.rb', line 119

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.



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

def abort?
    !!@abort
end

#abortedObject

Signals a completed abort operation.



140
141
142
143
144
# File 'lib/cuboid/state/application.rb', line 140

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.



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

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.



154
155
156
# File 'lib/cuboid/state/application.rb', line 154

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



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

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.



160
161
162
# File 'lib/cuboid/state/application.rb', line 160

def done?
    @status == :done
end

#dump(directory) ⇒ Object



280
281
282
283
284
285
# File 'lib/cuboid/state/application.rb', line 280

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.



224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/cuboid/state/application.rb', line 224

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.



257
258
259
# File 'lib/cuboid/state/application.rb', line 257

def pause?
    @pause_signals.any?
end

#pausedObject

Signals that the system has been paused..



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

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.



245
246
247
# File 'lib/cuboid/state/application.rb', line 245

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.



251
252
253
# File 'lib/cuboid/state/application.rb', line 251

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.



266
267
268
269
270
271
# File 'lib/cuboid/state/application.rb', line 266

def resume
    @status = :resuming
    @pause_signals.clear

    true
end

#resumedObject



273
274
275
276
277
278
# File 'lib/cuboid/state/application.rb', line 273

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:



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/cuboid/state/application.rb', line 174

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.



194
195
196
# File 'lib/cuboid/state/application.rb', line 194

def suspend?
    !!@suspend
end

#suspendedObject

Signals a completed suspension.



199
200
201
202
203
# File 'lib/cuboid/state/application.rb', line 199

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.



207
208
209
# File 'lib/cuboid/state/application.rb', line 207

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.



213
214
215
# File 'lib/cuboid/state/application.rb', line 213

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