Class: Charming::Tasks::Task
- Inherits:
-
Data
- Object
- Data
- Charming::Tasks::Task
- Defined in:
- lib/charming/tasks/task.rb
Overview
Task is the unit of work submitted to a task executor. It pairs a name (used by ‘on_task` handlers to route the result) with a block to invoke on the executor, plus an optional timeout in seconds.
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Instance Method Summary collapse
-
#call(progress = nil) ⇒ Object
Invokes the task’s block, passing progress when the block accepts an argument.
-
#initialize(name:, block:, timeout: nil) ⇒ Task
constructor
A new instance of Task.
Constructor Details
#initialize(name:, block:, timeout: nil) ⇒ Task
Returns a new instance of Task.
11 12 13 |
# File 'lib/charming/tasks/task.rb', line 11 def initialize(name:, block:, timeout: nil) super end |
Instance Attribute Details
#block ⇒ Object (readonly)
Returns the value of attribute block
10 11 12 |
# File 'lib/charming/tasks/task.rb', line 10 def block @block end |
#name ⇒ Object (readonly)
Returns the value of attribute name
10 11 12 |
# File 'lib/charming/tasks/task.rb', line 10 def name @name end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout
10 11 12 |
# File 'lib/charming/tasks/task.rb', line 10 def timeout @timeout end |
Instance Method Details
#call(progress = nil) ⇒ Object
Invokes the task’s block, passing progress when the block accepts an argument. Enforces the timeout (raising Tasks::Cancelled) when one was configured.
17 18 19 20 21 22 23 |
# File 'lib/charming/tasks/task.rb', line 17 def call(progress = nil) return invoke(progress) unless timeout Timeout.timeout(timeout, Cancelled, "task #{name} timed out after #{timeout}s") do invoke(progress) end end |