Module: Cuboid::Application::Parts::State

Defined in:
lib/cuboid/application/parts/state.rb

Overview

Provides access to State::Application and helpers.

Author:

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

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



10
11
12
# File 'lib/cuboid/application/parts/state.rb', line 10

def self.included( base )
    base.extend ClassMethods
end

Instance Method Details

#abort!Object

Aborts the Application#run on a best effort basis.



201
202
203
204
205
206
207
208
209
# File 'lib/cuboid/application/parts/state.rb', line 201

def abort!
    state.abort

    if handler = self.class.signal_handlers[:abort]
        method( handler ).call
    end

    state.aborted
end

#abort?Bool

Returns ‘true` if the framework has been instructed to abort (i.e. is in the process of being aborted or has been aborted), `false` otherwise.

Returns:

  • (Bool)

    ‘true` if the framework has been instructed to abort (i.e. is in the process of being aborted or has been aborted), `false` otherwise.



190
191
192
# File 'lib/cuboid/application/parts/state.rb', line 190

def abort?
    state.abort?
end

#aborted?Bool

Returns ‘true` if the Application#run has been aborted, `false` otherwise.

Returns:

  • (Bool)

    ‘true` if the Application#run has been aborted, `false` otherwise.



183
184
185
# File 'lib/cuboid/application/parts/state.rb', line 183

def aborted?
    state.aborted?
end

#aborting?Bool

Returns ‘true` if the framework is in the process of aborting, `false` otherwise.

Returns:

  • (Bool)

    ‘true` if the framework is in the process of aborting, `false` otherwise.



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

def aborting?
    state.aborting?
end

#done?Bool

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

Returns:

  • (Bool)

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



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

def done?
    state.done?
end

#initializeObject



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

def initialize
    super

    state.status = :ready
end

#pause!Integer

Note:

Each call from a unique caller is counted as a pause request and in order for the system to resume all pause callers need to #resume it.

Pauses the framework on a best effort basis.

Returns:

  • (Integer)

    ID identifying this pause request.



169
170
171
172
173
174
175
176
177
178
179
# File 'lib/cuboid/application/parts/state.rb', line 169

def pause!
    state.pause

    if handler = self.class.signal_handlers[:pause]
        method( handler ).call
    end

    state.paused

    nil
end

#pause?Bool

Returns ‘true` if the framework has been instructed to pause (i.e. is in the process of being paused or has been paused), `false` otherwise.

Returns:

  • (Bool)

    ‘true` if the framework has been instructed to pause (i.e. is in the process of being paused or has been paused), `false` otherwise.



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

def pause?
    state.pause?
end

#paused?Bool

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

Returns:

  • (Bool)

    ‘true` if the framework is paused, `false` otherwise.



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

def paused?
    state.paused?
end

#pausing?Bool

Returns ‘true` if the framework is in the process of pausing, `false` otherwise.

Returns:

  • (Bool)

    ‘true` if the framework is in the process of pausing, `false` otherwise.



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

def pausing?
    state.pausing?
end

#resetObject

Note:

Prefer this from #reset if you already have an instance.

Note:

You should first reset Options.

Resets everything and allows the framework to be re-used.



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/cuboid/application/parts/state.rb', line 70

def reset
    @state_archive   = nil
    @cleaned_up      = false
    @start_datetime  = nil
    @finish_datetime = nil

    # This needs to happen before resetting the other components so they
    # will be able to put in their hooks.
    self.class.reset

    clear_observers
end

#restore!(ses) ⇒ Application

Returns Restored instance.

Parameters:

  • ses (String)

    Path to an ‘.ses.` (Cuboid Application Snapshot) file created by #suspend.

Returns:



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/cuboid/application/parts/state.rb', line 93

def restore!( ses )
    if handler = self.class.signal_handlers[:restore]
        method( handler ).call
    end

    Snapshot.load ses

    state.status = :restored

    self
end

#resume!Object



