Class: Charming::Tasks::Task

Inherits:
Data
  • Object
show all
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

Instance Method Summary collapse

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

#blockObject (readonly)

Returns the value of attribute block

Returns:

  • (Object)

    the current value of block



10
11
12
# File 'lib/charming/tasks/task.rb', line 10

def block
  @block
end

#nameObject (readonly)

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



10
11
12
# File 'lib/charming/tasks/task.rb', line 10

def name
  @name
end

#timeoutObject (readonly)

Returns the value of attribute timeout

Returns:

  • (Object)

    the current value of 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