Class: Pindo::TaskSystem::PindoTask

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

Overview

PindoTask 基类(简化版,所有任务在主线程中执行)

Direct Known Subclasses

BuildTask, GitTagTask, UnityExportTask, UploadTask

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ PindoTask

Returns a new instance of PindoTask.



42
43
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
# File 'lib/pindo/module/task/pindo_task.rb', line 42

def initialize(name, options = {})
  @id = SecureRandom.uuid
  @name = name
  @type = self.class.task_type
  @priority = options[:priority] || TaskPriority::HIGH
  @status = TaskStatus::PENDING
  @dependencies = options[:dependencies] || []
  @context = options[:context] || {}
   = options[:metadata] || {}
  @error = nil
  @result = nil
  @created_at = Time.now
  @started_at = nil
  @finished_at = nil
  @callbacks = {
    before: [],
    after: [],
    on_success: [],
    on_failure: []
  }
  @callbacks_setup = false

  # 重试配置
  @retry_mode = options[:retry_mode] || self.class.default_retry_mode
  @retry_count = options[:retry_count] || self.class.default_retry_count
  @retry_delay = options[:retry_delay] || self.class.default_retry_delay
end

Instance Attribute Details

#callbacks_setupObject

标记回调是否已经设置



40
41
42
# File 'lib/pindo/module/task/pindo_task.rb', line 40

def callbacks_setup
  @callbacks_setup
end

#contextObject

Returns the value of attribute context.



36
37
38
# File 'lib/pindo/module/task/pindo_task.rb', line 36

def context
  @context
end

#created_atObject (readonly)

Returns the value of attribute created_at.



37
38
39
# File 'lib/pindo/module/task/pindo_task.rb', line 37

def created_at
  @created_at
end

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



35
36
37
# File 'lib/pindo/module/task/pindo_task.rb', line 35

def dependencies
  @dependencies
end

#errorObject

Returns the value of attribute error.



34
35
36
# File 'lib/pindo/module/task/pindo_task.rb', line 34

def error
  @error
end

#finished_atObject (readonly)

Returns the value of attribute finished_at.



37
38
39
# File 'lib/pindo/module/task/pindo_task.rb', line 37

def finished_at
  @finished_at
end

#idObject

Returns the value of attribute id.



34
35
36
# File 'lib/pindo/module/task/pindo_task.rb', line 34

def id
  @id
end

#metadataObject

Returns the value of attribute metadata.



36
37
38
# File 'lib/pindo/module/task/pindo_task.rb', line 36

def 
  
end

#nameObject

Returns the value of attribute name.



34
35
36
# File 'lib/pindo/module/task/pindo_task.rb', line 34

def name
  @name
end

#priorityObject (readonly)

Returns the value of attribute priority.



35
36
37
# File 'lib/pindo/module/task/pindo_task.rb', line 35

def priority
  @priority
end

#resultObject

Returns the value of attribute result.



34
35
36
# File 'lib/pindo/module/task/pindo_task.rb', line 34

def result
  @result
end

#retry_countObject

剩余重试次数



38
39
40
# File 'lib/pindo/module/task/pindo_task.rb', line 38

def retry_count
  @retry_count
end

#retry_delayObject (readonly)

Returns the value of attribute retry_delay.



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

def retry_delay
  @retry_delay
end

#retry_modeObject (readonly)

Returns the value of attribute retry_mode.



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

def retry_mode
  @retry_mode
end

#started_atObject (readonly)

Returns the value of attribute started_at.



37
38
39
# File 'lib/pindo/module/task/pindo_task.rb', line 37

def started_at
  @started_at
end

#statusObject

Returns the value of attribute status.



34
35
36
# File 'lib/pindo/module/task/pindo_task.rb', line 34

def status
  @status
end

#typeObject (readonly)

Returns the value of attribute type.



35
36
37
# File 'lib/pindo/module/task/pindo_task.rb', line 35

def type
  @type
end

