Module: Dopi::State

Includes:
Observable
Included in:
Command, CommandSet, Plan, Step, StepSet
Defined in:
lib/dopi/state.rb

Instance Method Summary collapse

Instance Method Details

#delete_on_signal(a_proc) ⇒ Object



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

def delete_on_signal(a_proc)
  signal_procs.delete(a_proc)
end

#on_signal(a_proc) ⇒ Object



209
210
211
# File 'lib/dopi/state.rb', line 209

def on_signal(a_proc)
  signal_procs << a_proc
end

#reset_signalsObject



200
201
202
203
# File 'lib/dopi/state.rb', line 200

def reset_signals
  @signals = Hash.new(false)
  state_children.each {|child| child.reset_signals}
end

#send_signal(signal) ⇒ Object



217
218
219
220
221
222
223
# File 'lib/dopi/state.rb', line 217

def send_signal(signal)
  unless signals[signal] == true
    signal_procs.each {|p| p.call(signal)}
    state_children.each {|child| child.send_signal(signal)}
    signals[signal] = true
  end
end

#signal_procsObject



205
206
207
# File 'lib/dopi/state.rb', line 205

def signal_procs
  @signal_procs ||= []
end

#signalsObject



196
197
198
# File 'lib/dopi/state.rb', line 196

def signals
  @signals ||= Hash.new(false)
end

#stateObject



15
16
17
# File 'lib/dopi/state.rb', line 15

def state
  @state ||= :ready
end

#state_add_child(child) ⇒ Object



32
33
34
35
# File 'lib/dopi/state.rb', line 32

def state_add_child(child)
  state_children << child
  child.add_observer(self)
end

#state_auto_evaluate_childrenObject



19
20
21
22
# File 'lib/dopi/state.rb', line 19

def state_auto_evaluate_children
  @auto_evaluate_children = true if @auto_evaluate_children.nil?
  @auto_evaluate_children
end

#state_auto_evaluate_children=(value) ⇒ Object



24
25
26
# File 'lib/dopi/state.rb', line 24

def state_auto_evaluate_children=(value)
  @auto_evaluate_children = value
end

#state_changed(notify_only = false) ⇒ Object



190
191
192
193
194
# File 'lib/dopi/state.rb', line 190

def state_changed(notify_only = false)
  Dopi.log.debug("State of '#{name}' updated to #{@state.to_s}, notifying observers")
  changed
  notify_observers(notify_only)
end

#state_childrenObject



28
29
30
# File 'lib/dopi/state.rb', line 28

def state_children
  @state_children ||= []
end

#state_children_done?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/dopi/state.rb', line 57

def state_children_done?
  state_children.all? {|child| child.state_done?}
end

#state_children_failed?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/dopi/state.rb', line 53

def state_children_failed?
  state_children.any? {|child| child.state_failed?}
end

#state_children_partial?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/dopi/state.rb', line 61

def state_children_partial?
  state_partial? || state_children.any?{|c| c.state_children_partial?}
end

#state_children_ready?Boolean

Returns:

  • (Boolean)


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

def state_children_ready?
  state_children.any? {|child| child.state_ready?}
end

#state_children_running?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/dopi/state.rb', line 49

def state_children_running?
  state_children.any? {|child| child.state_running?}
end

#state_children_running_noop?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/dopi/state.rb', line 45

def state_children_running_noop?
  state_children.any? {|child| child.state_running_noop?}
end

#state_children_starting?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/dopi/state.rb', line 41

def state_children_starting?
  state_children.any? {|child| child.state_starting?}
end

#state_done?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/dopi/state.rb', line 117

def state_done?
  state == :done
end

#state_failObject



167
168
169
170
171
172
# File 'lib/dopi/state.rb', line 167

def state_fail
  return if state == :failed
  raise Dopi::StateTransitionError, "Can't switch to failed from #{state.to_s}" unless state == :running
  @state = :failed
  state_changed
end

#state_failed?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/dopi/state.rb', line 121

def state_failed?
  state == :failed
end

#state_finishObject



