Class: Distribot::Plan

Inherits:
Object
  • Object
show all
Includes:
TSort
Defined in:
lib/distribot/plan.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Plan

Returns a new instance of Plan.



8
9
10
11
# File 'lib/distribot/plan.rb', line 8

def initialize(name)
  @name = name
  @tasks = [ ]
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/distribot/plan.rb', line 7

def name
  @name
end

#tasksObject

Returns the value of attribute tasks.



7
8
9
# File 'lib/distribot/plan.rb', line 7

def tasks
  @tasks
end

Class Method Details

.allObject



44
45
46
47
# File 'lib/distribot/plan.rb', line 44

def self.all
  @@plans ||= [ ]
  @@plans
end

.reset!Object



49
50
51
# File 'lib/distribot/plan.rb', line 49

def self.reset!
  @@plans = [ ]
end

Instance Method Details

#group(name, options = {}, &block) ⇒ Object



13
14
15
16
17
# File 'lib/distribot/plan.rb', line 13

def group(name, options={}, &block)
  task_group = TaskGroup.new(name, options[:depends_on])
  task_group.instance_eval(&block)
  self.tasks += task_group.tasks
end

#scheduleObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/distribot/plan.rb', line 28

def schedule
  grouped = tsort_each_child.map{|_parent, tasks| tasks}
  grouped.each_with_index.map do |group, idx|
    info = {
      name: 'phase %d/%d' % [ idx + 1, grouped.count ],
      handlers: group.map(&:handler_data)
    }
    if grouped[idx + 1]
      info[:transitions_to] = 'phase %d/%d' % [ idx + 2, grouped.count ]
    end
    info.merge!(is_initial: true) if idx == 0
    info.merge!(is_final: true) if idx + 1 == grouped.count
    info
  end
end

#task(name, options = {}, &block) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/distribot/plan.rb', line 19

def task(name, options={}, &block)
  if tasks.select{ |task| task.name.to_s == name.to_s }.any?
    raise TaskConfigError.new "The name '%s' is already taken"
  end
  task = Task.new(name, options[:depends_on])
  task.instance_eval(&block)
  tasks << task
end