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
# 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

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

输出执行摘要



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
133
134
135
136
137
138
# File 'lib/pindo/module/task/task_reporter.rb', line 93

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)

    错误对象



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/pindo/module/task/task_reporter.rb', line 73

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:



61
62
63
64
65
66
67
68
# File 'lib/pindo/module/task/task_reporter.rb', line 61

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:



52
53
54
55
56
57
# File 'lib/pindo/module/task/task_reporter.rb', line 52

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