Class: AWS::Flow::Core::Task

Inherits:
FlowFiber
  • Object
show all
Defined in:
lib/aws/flow/tasks.rb

Direct Known Subclasses

DaemonTask

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from FlowFiber

#[], [], []=, #[]=, finalize, unset

Constructor Details

#initialize(__context__, &block) ⇒ Task

Creates a new Task.

Parameters:

  • __context__

    A Task needs a reference to the __context__ that created it so that when the “task” macro is called it can find the __context__ which the new Task should be added to.

  • block

    A block of code that will be run by the task.



34
35
36
37
38
39
40
41
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/aws/flow/tasks.rb', line 34

def initialize(__context__, &block)
  @__context__ = __context__
  @result = Future.new
  @block = block

  # Is the task alive?
  def alive?
    super && !@cancelled
    #!!@alive# && !@cancelled
  end

  # @return
  #   The executor for this task.
  def executor
    @__context__.executor
  end

  super() do
    begin
      # Not return because 1.9 will freak about local jump problems if you
      # try to return, as this is inside a block
      next if @cancelled
      @result.set(lambda(&block).call)
      next if @cancelled
      @__context__.remove(self)
    rescue Exception => e
      if @backtrace != e
        backtrace = AsyncBacktrace.create_from_exception(@backtrace, e)
        e.set_backtrace(backtrace.backtrace) if backtrace
      end
      @__context__.fail(self, e)
    ensure
    end
  end
end

Instance Attribute Details

#__context__Object

Returns the value of attribute __context__.



23
24
25
# File 'lib/aws/flow/tasks.rb', line 23

def __context__
  @__context__
end

#backtraceObject

Returns the value of attribute backtrace.



23
24
25
# File 'lib/aws/flow/tasks.rb', line 23

def backtrace
  @backtrace
end

#blockObject (readonly)

Returns the value of attribute block.



22
23
24
# File 'lib/aws/flow/tasks.rb', line 22

def block
  @block
end

#parentObject

Returns the value of attribute parent.



23
24
25
# File 'lib/aws/flow/tasks.rb', line 23

def parent
  @parent
end

#resultObject (readonly)

Returns the value of attribute result.



22
23
24
# File 'lib/aws/flow/tasks.rb', line 22

def result
  @result
end

Instance Method Details

#<<(this_task) ⇒ Object

Adds a task to this task’s context

Parameters:

  • this_task

    The task to add.



120
121
122
# File 'lib/aws/flow/tasks.rb', line 120

def <<(this_task)
  @__context__.parent << this_task
end

#alive?Boolean

Is the task alive?

Returns:

  • (Boolean)


40
41
42
43
# File 'lib/aws/flow/tasks.rb', line 40

def alive?
  super && !@cancelled
  #!!@alive# && !@cancelled
end

#cancel(error) ⇒ Object

Cancel will prevent the execution of this particular task, if possible

Parameters:

  • error

    The error that is the cause of the cancellation



97
98
99
100
# File 'lib/aws/flow/tasks.rb', line 97

def cancel(error)
  @cancelled = true
  @__context__.remove(self)
end

#executorObject

Returns The executor for this task.

Returns:

  • The executor for this task.



47
48
49
# File 'lib/aws/flow/tasks.rb', line 47

def executor
  @__context__.executor
end

#get_heirsObject

Passes the get_heirs calls to the context, to ensure uniform handling of get_heirs



73
74
75
# File 'lib/aws/flow/tasks.rb', line 73

def get_heirs
  @__context__.get_heirs
end

#is_daemon?Boolean

Returns true/false, depending on if we are in a daemon task or not

Returns:

  • (Boolean)


80
81
82
# File 'lib/aws/flow/tasks.rb', line 80

def is_daemon?
  return false
end

#remove(this_task) ⇒ Object

Removes a task from this task’s context

Parameters:

  • this_task

    The task to remove.



129
130
131
# File 'lib/aws/flow/tasks.rb', line 129

def remove(this_task)
  @__context__.remove(this_task)
end

#scheduleObject

Used by Future#signal to schedule the task for re-evaluation.

This will simply add the task back to the list of things to be run in the parent’s event loop.



88
89
90
# File 'lib/aws/flow/tasks.rb', line 88

def schedule
  @__context__ << self
end