Class: Pindo::TaskSystem::TaskManager

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/pindo/module/task/task_manager.rb

Overview

简化版任务管理器所有任务在主线程中按顺序执行

Instance Method Summary collapse

Constructor Details

#initializeTaskManager

Returns a new instance of TaskManager.



11
12
13
14
15
# File 'lib/pindo/module/task/task_manager.rb', line 11

def initialize
  @pending_queue = []      # 待执行队列
  @completed_tasks = []    # 已完成的任务
  @current_task = nil      # 当前正在执行的任务
end

Instance Method Details

#add_task(task, options = {}) ⇒ Object

添加任务

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/pindo/module/task/task_manager.rb', line 18

def add_task(task, options = {})
  raise ArgumentError, "Task must be a PindoTask" unless task.is_a?(PindoTask)

  # 验证任务
  unless task.validate
    raise ArgumentError, "Task validation failed: #{task.name}"
  end

  # 处理依赖
  if options[:wait_for]
    task.dependencies.concat(Array(options[:wait_for]))
  end

  @pending_queue << task
  # 按优先级排序
  @pending_queue.sort_by! { |t| -t.priority }

  task.id
end

#add_tasks(tasks) ⇒ Object

批量添加任务



39
40
41
# File 'lib/pindo/module/task/task_manager.rb', line 39

def add_tasks(tasks)
  tasks.map { |task| add_task(task) }
end

#cancel_task(task_id) ⇒ Object

取消任务



106
107
108
109
# File 'lib/pindo/module/task/task_manager.rb', line 106

def cancel_task(task_id)
  task = find_task(task_id)
  task&.cancel
end

#clear_allObject

清空所有队列(用于重置)



93
94
95
96
97
# File 'lib/pindo/module/task/task_manager.rb', line 93

def clear_all
  @pending_queue.clear
  @completed_tasks.clear
  @current_task = nil
end

#execution_reportObject

获取执行报告



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

def execution_report
  {
    pending: @pending_queue.count,
    completed: @completed_tasks.count,
    success: @completed_tasks.count { |t| t.status == TaskStatus::SUCCESS },
    failed: @completed_tasks.count { |t| t.status == TaskStatus::FAILED },
    tasks: (@pending_queue + @completed_tasks).map do |task|
      {
        id: task.id,
        name: task.name,
        type: task.type,
        status: task.status,
        error: task.error&.message,
        execution_time: task.execution_time
      }
    end
  }
end

#startObject

开始执行(在主线程中顺序执行所有任务)



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/pindo/module/task/task_manager.rb', line 44

def start
  # 输出任务执行计划
  print_execution_plan

  # 主循环:按顺序执行所有任务
  while @pending_queue.any?
    task = get_next_executable_task

    unless task
      # 检查是否所有任务都因依赖问题无法执行
      if @pending_queue.all? { |t| check_dependencies(t) != :ready }
        Funlog.warning("所有剩余任务都因依赖问题无法执行")
        break
      end

      # 没有可执行的任务,短暂休眠后重试
      sleep(0.1)
      next
    end

    # 执行任务
    execute_task(task)
  end

  # 输出执行摘要
  print_execution_summary
end

#task_status(task_id) ⇒ Object

获取任务状态



100
101
102
103
# File 'lib/pindo/module/task/task_manager.rb', line 100

def task_status(task_id)
  task = find_task(task_id)
  task&.status
end