Class: Pindo::TaskSystem::PindoTask
- Inherits:
-
Object
- Object
- Pindo::TaskSystem::PindoTask
- Defined in:
- lib/pindo/module/task/pindo_task.rb
Overview
PindoTask 基类(简化版,所有任务在主线程中执行)
Direct Known Subclasses
Instance Attribute Summary collapse
-
#callbacks_setup ⇒ Object
标记回调是否已经设置.
-
#context ⇒ Object
Returns the value of attribute context.
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#dependencies ⇒ Object
readonly
Returns the value of attribute dependencies.
-
#error ⇒ Object
Returns the value of attribute error.
-
#finished_at ⇒ Object
readonly
Returns the value of attribute finished_at.
-
#id ⇒ Object
Returns the value of attribute id.
-
#max_retry_count ⇒ Object
readonly
max_retry_count: 初始最大重试次数.
-
#metadata ⇒ Object
Returns the value of attribute metadata.
-
#name ⇒ Object
Returns the value of attribute name.
-
#priority ⇒ Object
readonly
Returns the value of attribute priority.
-
#result ⇒ Object
Returns the value of attribute result.
-
#retry_count ⇒ Object
剩余重试次数.
-
#retry_delay ⇒ Object
readonly
max_retry_count: 初始最大重试次数.
-
#retry_mode ⇒ Object
readonly
max_retry_count: 初始最大重试次数.
-
#started_at ⇒ Object
readonly
Returns the value of attribute started_at.
-
#status ⇒ Object
Returns the value of attribute status.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
- .default_retry_count ⇒ Object
- .default_retry_delay ⇒ Object
-
.default_retry_mode ⇒ Object
默认重试配置.
-
.task_type ⇒ Object
子类必须实现的方法.
Instance Method Summary collapse
- #before_retry ⇒ Object
-
#cancel ⇒ Object
取消任务.
-
#cancelled? ⇒ Boolean
检查是否已取消(不抛异常).
-
#check_cancelled! ⇒ Object
检查是否已取消,如果已取消则抛出异常.
-
#do_task ⇒ Object
执行任务(在主线程中同步执行).
-
#execution_time ⇒ Object
执行时间.
-
#finished? ⇒ Boolean
检查是否完成.
-
#initialize(name, options = {}) ⇒ PindoTask
constructor
A new instance of PindoTask.
-
#on(event, &block) ⇒ Object
添加回调.
- #reset_for_retry ⇒ Object
-
#retryable? ⇒ Boolean
重试相关方法.
-
#running? ⇒ Boolean
是否正在运行.
- #should_retry?(error) ⇒ Boolean
-
#validate ⇒ Object
验证任务是否可以执行.
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 69 |
# File 'lib/pindo/module/task/pindo_task.rb', line 42 def initialize(name, = {}) @id = SecureRandom.uuid @name = name @type = self.class.task_type @priority = [:priority] || TaskPriority::HIGH @status = TaskStatus::PENDING @dependencies = [:dependencies] || [] @context = [:context] || {} @metadata = [: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 = [:retry_mode] || self.class.default_retry_mode @retry_count = [:retry_count] || self.class.default_retry_count @max_retry_count = @retry_count # 保存初始最大重试次数 @retry_delay = [:retry_delay] || self.class.default_retry_delay end |
Instance Attribute Details
#callbacks_setup ⇒ Object
标记回调是否已经设置
40 41 42 |
# File 'lib/pindo/module/task/pindo_task.rb', line 40 def callbacks_setup @callbacks_setup end |
#context ⇒ Object
Returns the value of attribute context.
36 37 38 |
# File 'lib/pindo/module/task/pindo_task.rb', line 36 def context @context end |
#created_at ⇒ Object (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 |
#dependencies ⇒ Object (readonly)
Returns the value of attribute dependencies.
35 36 37 |
# File 'lib/pindo/module/task/pindo_task.rb', line 35 def dependencies @dependencies end |
#error ⇒ Object
Returns the value of attribute error.
34 35 36 |
# File 'lib/pindo/module/task/pindo_task.rb', line 34 def error @error end |
#finished_at ⇒ Object (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 |
#id ⇒ Object
Returns the value of attribute id.
34 35 36 |
# File 'lib/pindo/module/task/pindo_task.rb', line 34 def id @id end |
#max_retry_count ⇒ Object (readonly)
max_retry_count: 初始最大重试次数
39 40 41 |
# File 'lib/pindo/module/task/pindo_task.rb', line 39 def max_retry_count @max_retry_count end |
#metadata ⇒ Object
Returns the value of attribute metadata.
36 37 38 |
# File 'lib/pindo/module/task/pindo_task.rb', line 36 def @metadata end |
#name ⇒ Object
Returns the value of attribute name.
34 35 36 |
# File 'lib/pindo/module/task/pindo_task.rb', line 34 def name @name end |
#priority ⇒ Object (readonly)
Returns the value of attribute priority.
35 36 37 |
# File 'lib/pindo/module/task/pindo_task.rb', line 35 def priority @priority end |
#result ⇒ Object
Returns the value of attribute result.
34 35 36 |
# File 'lib/pindo/module/task/pindo_task.rb', line 34 def result @result end |
#retry_count ⇒ Object
剩余重试次数
38 39 40 |
# File 'lib/pindo/module/task/pindo_task.rb', line 38 def retry_count @retry_count end |
#retry_delay ⇒ Object (readonly)
max_retry_count: 初始最大重试次数
39 40 41 |
# File 'lib/pindo/module/task/pindo_task.rb', line 39 def retry_delay @retry_delay end |
#retry_mode ⇒ Object (readonly)
max_retry_count: 初始最大重试次数
39 40 41 |
# File 'lib/pindo/module/task/pindo_task.rb', line 39 def retry_mode @retry_mode end |
#started_at ⇒ Object (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 |
#status ⇒ Object
Returns the value of attribute status.
34 35 36 |
# File 'lib/pindo/module/task/pindo_task.rb', line 34 def status @status end |
#type ⇒ Object (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_count ⇒ Object
81 82 83 |
# File 'lib/pindo/module/task/pindo_task.rb', line 81 def self.default_retry_count 0 # 默认不重试 end |
.default_retry_delay ⇒ Object
85 86 87 |
# File 'lib/pindo/module/task/pindo_task.rb', line 85 def self.default_retry_delay 10 # 默认延迟 10 秒 end |
.default_retry_mode ⇒ Object
默认重试配置
77 78 79 |
# File 'lib/pindo/module/task/pindo_task.rb', line 77 def self.default_retry_mode RetryMode::IMMEDIATE end |
.task_type ⇒ Object
子类必须实现的方法
72 73 74 |
# File 'lib/pindo/module/task/pindo_task.rb', line 72 def self.task_type raise NotImplementedError, "Subclass must define task_type" end |
Instance Method Details
#before_retry ⇒ Object
152 153 154 |
# File 'lib/pindo/module/task/pindo_task.rb', line 152 def before_retry # 子类可重写,进行重试前的清理工作 end |
#cancel ⇒ Object
取消任务
110 111 112 113 114 115 116 117 |
# File 'lib/pindo/module/task/pindo_task.rb', line 110 def cancel if @status == TaskStatus::PENDING @status = TaskStatus::CANCELLED true else false end end |
#cancelled? ⇒ Boolean
检查是否已取消(不抛异常)
134 135 136 |
# File 'lib/pindo/module/task/pindo_task.rb', line 134 def cancelled? @status == TaskStatus::CANCELLED end |
#check_cancelled! ⇒ Object
检查是否已取消,如果已取消则抛出异常
127 128 129 130 131 |
# File 'lib/pindo/module/task/pindo_task.rb', line 127 def check_cancelled! if @status == TaskStatus::CANCELLED raise TaskCancelledException.new("任务已被取消: #{@name}") end end |
#do_task ⇒ Object
执行任务(在主线程中同步执行)
90 91 92 |
# File 'lib/pindo/module/task/pindo_task.rb', line 90 def do_task execute_internal end |
#execution_time ⇒ Object
执行时间
120 121 122 123 124 |
# File 'lib/pindo/module/task/pindo_task.rb', line 120 def execution_time return nil unless @started_at end_time = @finished_at || Time.now end_time - @started_at end |
#finished? ⇒ Boolean
检查是否完成
95 96 97 |
# File 'lib/pindo/module/task/pindo_task.rb', line 95 def finished? [TaskStatus::SUCCESS, TaskStatus::FAILED, TaskStatus::CANCELLED].include?(@status) end |
#on(event, &block) ⇒ Object
添加回调
139 140 141 |
# File 'lib/pindo/module/task/pindo_task.rb', line 139 def on(event, &block) @callbacks[event] << block if @callbacks[event] end |
#reset_for_retry ⇒ Object
156 157 158 159 160 161 |
# File 'lib/pindo/module/task/pindo_task.rb', line 156 def reset_for_retry @status = TaskStatus::PENDING @result = nil @started_at = nil @finished_at = nil end |
#retryable? ⇒ Boolean
重试相关方法
144 145 146 |
# File 'lib/pindo/module/task/pindo_task.rb', line 144 def retryable? @retry_count > 0 end |
#running? ⇒ Boolean
是否正在运行
100 101 102 |
# File 'lib/pindo/module/task/pindo_task.rb', line 100 def running? @status == TaskStatus::RUNNING end |
#should_retry?(error) ⇒ Boolean
148 149 150 |
# File 'lib/pindo/module/task/pindo_task.rb', line 148 def should_retry?(error) true # 默认所有错误都重试,子类可重写 end |
#validate ⇒ Object
验证任务是否可以执行
105 106 107 |
# File 'lib/pindo/module/task/pindo_task.rb', line 105 def validate true end |