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.
-
#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
Returns the value of attribute retry_delay.
-
#retry_mode ⇒ Object
readonly
Returns the value of attribute retry_mode.
-
#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 |
# 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] || {} @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 @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 |
#metadata ⇒ Object
Returns the value of attribute metadata.
36 37 38 |
# File 'lib/pindo/module/task/pindo_task.rb', line 36 def 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)
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_mode ⇒ Object (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_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
80 81 82 |
# File 'lib/pindo/module/task/pindo_task.rb', line 80 def self.default_retry_count 0 # 默认不重试 end |
.default_retry_delay ⇒ Object
84 85 86 |
# File 'lib/pindo/module/task/pindo_task.rb', line 84 def self.default_retry_delay 10 # 默认延迟 10 秒 end |
.default_retry_mode ⇒ Object
默认重试配置
76 77 78 |
# File 'lib/pindo/module/task/pindo_task.rb', line 76 def self.default_retry_mode RetryMode::IMMEDIATE end |
.task_type ⇒ Object
子类必须实现的方法
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_retry ⇒ Object
151 152 153 |
# File 'lib/pindo/module/task/pindo_task.rb', line 151 def before_retry # 子类可重写,进行重试前的清理工作 end |
#cancel ⇒ Object
取消任务
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
检查是否已取消(不抛异常)
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_task ⇒ Object
执行任务(在主线程中同步执行)
89 90 91 |
# File 'lib/pindo/module/task/pindo_task.rb', line 89 def do_task execute_internal end |
#execution_time ⇒ Object
执行时间
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
检查是否完成
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_retry ⇒ Object
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
重试相关方法
143 144 145 |
# File 'lib/pindo/module/task/pindo_task.rb', line 143 def retryable? @retry_count > 0 end |
#running? ⇒ Boolean
是否正在运行
99 100 101 |
# File 'lib/pindo/module/task/pindo_task.rb', line 99 def running? @status == TaskStatus::RUNNING end |
#should_retry?(error) ⇒ Boolean
147 148 149 |
# File 'lib/pindo/module/task/pindo_task.rb', line 147 def should_retry?(error) true # 默认所有错误都重试,子类可重写 end |
#validate ⇒ Object
验证任务是否可以执行
104 105 106 |
# File 'lib/pindo/module/task/pindo_task.rb', line 104 def validate true end |