Class: Cuboid::State::Application

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

Overview

State information for Framework.

Author:

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

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



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

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
145
# File 'lib/cuboid/state/application.rb', line 140

def aborted
    @abort = false
    @status = :aborted
    @running = false
    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.



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

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.



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

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



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

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.



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

def done?
    @status == :done
end

#dump(directory) ⇒ Object



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

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.



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

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.



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

def pause?
    @pause_signals.any?
end

#pausedObject

Signals that the system has been paused..



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

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.



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

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.



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

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.



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

def resume
    @status = :resuming
    @pause_signals.clear

    true
end

#resumedObject



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

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:



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

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.



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

def suspend?
    !!@suspend
end

#suspendedObject

Signals a completed suspension.



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

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.



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

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.



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

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