160
161
162
163
164
165
# File 'lib/dopi/state.rb', line 160

def state_finish
  return if state == :done
  raise Dopi::StateTransitionError, "Can't switch to done from #{state.to_s}" unless state == :running
  @state = :done
  state_changed
end

#state_partial?Boolean

Returns:

  • (Boolean)


125
126
127
128
129
130
# File 'lib/dopi/state.rb', line 125

def state_partial?
  [:failed, :done, :running, :running_noop, :starting, :ready].each do |s|
    return false if state_children.all?{|c| c.state == s}
  end
  return true
end

#state_readyObject



153
154
155
156
157
158
# File 'lib/dopi/state.rb', line 153

def state_ready
  return if state == :ready
  raise Dopi::StateTransitionError, "Can't switch to ready from #{state.to_s}" unless state == :running_noop
  @state = :ready
  state_changed
end

#state_ready?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/dopi/state.rb', line 101

def state_ready?
  state == :ready
end

#state_reset(force = false) ⇒ Object



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

def state_reset(force = false)
  if force
    state_children.each {|child| child.state_reset(force)}
    @state = :ready
    state_changed
  else
    raise Dopi::StateTransitionError, "Can't switch to ready from #{state.to_s}" unless state == :failed || state == :ready
    if state_children.any?
      state_children.each {|child| child.state_reset unless (child.state_done? || child.state_ready?)}
    else
      @state = :ready
      state_changed
    end
  end
end

#state_reset_with_children(force = false) ⇒ Object



94
95
96
97
98
99
# File 'lib/dopi/state.rb', line 94

def state_reset_with_children(force = false)
  state_reset(force) if state_failed? or force
  if state_children_failed? or force
    state_children.each {|child| child.state_reset_with_children(force) }
  end
end

#state_runObject



139
140
141
142
143
144
# File 'lib/dopi/state.rb', line 139

def state_run
  return if state == :running
  raise Dopi::StateTransitionError, "Can't switch to running from #{state.to_s}" unless state == :ready || state == :starting
  @state = :running
  state_changed
end

#state_run_noopObject



146
147
148
149
150
151
# File 'lib/dopi/state.rb', line 146

def state_run_noop
  return if state == :running_noop
  raise Dopi::StateTransitionError, "Can't switch to running_noop from #{state.to_s}" unless state == :ready || state == :starting
  @state = :running_noop
  state_changed
end

#state_running?Boolean

Returns:

  • (Boolean)


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

def state_running?
  state == :running
end

#state_running_noop?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/dopi/state.rb', line 113

def state_running_noop?
  state == :running_noop
end

#state_startObject



132
133
134
135
136
137
# File 'lib/dopi/state.rb', line 132

def state_start
  return if state == :starting
  raise Dopi::StateTransitionError, "Can't switch to running from #{state.to_s}" unless state == :ready
  @state = :starting
  state_changed
end

#state_starting?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/dopi/state.rb', line 105

def state_starting?
  state == :starting
end

#to_yaml_propertiesObject



11
12
13
# File 'lib/dopi/state.rb', line 11

def to_yaml_properties
  instance_variables - [:@signal_procs]
end

#update(notify_only) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/dopi/state.rb', line 69

def update(notify_only)
  update_mutex.synchronize do
    unless notify_only
      old_state = @state
      Dopi.log.debug("Checking if state of '#{name}' needs to be updated")
      unless state_children.empty? || !state_auto_evaluate_children
        if    state_children_failed?       then @state = :failed
        elsif state_children_done?         then @state = :done
        elsif state_children_running?      then @state = :running
        elsif state_children_running_noop? then @state = :running_noop
        elsif state_children_starting?     then @state = :starting
        elsif state_children_ready?        then @state = :ready
        end
        if old_state == @state
          state_changed(true)
        else
          state_changed(false)
        end
      end
    else
      state_changed(true)
    end
  end
end

#update_mutexObject



65
66
67
# File 'lib/dopi/state.rb', line 65

def update_mutex
  @upate_mutex || Mutex.new
end