211
212
213
214
215
216
217
218
219
# File 'lib/cuboid/application/parts/state.rb', line 211

def resume!
    state.resume

    if handler = self.class.signal_handlers[:resume]
        method( handler ).call
    end

    state.resumed
end

#running?Bool

Returns ‘true` if the framework is running, `false` otherwise. This is `true` even if the scan is #paused?.

Returns:

  • (Bool)

    ‘true` if the framework is running, `false` otherwise. This is `true` even if the scan is #paused?.



133
134
135
# File 'lib/cuboid/application/parts/state.rb', line 133

def running?
    state.running?
end

#snapshot_pathString

Returns Provisioned #suspend dump file for this instance.

Returns:

  • (String)

    Provisioned #suspend dump file for this instance.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cuboid/application/parts/state.rb', line 48

def snapshot_path
    return @state_archive if @state_archive

    default_filename =
        "Cuboid #{Time.now.to_s.gsub( ':', '_' )} " <<
            "#{generate_token}.#{Snapshot::EXTENSION}"

    location = Cuboid::Options.snapshot.path

    if !location
        location = default_filename
    elsif File.directory? location
        location += "/#{default_filename}"
    end

    @state_archive ||= File.expand_path( location )
end

#stateState::Application

Returns:



84
85
86
# File 'lib/cuboid/application/parts/state.rb', line 84

def state
    Cuboid::State.application
end

#statusSymbol

Returns Status of the instance, possible values are (in order):

  • ‘:ready` – Initialised and waiting for instructions.

  • ‘:preparing` – Getting ready to start.

  • ‘:pausing` – The instance is being paused (if applicable).

  • ‘:paused` – The instance has been paused (if applicable).

  • ‘:suspending` – The instance is being suspended (if applicable).

  • ‘:suspended` – The instance has being suspended (if applicable).

  • ‘:cleanup` – The instance is done and cleaning up.

  • ‘:aborted` – The scan has been Application::Parts::State#abort, you can grab the

    report and shutdown.
    
  • ‘:done` – The scan has completed, you can grab the report and shutdown.

  • ‘:timed_out` – The scan was aborted due to a time-out..

Returns:

  • (Symbol)

    Status of the instance, possible values are (in order):

    • ‘:ready` – Initialised and waiting for instructions.

    • ‘:preparing` – Getting ready to start.

    • ‘:pausing` – The instance is being paused (if applicable).

    • ‘:paused` – The instance has been paused (if applicable).

    • ‘:suspending` – The instance is being suspended (if applicable).

    • ‘:suspended` – The instance has being suspended (if applicable).

    • ‘:cleanup` – The instance is done and cleaning up.

    • ‘:aborted` – The scan has been Application::Parts::State#abort, you can grab the

      report and shutdown.
      
    • ‘:done` – The scan has completed, you can grab the report and shutdown.

    • ‘:timed_out` – The scan was aborted due to a time-out..



126
127
128
# File 'lib/cuboid/application/parts/state.rb', line 126

def status
    state.status
end

#status_messagesArray<String>

Returns Messages providing more information about the current #status of the framework.

Returns:

  • (Array<String>)

    Messages providing more information about the current #status of the framework.



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

def status_messages
    state.status_messages
end

#suspend!String?

Writes a Snapshot.dump to disk and aborts the scan.

Returns:

  • (String, nil)

    Path to the state file ‘wait` is `true`, `nil` otherwise.



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

def suspend!
    state.suspend

    if handler = self.class.signal_handlers[:suspend]
        method( handler ).call
    end

    suspend_to_disk
    state.suspended

    snapshot_path
end

#suspend?Bool

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

Returns:

  • (Bool)

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



241
242
243
# File 'lib/cuboid/application/parts/state.rb', line 241

def suspend?
    state.suspend?
end

#suspended?Bool

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

Returns:

  • (Bool)

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



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

def suspended?
    state.suspended?
end