Class: Pindo::TaskSystem::TaskReporter

Inherits:
Object
  • Object
show all
Defined in:
lib/pindo/module/task/task_reporter.rb

Overview

TaskReporter - 任务报告输出

职责:

  • 输出任务执行计划

  • 输出任务进度信息

  • 输出任务执行摘要

  • 格式化任务状态显示

Instance Method Summary collapse

Constructor Details

#initialize(queue) ⇒ TaskReporter

Returns a new instance of TaskReporter.



13
14
15
# File 'lib/pindo/module/task/task_reporter.rb', line 13

def initialize(queue)
  @queue = queue
end

Instance Method Details

输出任务执行计划

Parameters:

  • strategy (Object)

    执行策略(响应 :name 方法)



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
53
54
55
56
57
58
59
60
61
# File 'lib/pindo/module/task/task_reporter.rb', line 19

def print_execution_plan(strategy)
  # 获取待执行任务的快照(按类型分组)
  tasks_by_type = @queue.pending_grouped_by_type

  puts "\n"
  puts "\e[34m" + "=" * 60
  puts "\e[34m" + "  任务执行计划 (#{strategy.name})"
  puts "\e[34m" + "=" * 60

  tasks_by_type.each do |type, tasks|
    # 直接从任务类获取显示名称
    type_name = if tasks.first&.class&.respond_to?(:task_type_name)
                  tasks.first.class.task_type_name
                else
                  type.to_s.capitalize
                end
    puts "\e[34m"
    puts "\e[34m" + "  #{type_name}: #{tasks.count} 个任务"

    # 显示该类型下的每个任务名称
    tasks.each do |task|
      puts "\e[34m" + "    - #{task.name}"
    end
  end

  # 按依赖拓扑排序展示真实执行顺序
  ordered = topological_execution_order(@queue.pending_snapshot)
  is_serial = strategy.respond_to?(:name) && strategy.name.include?("串行")
  puts "\e[34m"
  if is_serial
    puts "\e[34m" + "  实际执行顺序:"
  else
    puts "\e[34m" + "  执行顺序预览(并发模式下实际顺序受运行时调度影响):"
  end
  ordered.each_with_index do |task, index|
    puts "\e[34m" + "    #{index + 1}. #{task.name}"
  end

  puts "\e[34m"
  puts "\e[34m" + "  总计: #{@queue.pending_count} 个任务"
  puts "\e[34m" + "=" * 60 + "\e[0m"
  puts "\n"
end

输出执行摘要



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/pindo/module/task/task_reporter.rb', line 138

def print_execution_summary
  completed = @queue.completed_snapshot
  success_count = completed.count { |t| t.status == TaskStatus::SUCCESS }
  failed_count = completed.count { |t| t.status == TaskStatus::FAILED }
  cancelled_count = completed.count { |t| t.status == TaskStatus::CANCELLED }

  # 获取失败和取消的任务
  failed_tasks = completed.select { |t| t.status == TaskStatus::FAILED }
  cancelled_tasks = completed.select { |t| t.status == TaskStatus::CANCELLED }

  # 计算总耗时
  total_time = completed.map(&:execution_time).compact.sum
  minutes = (total_time / 60).to_i
  seconds = (total_time % 60).to_i

  puts "\n"
  puts "\e[34m" + "=" * 60
  puts "\e[34m" + "  任务执行完成"
  puts "\e[34m" + "=" * 60

  # 显示统计信息
  puts "\e[34m" + "  成功: #{success_count} 个任务" + "\e[0m" if success_count > 0

  if failed_count > 0
    puts "\e[31m" + "  失败: #{failed_count} 个任务" + "\e[0m"
    failed_tasks.each do |task|
      puts "\e[31m" + "    - #{task.name}" + "\e[0m"
    end
  end

  if cancelled_count > 0
    puts "\e[33m" + "  取消: #{cancelled_count} 个任务" + "\e[0m"
    cancelled_tasks.each do |task|
      puts "\e[33m" + "    - #{task.name}" + "\e[0m"
    end
  end

  if minutes > 0
    puts "\e[34m" + "  总耗时: #{minutes}#{seconds}" + "\e[0m"
  else
    puts "\e[34m" + "  总耗时: #{seconds}" + "\e[0m"
  end

  puts "\e[34m" + "=" * 60 + "\e[0m"
  puts "\n"
end

输出任务失败信息

Parameters:

  • task (PindoTask)

    任务对象

  • error (Exception)

    错误对象



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/pindo/module/task/task_reporter.rb', line 118

def print_task_failure(task, error)
  puts ""
  puts ("*" * 60).cyan
  puts "✗ Task End: #{task.name} 失败".cyan
  puts "错误信息: #{error.message}".cyan

  # 打印堆栈信息用于调试(仅在 PINDO_DEBUG 模式下)
  if ENV['PINDO_DEBUG'] && error.backtrace && !error.backtrace.empty?
    puts ""
    puts "堆栈信息:".cyan
    error.backtrace.first(10).each do |line|
      puts "  #{line}".cyan
    end
  end

  puts ("*" * 60).cyan
  puts ""
end

输出任务成功信息

Parameters:



106
107
108
109
110
111
112
113
# File 'lib/pindo/module/task/task_reporter.rb', line 106

def print_task_success(task)
  time_str = task.execution_time ? task.execution_time.round(2) : 0
  puts ""
  puts ("*" * 60).cyan
  puts "✓ Task End: #{task.name} 完成 (#{time_str}s)".cyan
  puts ("*" * 60).cyan
  puts ""
end

输出任务执行标题

Parameters:



97
98
99
100
101
102
# File 'lib/pindo/module/task/task_reporter.rb', line 97

def print_task_title(task)
  puts ""
  puts ("*" * 60).cyan
  puts "▶ Task Start: #{task.name}".cyan
  puts ("*" * 60).cyan
end

#topological_execution_order(tasks) ⇒ Array<PindoTask>

按依赖关系生成执行顺序(贪心拓扑排序,模拟串行调度:每轮取「入队顺序中第一个依赖已满足」的任务),与串行实际执行顺序一致

Parameters:

  • tasks (Array<PindoTask>)

    待排序任务(入队顺序)

Returns:

  • (Array<PindoTask>)

    按执行顺序排列的任务



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/pindo/module/task/task_reporter.rb', line 67

def topological_execution_order(tasks)
  all_ids = tasks.map(&:id)
  remaining = tasks.dup
  completed_ids = []
  ordered = []

  until remaining.empty?
    index = remaining.index do |task|
      task.dependencies.all? do |dep_id|
        # 依赖已完成,或依赖不在本批任务中(视为已提前完成)
        completed_ids.include?(dep_id) || !all_ids.include?(dep_id)
      end
    end

    # 存在循环依赖或依赖缺失,剩余任务按原顺序追加并结束
    if index.nil?
      ordered.concat(remaining)
      break
    end

    task = remaining.delete_at(index)
    completed_ids << task.id
    ordered << task
  end

  ordered
end