Module: Dopi::State
- Includes:
- Observable
- Included in:
- Command, CommandSet, Plan, Step, StepSet
- Defined in:
- lib/dopi/state.rb
Instance Method Summary collapse
- #delete_on_signal(a_proc) ⇒ Object
- #on_signal(a_proc) ⇒ Object
- #reset_signals ⇒ Object
- #send_signal(signal) ⇒ Object
- #signal_procs ⇒ Object
- #signals ⇒ Object
- #state ⇒ Object
- #state_add_child(child) ⇒ Object
- #state_auto_evaluate_children ⇒ Object
- #state_auto_evaluate_children=(value) ⇒ Object
- #state_changed(notify_only = false) ⇒ Object
- #state_children ⇒ Object
- #state_children_done? ⇒ Boolean
- #state_children_failed? ⇒ Boolean
- #state_children_partial? ⇒ Boolean
- #state_children_ready? ⇒ Boolean
- #state_children_running? ⇒ Boolean
- #state_children_running_noop? ⇒ Boolean
- #state_children_starting? ⇒ Boolean
- #state_done? ⇒ Boolean
- #state_fail ⇒ Object
- #state_failed? ⇒ Boolean
- #state_finish ⇒ Object
- #state_partial? ⇒ Boolean
- #state_ready ⇒ Object
- #state_ready? ⇒ Boolean
- #state_reset(force = false) ⇒ Object
- #state_reset_with_children(force = false) ⇒ Object
- #state_run ⇒ Object
- #state_run_noop ⇒ Object
- #state_running? ⇒ Boolean
- #state_running_noop? ⇒ Boolean
- #state_start ⇒ Object
- #state_starting? ⇒ Boolean
- #to_yaml_properties ⇒ Object
- #update(notify_only) ⇒ Object
- #update_mutex ⇒ Object
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_signals ⇒ Object
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_procs ⇒ Object
205 206 207 |
# File 'lib/dopi/state.rb', line 205 def signal_procs @signal_procs ||= [] end |
#signals ⇒ Object
196 197 198 |
# File 'lib/dopi/state.rb', line 196 def signals @signals ||= Hash.new(false) end |
#state ⇒ Object
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_children ⇒ Object
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_children ⇒ Object
28 29 30 |
# File 'lib/dopi/state.rb', line 28 def state_children @state_children ||= [] end |
#state_children_done? ⇒ 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
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
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
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
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
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
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
117 118 119 |
# File 'lib/dopi/state.rb', line 117 def state_done? state == :done end |
#state_fail ⇒ Object
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
121 122 123 |
# File 'lib/dopi/state.rb', line 121 def state_failed? state == :failed end |
#state_finish ⇒ Object
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
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_ready ⇒ Object
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
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_run ⇒ Object
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_noop ⇒ Object
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
109 110 111 |
# File 'lib/dopi/state.rb', line 109 def state_running? state == :running end |
#state_running_noop? ⇒ Boolean
113 114 115 |
# File 'lib/dopi/state.rb', line 113 def state_running_noop? state == :running_noop end |
#state_start ⇒ Object
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
105 106 107 |
# File 'lib/dopi/state.rb', line 105 def state_starting? state == :starting end |
#to_yaml_properties ⇒ Object
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_mutex ⇒ Object
65 66 67 |
# File 'lib/dopi/state.rb', line 65 def update_mutex @upate_mutex || Mutex.new end |