Class: AWS::Flow::Core::BeginRescueEnsure

Inherits:
FlowFiber
  • Object
show all
Extended by:
SimpleDFA
Defined in:
lib/aws/flow/begin_rescue_ensure.rb

Overview

This class allows asynchronous error handling within the AWS Flow Framework for Ruby. Calling #begin/#rescue/#ensure is similar to Ruby’s native ‘begin`/`rescue`/`end` semantics.

Direct Known Subclasses

DaemonBeginRescueEnsure

Instance Attribute Summary collapse

Attributes included from SimpleDFA

#start_state, #states, #symbols, #transitions

Instance Method Summary collapse

Methods included from SimpleDFA

add_transition, define_general, get_start_state, get_transitions, init, uncovered_transitions

Methods inherited from FlowFiber

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

Constructor Details

#initialize(options = {}) ⇒ BeginRescueEnsure

Create a new BeginRescueEnsure object, with the provided options.

Parameters:

  • options (defaults to: {})

    Options to set for the class.

Options Hash (options):

  • :parent (Object)

    The parent object.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/aws/flow/begin_rescue_ensure.rb', line 40

def initialize(options = {})
  # We have two different arrays, rather than a hash,
  # because we want to ensure that we process the rescues in the order
  # they are written, and because prior to Ruby 1.9, hashes will not
  # return their elements in the order they were inserted.
  @rescue_exceptions = []
  @rescue_tasks = []
  @parent = options[:parent] || Fiber.current.__context__
  @current = @parent
  @executor = @parent.executor
  @__context__ = self
  @nonDaemonHeirsCount = 0
  @current_state ||= self.class.get_start_state
  @heirs = Set.new
  @backtrace = make_backtrace(@parent.backtrace)
  @result = Future.new
  super() { consume(:run) }
end

Instance Attribute Details

#__context__Object (readonly)

Returns the value of attribute __context__.



30
31
32
# File 'lib/aws/flow/begin_rescue_ensure.rb', line 30

def __context__
  @__context__
end

#backtraceObject (readonly)

Returns the value of attribute backtrace.



30
31
32
# File 'lib/aws/flow/begin_rescue_ensure.rb', line 30

def backtrace
  @backtrace
end

#begin_taskObject

Returns the value of attribute begin_task.



28
29
30
# File 'lib/aws/flow/begin_rescue_ensure.rb', line 28

def begin_task
  @begin_task
end

#cancelledObject

Returns the value of attribute cancelled.



28
29
30
# File 'lib/aws/flow/begin_rescue_ensure.rb', line 28

def cancelled
  @cancelled
end

#ensure_taskObject

Returns the value of attribute ensure_task.



28
29
30
# File 'lib/aws/flow/begin_rescue_ensure.rb', line 28

def ensure_task
  @ensure_task
end

#executorObject

Returns the value of attribute executor.



28
29
30
# File 'lib/aws/flow/begin_rescue_ensure.rb', line 28

def executor
  @executor
end

#failureObject

Returns the value of attribute failure.



28
29
30
# File 'lib/aws/flow/begin_rescue_ensure.rb', line 28

def failure
  @failure
end

#heirsObject

Returns the value of attribute heirs.



28
29
30
# File 'lib/aws/flow/begin_rescue_ensure.rb', line 28

def heirs
  @heirs
end

#nonDaemonHeirsCountObject

Returns the value of attribute nonDaemonHeirsCount.



28
29
30
# File 'lib/aws/flow/begin_rescue_ensure.rb', line 28

def nonDaemonHeirsCount
  @nonDaemonHeirsCount
end

#parentObject

Returns the value of attribute parent.



28
29
30
# File 'lib/aws/flow/begin_rescue_ensure.rb', line 28

def parent
  @parent
end

#rescue_exceptionsObject

Returns the value of attribute rescue_exceptions.



28
29
30
# File 'lib/aws/flow/begin_rescue_ensure.rb', line 28

def rescue_exceptions
  @rescue_exceptions
end

#rescue_tasksObject

Returns the value of attribute rescue_tasks.



28
29
30
# File 'lib/aws/flow/begin_rescue_ensure.rb', line 28

def rescue_tasks
  @rescue_tasks
end

#resultObject

Returns the value of attribute result.



28
29
30
# File 'lib/aws/flow/begin_rescue_ensure.rb', line 28

def result
  @result
end

Instance Method Details

#begin(block) ⇒ Object

Binds the block to the a lambda to be called when we get to the begin part of the DFA

Parameters:

  • block

    The code block to be called when asynchronous begin starts.



265
266
267
268
269
# File 'lib/aws/flow/begin_rescue_ensure.rb', line 265

def begin(block)
  raise "Duplicated begin" if @begin_task
  # @begin_task = lambda { block.call }
  @begin_task = Task.new(self) { @result.set(block.call) }
end

#ensure(block) ⇒ Object

Binds the block to the a lambda to be called when we get to the ensure part of the DFA

Parameters:

  • block

    The code block to be called when asynchronous ensure starts.



293
294
295
296
# File 'lib/aws/flow/begin_rescue_ensure.rb', line 293

def ensure(block)
  raise "Duplicated ensure" if @ensure_task
  @ensure_task = Task.new(self) { block.call }
end

#fail(this_task, error) ⇒ Object

Fails the task, cancels all of its heirs, and then updates the state.

Parameters:

  • this_task

    The task to fail.

  • error

    The error associated with the failure.



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/aws/flow/begin_rescue_ensure.rb', line 103

def fail(this_task, error)
  check_closed
  if ( ! (error.class <= CancellationException) || @failure == nil && !@daemondCausedCancellation)
    backtrace = AsyncBacktrace.create_from_exception(@backtrace, error)
    error.set_backtrace(backtrace.backtrace) if backtrace
    @failure = error
  end
  task_out = @heirs.delete?(this_task)
  raise "There was a task attempted to be removed from a BRE, when the BRE did not have that task as an heir" unless task_out
  @nonDaemonHeirsCount -= 1 if ! this_task.is_daemon?
  cancelHeirs
  update_state
end

#remove(this_task) ⇒ Object

Removes the task and updates the state

Parameters:

  • this_task

    The task to remove.



122
123
124
125
126
127
128
129
# File 'lib/aws/flow/begin_rescue_ensure.rb', line 122

def remove(this_task)
  check_closed

  task_out = @heirs.delete?(this_task)
  raise "There was a task attempted to be removed from a BRE, when the BRE did not have that task as an heir" unless task_out
  @nonDaemonHeirsCount -= 1 if ! this_task.is_daemon?
  update_state
end

#rescue(error_type, block) ⇒ Object

Binds the block to the a lambda to be called when we get to the rescue part of the DFA

Parameters:

  • error_type

    The error type.

  • block

    The code block to be called when asynchronous rescue starts.



279
280
281
282
283
284
285
286
# File 'lib/aws/flow/begin_rescue_ensure.rb', line 279

def rescue(error_type, block)
  this_task = lambda { |failure| block.call(failure) }
  if @rescue_exceptions.include? error_type
    raise "You have already registered #{error_type}!"
  end
  @rescue_exceptions << error_type
  @rescue_tasks << this_task
end

#scheduleObject



298
299
300
# File 'lib/aws/flow/begin_rescue_ensure.rb', line 298

def schedule
  @parent << self
end