Class: Outback::Manager

Inherits:
Object
  • Object
show all
Includes:
Observable
Defined in:
lib/outback/manager.rb

Constant Summary collapse

ROLLOUT =
1
ROLLBACK =
-1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeManager

Returns a new instance of Manager.



16
17
18
19
20
21
# File 'lib/outback/manager.rb', line 16

def initialize
  @tasks = []
  @cache = {}
  @position = 0
  @direction = 1
end

Instance Attribute Details

#cacheObject

Returns the value of attribute cache.



14
15
16
# File 'lib/outback/manager.rb', line 14

def cache
  @cache
end

#directionObject

Returns the value of attribute direction.



14
15
16
# File 'lib/outback/manager.rb', line 14

def direction
  @direction
end

#positionObject

Returns the value of attribute position.



14
15
16
# File 'lib/outback/manager.rb', line 14

def position
  @position
end

#tasksObject (readonly)

Returns the value of attribute tasks.



13
14
15
# File 'lib/outback/manager.rb', line 13

def tasks
  @tasks
end

#workdirObject

Returns the value of attribute workdir.



14
15
16
# File 'lib/outback/manager.rb', line 14

def workdir
  @workdir
end

Instance Method Details

#add_task(task = nil, &block) ⇒ Object



27
28
29
30
31
32
# File 'lib/outback/manager.rb', line 27

def add_task( task=nil, &block )
  if block_given?
    task = Outback::Task.new(&block)
  end
  add_tasks( task ) if task
end

#add_tasks(*tasks) ⇒ Object



23
24
25
# File 'lib/outback/manager.rb', line 23

def add_tasks( *tasks )
  @tasks += tasks
end

#restore_state(state) ⇒ Object



77
78
79
80
81
# File 'lib/outback/manager.rb', line 77

def restore_state( state )
  @position = state[:position] || @position
  @direction = state[:direction] || @direction
  self
end

#rollback!Object



39
40
41
42
# File 'lib/outback/manager.rb', line 39

def rollback!
  @direction = ROLLBACK
  run_all
end

#rollout!Object



34
35
36
37
# File 'lib/outback/manager.rb', line 34

def rollout!
  @direction = ROLLOUT
  run_all
end

#run(task, direction = 1) ⇒ Object



83
84
85
86
87
# File 'lib/outback/manager.rb', line 83

def run( task, direction=1 )
  method = { ROLLOUT => :rollout!, ROLLBACK => :rollback! }[direction]

  task.send(method, task_helper)
end

#run_allObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/outback/manager.rb', line 44

def run_all
  cache.delete(:errors)
  
  tasks_to_run.each do |task|
    temp = cache.dup
    begin
      run task, @direction
    rescue => e
      self.cache = temp
      cache[:errors] ||= []
      cache[:errors] << e
      break false
    end
    @position += @direction
    changed
    notify_observers(state, cache)
  end
end

#stateObject



71
72
73
74
75
# File 'lib/outback/manager.rb', line 71

def state
  { :position => @position,
    :direction => @direction
  }
end

#task_helperObject



89
90
91
# File 'lib/outback/manager.rb', line 89

def task_helper
  @task_helper ||= TaskHelper.new(self)
end

#tasks_to_runObject



63
64
65
66
67
68
69
# File 'lib/outback/manager.rb', line 63

def tasks_to_run
  if @direction == ROLLOUT
    @tasks[@position..-1]
  elsif @direction == ROLLBACK
    @tasks[0...@position].reverse
  end
end