Class: Patir::CommandSequence
- Inherits:
-
Object
- Object
- Patir::CommandSequence
- Includes:
- Observable
- Defined in:
- lib/patir/command.rb
Overview
CommandSequence describes a set of commands to be executed in sequence.
Each instance of CommandSequence contains a set of Patir::Command instances, which are the steps to perform.
The steps are executed in the sequence they are added. A CommandSequence can terminate immediately on step failure or it can continue. It will still be marked as failed as long as a single step fails.
Access to the CommandSequence status is achieved using the Observer pattern.
The :sequence_status message contains the status of the sequence, an instance of the class CommandSequenceStatus.
CommandSequence is designed to be reusable, so it does not correspond to a single sequence run, rather it corresponds to the currently active run. Calling reset, or run will discard the old state and create a new sequence ‘instance’ and status.
No threads are spawned by CommandSequence (that doesn’t mean the commands cannot, but it is not advisable).
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#sequence_id ⇒ Object
Returns the value of attribute sequence_id.
-
#sequence_runner ⇒ Object
Returns the value of attribute sequence_runner.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#steps ⇒ Object
readonly
Returns the value of attribute steps.
Instance Method Summary collapse
-
#add_step(step, exit_strategy = :fail_on_error) ⇒ Object
Adds a step to the CommandSequence using the given exit strategy.
-
#completed? ⇒ Boolean
Returns true if the sequence has finished executing.
-
#initialize(name, sequence_runner = "") ⇒ CommandSequence
constructor
A new instance of CommandSequence.
-
#reset ⇒ Object
Resets the status.
-
#run ⇒ Object
Executes the CommandSequence.
- #to_s ⇒ Object
Constructor Details
#initialize(name, sequence_runner = "") ⇒ CommandSequence
Returns a new instance of CommandSequence.
177 178 179 180 181 182 183 |
# File 'lib/patir/command.rb', line 177 def initialize name,sequence_runner="" @name=name @steps||=Array.new @sequence_runner=sequence_runner #intialize the status for the currently active build (not executed) reset end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
173 174 175 |
# File 'lib/patir/command.rb', line 173 def name @name end |
#sequence_id ⇒ Object
Returns the value of attribute sequence_id.
175 176 177 |
# File 'lib/patir/command.rb', line 175 def sequence_id @sequence_id end |
#sequence_runner ⇒ Object
Returns the value of attribute sequence_runner.
174 175 176 |
# File 'lib/patir/command.rb', line 174 def sequence_runner @sequence_runner end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
173 174 175 |
# File 'lib/patir/command.rb', line 173 def state @state end |
#steps ⇒ Object (readonly)
Returns the value of attribute steps.
173 174 175 |
# File 'lib/patir/command.rb', line 173 def steps @steps end |
Instance Method Details
#add_step(step, exit_strategy = :fail_on_error) ⇒ Object
Adds a step to the CommandSequence using the given exit strategy.
Steps are always added at the end of the build sequence. A step should quack like a Patir::Command.
Valid exit strategies are :fail_on_error - CommandSequence terminates on failure of this step :flunk_on_error - CommandSequence is flagged as failed but continues to the next step :fail_on_warning - CommandSequence terminates on warnings in this step :flunk_on_warning - CommandSequence is flagged as failed on warning in this step
260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/patir/command.rb', line 260 def add_step step,exit_strategy=:fail_on_error #duplicate the command bstep=step.dup #reset it bstep.reset #set the extended attributes bstep.number=@steps.size bstep.strategy=exit_strategy #add it to the lot @steps<<bstep #add it to status as well @state.step=bstep notify(:sequence_status=>@state) end |
#completed? ⇒ Boolean
Returns true if the sequence has finished executing
291 292 293 |
# File 'lib/patir/command.rb', line 291 def completed? return @state.completed? end |
#reset ⇒ Object
Resets the status. This will set :not_executed status, and set the start and end times to nil.
277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/patir/command.rb', line 277 def reset #reset all the steps (stati and execution times) @steps.each{|step| step.reset} #reset the status @state=CommandSequenceStatus.new(@name) @steps.each{|step| @state.step=step} @state.start_time=Time.now @state.stop_time=nil @state.sequence_runner=@sequence_runner #tell the world notify(:sequence_status=>@state) end |
#run ⇒ Object
Executes the CommandSequence.
Will run all step instances in sequence observing the exit strategies on warning/failures
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/patir/command.rb', line 199 def run #set the start time @state.start_time=Time.now #reset the stop time @state.stop_time=nil #we started running, lets tell the world @state.status=:running notify(:sequence_status=>@state) #we are optimistic running_status=:success #but not that much running_status=:warning if @steps.empty? #execute the steps in sequence @steps.each do |step| #the step is running, tell the world @state.step=step step.status=:running notify(:sequence_status=>@state) #run it, get the result and notify result=step.run @state.step=step step.status=:running notify(:sequence_status=>@state) #evaluate the results' effect on execution status at the end case result when :success #everything is fine, continue when :error #this will be the final status running_status=:error #stop if we fail on error if :fail_on_error==step.strategy @state.status=:error break end when :warning #a previous failure overrides a warning running_status=:warning unless :error==running_status #escalate this to a failure if the strategy says so running_status=:error if :flunk_on_warning==step.strategy #stop if we fail on warning if :fail_on_warning==step.strategy @state.status=:error break end end end #we finished @state.stop_time=Time.now @state.status=running_status notify(:sequence_status=>@state) end |
#to_s ⇒ Object
295 296 297 |
# File 'lib/patir/command.rb', line 295 def to_s "#{sequence_id}:#{:name} on #{@sequence_runner}, #{@steps.size} steps" end |