Class Method Details

.default_retry_countObject



80
81
82
# File 'lib/pindo/module/task/pindo_task.rb', line 80

def self.default_retry_count
  0  # 默认不重试
end

.default_retry_delayObject



84
85
86
# File 'lib/pindo/module/task/pindo_task.rb', line 84

def self.default_retry_delay
  10  # 默认延迟 10 秒
end

.default_retry_modeObject

默认重试配置



76
77
78
# File 'lib/pindo/module/task/pindo_task.rb', line 76

def self.default_retry_mode
  RetryMode::IMMEDIATE
end

.task_typeObject

子类必须实现的方法

Raises:

  • (NotImplementedError)


71
72
73
# File 'lib/pindo/module/task/pindo_task.rb', line 71

def self.task_type
  raise NotImplementedError, "Subclass must define task_type"
end

Instance Method Details

#before_retryObject



151
152
153
# File 'lib/pindo/module/task/pindo_task.rb', line 151

def before_retry
  # 子类可重写,进行重试前的清理工作
end

#cancelObject

取消任务



109
110
111
112
113
114
115
116
# File 'lib/pindo/module/task/pindo_task.rb', line 109

def cancel
  if @status == TaskStatus::PENDING
    @status = TaskStatus::CANCELLED
    true
  else
    false
  end
end

#cancelled?Boolean

检查是否已取消(不抛异常)

Returns:

  • (Boolean)


133
134
135
# File 'lib/pindo/module/task/pindo_task.rb', line 133

def cancelled?
  @status == TaskStatus::CANCELLED
end

#check_cancelled!Object

检查是否已取消,如果已取消则抛出异常



126
127
128
129
130
# File 'lib/pindo/module/task/pindo_task.rb', line 126

def check_cancelled!
  if @status == TaskStatus::CANCELLED
    raise TaskCancelledException.new("任务已被取消: #{@name}")
  end
end

#do_taskObject

执行任务(在主线程中同步执行)



89
90
91
# File 'lib/pindo/module/task/pindo_task.rb', line 89

def do_task
  execute_internal
end

#execution_timeObject

执行时间



119
120
121
122
123
# File 'lib/pindo/module/task/pindo_task.rb', line 119

def execution_time
  return nil unless @started_at
  end_time = @finished_at || Time.now
  end_time - @started_at
end

#finished?Boolean

检查是否完成

Returns:

  • (Boolean)


94
95
96
# File 'lib/pindo/module/task/pindo_task.rb', line 94

def finished?
  [TaskStatus::SUCCESS, TaskStatus::FAILED, TaskStatus::CANCELLED].include?(@status)
end

#on(event, &block) ⇒ Object

添加回调



138
139
140
# File 'lib/pindo/module/task/pindo_task.rb', line 138

def on(event, &block)
  @callbacks[event] << block if @callbacks[event]
end

#reset_for_retryObject



155
156
157
158
159
160
# File 'lib/pindo/module/task/pindo_task.rb', line 155

def reset_for_retry
  @status = TaskStatus::PENDING
  @result = nil
  @started_at = nil
  @finished_at = nil
end

#retryable?Boolean

重试相关方法

Returns:

  • (Boolean)


143
144
145
# File 'lib/pindo/module/task/pindo_task.rb', line 143

def retryable?
  @retry_count > 0
end

#running?Boolean

是否正在运行

Returns:

  • (Boolean)


99
100
101
# File 'lib/pindo/module/task/pindo_task.rb', line 99

def running?
  @status == TaskStatus::RUNNING
end

#should_retry?(error) ⇒ Boolean

Returns:

  • (Boolean)


147
148
149
# File 'lib/pindo/module/task/pindo_task.rb', line 147

def should_retry?(error)
  true  # 默认所有错误都重试,子类可重写
end

#validateObject

验证任务是否可以执行



104
105
106
# File 'lib/pindo/module/task/pindo_task.rb', line 104

def validate
  true
end