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



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

def initialize(queue)
  @queue = queue
end

Instance Method Details

输出任务执行计划



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
# 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 "  任务执行计划 (#{strategy.name})"
  puts "=" * 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 "\n  #{type_name}: #{tasks.count} 个任务"

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

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

输出执行摘要



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/pindo/module/task/task_reporter.rb', line 87

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

输出任务失败信息



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/pindo/module/task/task_reporter.rb', line 67

def print_task_failure(task, error)
  puts "\n"
  Funlog.error("任务失败: #{task.name}")
  Funlog.error("错误信息: #{error.message}")

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

输出任务底部分隔线



82
83
84
# File 'lib/pindo/module/task/task_reporter.rb', line 82

def print_task_footer
  # 简化输出,不需要额外的分隔线
end

输出任务执行头部



50
51
52
53
54
# File 'lib/pindo/module/task/task_reporter.rb', line 50

def print_task_header(task)
  puts ""
  puts "\e[34m▶ #{task.name}\e[0m"
  puts "─" * 60
end

输出任务成功信息



58
59
60
61
62
# File 'lib/pindo/module/task/task_reporter.rb', line 58

def print_task_success(task)
  time_str = task.execution_time ? task.execution_time.round(2) : 0
  puts ""
  puts "\e[32m✓ #{task.name} 完成 (#{time_str}s)\e[0m"
end