Class: AWS::Flow::Core::ExternalTask Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Used to bridge asynchronous execution to external asynchronous APIs or events. It is passed a block, like so:

external_task do |t|
  t.cancellation_handler { |h, cause| h.fail(cause) }
  t.initiate_task { |h| trace << :task_started; h.complete; }
end

The #initiate_task method is expected to call an external API and return without blocking. Completion or failure of the external task is reported through ExternalTaskCompletionHandle, which is passed into the initiate_task and cancellation_handler blocks. The cancellation handler, defined in the same block as the initiate_task, is used to report the cancellation of the external task.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from FlowFiber

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

Constructor Details

#initialize(options = {}, &block) ⇒ ExternalTask

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of ExternalTask.



205
206
207
208
209
210
211
212
# File 'lib/aws/flow/tasks.rb', line 205

def initialize(options = {}, &block)
  @inCancellationHandler = false
  @block = block
  # TODO: What should the default value be?
  @parent = options[:parent]
  @handle = ExternalTaskCompletionHandle.new(self)
  block.call(self)
end

Instance Attribute Details

#__context__Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



184
185
186
# File 'lib/aws/flow/tasks.rb', line 184

def __context__
  @__context__
end

#backtraceObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method is here because the way we create external tasks is a little tricky. If the parent isn’t passed in on construction(as is the case with the external_task function), then the parent will only be set after ExternalTask#initialize is called. We’d prefer to set it in initialize, however, the backtrace relies on the parent’s backtrace, and so we cannot do that. Instead, we use this method to lazily create it when it is called. The method itself simply sets the backtrace to the make_backtrace of the parent’s backtrace, if the backtrace is not already set, and will otherwise simply return the backtrace



225
226
227
# File 'lib/aws/flow/tasks.rb', line 225

def backtrace
  @backtrace
end

#blockObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



183
184
185
# File 'lib/aws/flow/tasks.rb', line 183

def block
  @block
end

#cancelledObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



184
185
186
# File 'lib/aws/flow/tasks.rb', line 184

def cancelled
  @cancelled
end

#inCancellationHandlerObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



184
185
186
# File 'lib/aws/flow/tasks.rb', line 184

def inCancellationHandler
  @inCancellationHandler
end

#parentObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



184
185
186
# File 'lib/aws/flow/tasks.rb', line 184

def parent
  @parent
end

Instance Method Details

#alive?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Part of the interface provided by fiber, has to overridden to properly reflect that an external tasks alive-ness relies on its ExternalTaskCompletionHandle.

Returns:

  • (Boolean)


250
251
252
# File 'lib/aws/flow/tasks.rb', line 250

def alive?
  ! @handle.completed
end

#cancel(cause) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/aws/flow/tasks.rb', line 255

def cancel(cause)
  return if @cancelled
  return if @handle.failure != nil || @handle.completed
  @cancelled = true
  if @cancellation_task != nil
    begin
      @inCancellationHandler = true
      @cancellation_task.call(cause)
    rescue Exception => e
      if ! self.backtrace.nil?
        backtrace = AsyncBacktrace.create_from_exception(@backtrace, e)
        e.set_backtrace(backtrace.backtrace) if backtrace
      end
      @handle.failure = e
    ensure
      @inCancellationHandler = false
      if ! @handle.failure.nil?
        fail_to_parent(@handle.failure)
      elsif @handle.completed
        remove_from_parent
      end
    end
  else
    remove_from_parent
  end
end

#cancellation_handler(&block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Store the passed-in cancellation handler block for later reference.



285
286
287
# File 'lib/aws/flow/tasks.rb', line 285

def cancellation_handler(&block)
  @cancellation_task = lambda { |cause| block.call(@handle, cause) }
end

#fail_to_parent(error) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Add a task that fails yourself with the suppiled error, and pass it through the parents executor.



240
241
242
# File 'lib/aws/flow/tasks.rb', line 240

def fail_to_parent(error)
  @__context__.executor << FlowFiber.new { @parent.fail(self, error) }
end

#get_heirsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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



200
201
202
# File 'lib/aws/flow/tasks.rb', line 200

def get_heirs
  @__context__.get_heirs
end

#initiate_task(&block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Store the passed-in block for later.



292
293
294
# File 'lib/aws/flow/tasks.rb', line 292

def initiate_task(&block)
  @initiation_task = lambda { block.call(@handle) }
end

#is_daemon?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Will always be false, provides a common API for BeginRescueEnsure’s to ensure they are maintaining their nonDaemonHeirsCount correctly.

Returns:

  • (Boolean)


191
192
193
# File 'lib/aws/flow/tasks.rb', line 191

def is_daemon?
  false
end

#remove_from_parentObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Add a task that removes yourself, and pass it through the parents executor.



232
233
234
# File 'lib/aws/flow/tasks.rb', line 232

def remove_from_parent
  @__context__.executor << FlowFiber.new { @parent.remove(self) }
end

#resumeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

From the interface provided by fiber, will execute the external task.



299
300
301
302
303
304
305
306
307
308
# File 'lib/aws/flow/tasks.rb', line 299

def resume
  return if @cancelled
  begin
    @cancellation_handler = @initiation_task.call
  rescue Exception => e
    backtrace = AsyncBacktrace.create_from_exception(self.backtrace, e)
    e.set_backtrace(backtrace.backtrace) if backtrace
    @parent.fail(self, e)
  end
end