Class: TaskManager

Inherits:
Object
  • Object
show all
Defined in:
lib/task_manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(output) ⇒ TaskManager

Returns a new instance of TaskManager.



3
4
5
6
7
# File 'lib/task_manager.rb', line 3

def initialize(output)
  @output = output
  @tasks = []
  @fail_index = -1
end

Instance Method Details

#add(task) ⇒ Object



9
10
11
# File 'lib/task_manager.rb', line 9

def add(task)
  @tasks << task
end

#build_exception_result(ex) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/task_manager.rb', line 71

def build_exception_result(ex)
  return {
    :state => :error,
    :title => 'Task',
    :summary => 'Exception',
    :first => ex.message,
    :detail => ex.message + "\n" + ex.backtrace.join("\n")
  }
end

#output_exception(ex) ⇒ Object



81
82
83
# File 'lib/task_manager.rb', line 81

def output_exception(ex)
  @output.add_result build_exception_result ex
end

#run(files) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/task_manager.rb', line 13

def run(files)
  @num_tasks_run = 0
  
  @tasks.each_index do |index|
    task = @tasks[index]

    # Don't run if this task is after the last failed task
    if task.is_configured?(files) && @fail_index >= 0 && @fail_index < index
      @output.add_result(@fail_result)
      break
    end
    
    result = nil
    begin
      result = run_task(files, task)        
    rescue Exception => ex
      result = build_exception_result ex
      @output.add_result result
    end

    next if result.nil?

    if [:error, :failure].include? result[:state]
      # Remember this task as the one that failed
      # And end this current run of tasks
      @fail_index = index
      @fail_result = result
      
      @fail_result[:state] = :error
      @fail_result[:title] = 'Fix ' + @fail_result[:title] + ' to run other tasks'
      break
    elsif result[:state] == :success
      # reset fail index
      @fail_index = -1 if index == @fail_index
    end
    
  end
  
  @num_tasks_run    
end

#run_task(files, task) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/task_manager.rb', line 54

def run_task(files, task)
  result = task.run(files)
  return result if result.nil?

  task_run
  
  @output.add_result(result) 
  return result
end

#task_runObject



64
65
66
67
68
69
# File 'lib/task_manager.rb', line 64

def task_run
  @num_tasks_run += 1
  if (@num_tasks_run == 1) 
    @output.start_run
  end  
end