Class: AWS::Flow::AsyncDecider

Inherits:
Object
  • Object
show all
Defined in:
aws-flow-ruby/aws-flow/lib/aws/decider/async_decider.rb

Overview

Represents an asynchronous decider class.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (AsyncDecider) initialize(workflow_definition_factory, history_helper, decision_helper)

Creates a new asynchronous decider.



193
194
195
196
197
198
199
200
201
202
203
204
# File 'aws-flow-ruby/aws-flow/lib/aws/decider/async_decider.rb', line 193

def initialize(workflow_definition_factory, history_helper, decision_helper)
  @workflow_definition_factory = workflow_definition_factory
  @history_helper = history_helper
  @decision_helper = decision_helper
  @decision_task = history_helper.get_decision_task
  @workflow_clock = WorkflowClock.new(@decision_helper)

  @workflow_context = WorkflowContext.new(@decision_task, @workflow_clock)
  @activity_client = GenericActivityClient.new(@decision_helper, nil)
  @workflow_client = GenericWorkflowClient.new(@decision_helper, @workflow_context)
  @decision_context = DecisionContext.new(@activity_client, @workflow_client, @workflow_clock, @workflow_context, @decision_helper)
end

Instance Attribute Details

- (Object) decision_helper

Returns the value of attribute decision_helper



190
191
192
# File 'aws-flow-ruby/aws-flow/lib/aws/decider/async_decider.rb', line 190

def decision_helper
  @decision_helper
end

- (Object) task_token

Returns the value of attribute task_token



190
191
192
# File 'aws-flow-ruby/aws-flow/lib/aws/decider/async_decider.rb', line 190

def task_token
  @task_token
end

Instance Method Details

- (Object) complete_workflow

Registers a CompleteWorkflowExecution decision.



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'aws-flow-ruby/aws-flow/lib/aws/decider/async_decider.rb', line 326

def complete_workflow
  return unless @completed && ! @unhandled_decision
  decision_id = [:SELF, nil]
  if @failure
    @decision_helper[decision_id] = make_fail_decision(decision_id, @failure)
  elsif @cancel_requested
        @decision_helper[decision_id] = make_cancel_decision(decision_id)
  else

    if ! @workflow_context.continue_as_new_options.nil?
      @decision_helper[decision_id] = continue_as_new_workflow(decision_id, @workflow_context.continue_as_new_options)
    else
      if @result.nil?
        @decision_helper[decision_id] = make_completion_decision(decision_id, {
                                                                   :decision_type => "CompleteWorkflowExecution"})
      else
        @decision_helper[decision_id] = make_completion_decision(decision_id, {
                                                                   :decision_type => "CompleteWorkflowExecution",
                                                                   :complete_workflow_execution_decision_attributes => {:result => @result.get }})
      end
    end
  end
end

- (true, false) completed?

Indicates whether the task completed.

Returns:

  • (true, false)

    Returns true if the task is completed; false otherwise.



355
356
357
# File 'aws-flow-ruby/aws-flow/lib/aws/decider/async_decider.rb', line 355

def completed?
  @completed
end

- (Object) continue_as_new_workflow(decision_id, continue_as_new_options)

Continues this as a new workflow, using the provided decision and options.

Parameters:

  • decision_id (DecisionID)

    The decision ID to use.

  • continue_as_new_options (WorkflowOptions)

    The options to use for the new workflow.



310
311
312
313
314
315
316
317
318
319
# File 'aws-flow-ruby/aws-flow/lib/aws/decider/async_decider.rb', line 310

def continue_as_new_workflow(decision_id, continue_as_new_options)
  result = {
    :decision_type => "ContinueAsNewWorkflowExecution",
  }

  task_list = continue_as_new_options.task_list ? {:task_list => {:name => continue_as_new_options.task_list}} : {}
  to_add = continue_as_new_options.get_options([:execution_start_to_close_timeout, :task_start_to_close_timeout, :task_priority, :child_policy, :tag_list, :workflow_type_version, :input], task_list)
  result[:continue_as_new_workflow_execution_decision_attributes] = to_add
  CompleteWorkflowStateMachine.new(decision_id, result)
end

- (Object) handle_activity_task_cancel_requested(event)

Handler for the :ActivityTaskCancelRequested event.

Parameters:

  • event (Object)

    The event to process.



435
436
437
438
# File 'aws-flow-ruby/aws-flow/lib/aws/decider/async_decider.rb', line 435

def handle_activity_task_cancel_requested(event)
  activity_id = event.attributes[:activity_id]
  @decision_helper[activity_id].consume(:handle_cancellation_initiated_event)
end

- (Object) handle_activity_task_scheduled(event)

Handler for the :ActivityTaskScheduled event.

Parameters:

  • event (Object)

    The event to process.



364
365
366
367
368
369
# File 'aws-flow-ruby/aws-flow/lib/aws/decider/async_decider.rb', line 364

def handle_activity_task_scheduled(event)
  activity_id = event.attributes[:activity_id]
  @decision_helper.activity_scheduling_event_id_to_activity_id[event.id] = activity_id
  @decision_helper[activity_id].consume(:handle_initiated_event)
  return @decision_helper[activity_id].done?
end

- (Object) handle_cancel_timer_failed(event)

Handler for the :CancelTimerFailed event.

Parameters:

  • event (Object)

    The event to process.



573
574
575
576
577
578
# File 'aws-flow-ruby/aws-flow/lib/aws/decider/async_decider.rb', line 573

def handle_cancel_timer_failed(event)
  handle_event(event, {
                 :id_methods => [:timer_id],
                 :consume_symbol => :handle_cancellation_failure_event
               })
end

- (Object) handle_cancel_workflow_execution_failed(event)

Handler for the :CancelWorkflowExecutionFailed event.

Parameters:

  • event (Object)

    The event to process.



476
477
478
# File 'aws-flow-ruby/aws-flow/lib/aws/decider/async_decider.rb', line 476

def handle_cancel_workflow_execution_failed(event)
  handle_closing_failure
end

- (Object) handle_closing_failure



450
451
452
453
# File 'aws-flow-ruby/aws-flow/lib/aws/decider/async_decider.rb', line 450

def handle_closing_failure
  @unhandled_decision = true
  @decision_helper[[:SELF, nil]].consume(:handle_initiation_failed_event)
end

- (Object) handle_complete_workflow_execution_failed(event)

Handler for the :CompleteWorkflowExecutionFailed event.

Parameters:

  • event (Object)

    The event to process.



458
459
460
# File 'aws-flow-ruby/aws-flow/lib/aws/decider/async_decider.rb', line 458

def handle_complete_workflow_execution_failed(event)
  handle_closing_failure
end

- (Object) handle_continue_as_new_workflow_execution_failed(event)

Handler for the :ContinueAsNewWorkflowExecutionFailed event.

Parameters:

  • event (Object)

    The event to process.



485
486
487
# File 'aws-flow-ruby/aws-flow/lib/aws/decider/async_decider.rb', line 485

def handle_continue_as_new_workflow_execution_failed(event)
  handle_closing_failure
end

- (Object) handle_fail_workflow_execution_failed(event)

Handler for the :FailWorkflowExecutionFailed event.

Parameters:

  • event (Object)

    The event to process.



467
468
469
# File 'aws-flow-ruby/aws-flow/lib/aws/decider/async_decider.rb', line 467

def handle_fail_workflow_execution_failed(event)
  handle_closing_failure
end

- (Object) handle_request_cancel_activity_task_failed(event)

Handler for the :RequestCancelActivityTaskFailed event.

Parameters:

  • event (Object)

    The event to process.



443
444
445
446
447
448
# File 'aws-flow-ruby/aws-flow/lib/aws/decider/async_decider.rb', line 443

def handle_request_cancel_activity_task_failed(event)
  handle_event(event, {
                 :id_methods => [:activity_id],
                 :consume_symbol => :handle_cancellation_failure_event
               })
end

- (Object) handle_request_cancel_external_workflow_execution_failed(event)

Handler for the :RequestCancelExternalWorkflowExecutionFailed event.

Parameters:

  • event (Object)

    The event to process.



549
550
551
552
553
554
# File 'aws-flow-ruby/aws-flow/lib/aws/decider/async_decider.rb', line 549

def handle_request_cancel_external_workflow_execution_failed(event)
  handle_event(event, {
                 :id_methods => [:workflow_id],
                 :consume_symbol => :handle_cancellation_failure_event
               })
end

- (Object) handle_request_cancel_external_workflow_execution_initiated(event)

Handler for the :RequestCancelExternalWorkflowExecutionInitiated event.

Parameters:

  • event (Object)

    The event to process.



537
538
539
540
541
542
# File 'aws-flow-ruby/aws-flow/lib/aws/decider/async_decider.rb', line 537

def handle_request_cancel_external_workflow_execution_initiated(event)
  handle_event(event, {
                 :id_methods => [:workflow_id],
                 :consume_symbol => :handle_cancellation_initiated_event
               })
end

- (Object) handle_signal_external_workflow_execution_initiated(event)

Handler for the :SignalExternalWorkflowExecutionInitiated event.

Parameters:

  • event (Object)

    The event to process.



525
526
527
528
529
530
# File 'aws-flow-ruby/aws-flow/lib/aws/decider/async_decider.rb', line 525

def handle_signal_external_workflow_execution_initiated(event)
  signal_id = event.attributes[:control]
  @decision_helper.signal_initiated_event_to_signal_id[event.id] = signal_id
  @decision_helper[signal_id].consume(:handle_initiated_event)
  @decision_helper[signal_id].done?
end

- (Object) handle_start_child_workflow_execution_initiated(event)

Handler for the :StartChildWorkflowExecutionInitiated event.

Parameters:

  • event (Object)

    The event to process.



561
562
563
564
565
566
# File 'aws-flow-ruby/aws-flow/lib/aws/decider/async_decider.rb', line 561

def handle_start_child_workflow_execution_initiated(event)
  workflow_id = event.attributes[:workflow_id]
  @decision_helper.child_initiated_event_id_to_workflow_id[event.id] = workflow_id
  @decision_helper[workflow_id].consume(:handle_initiated_event)
  @decision_helper[workflow_id].done?
end

- (Object) handle_start_timer_failed(event)

Handler for the :StartTimerFailed event.

Parameters:

  • event (Object)

    The event to process.



407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# File 'aws-flow-ruby/aws-flow/lib/aws/decider/async_decider.rb', line 407

def handle_start_timer_failed(event)
  timer_id = event.attributes.timer_id
  return if timer_id == DecisionHelper.force_immediate_decision_timer
  handle_event(event, {
                 :id_methods => [:timer_id],
                 :consume_symbol => :handle_completion_event,
                 :decision_helper_scheduled => :scheduled_timers,
                 :handle_open_request => lambda do |event, open_request|
                   exception = StartTimerFailedException(event.id, timer_id, nil, event.attributes.cause)
                   open_request.completion_handle.fail(exception)
                 end
               })
  state_machine = @decision_helper[timer_id]


end

- (Object) handle_timer_canceled(event)

Handler for the :TimerCanceled event.

Parameters:

  • event (Object)

    The event to process.



506
507
508
509
510
511
512
513
514
515
516
517
518
# File 'aws-flow-ruby/aws-flow/lib/aws/decider/async_decider.rb', line 506

def handle_timer_canceled(event)
  handle_event(event, {
                 :id_methods => [:timer_id],
                 :consume_symbol => :handle_cancellation_event,
                 :decision_helper_scheduled => :scheduled_timers,
                 :handle_open_request => lambda do |event, open_request|
                   if ! open_request.nil?
                     cancellation_exception = CancellationException.new("Cancelled from a Timer Cancelled event")
                     open_request.completion_handle.fail(cancellation_exception)
                   end
                 end
               })
end

- (Object) handle_timer_fired(event)

Handler for the :TimerFired event.

Parameters:

  • event (Object)

    The event to process.



390
391
392
393
394
395
396
397
398
399
400
# File 'aws-flow-ruby/aws-flow/lib/aws/decider/async_decider.rb', line 390

def handle_timer_fired(event)
  timer_id = event.attributes[:timer_id]
  return if timer_id == DecisionHelper.force_immediate_decision_timer
  @decision_helper[timer_id].consume(:handle_completion_event)
  if @decision_helper[timer_id].done?
    open_request = @decision_helper.scheduled_timers.delete(timer_id)
    return if open_request.nil?
    open_request.blocking_promise.set(nil)
    open_request.completion_handle.complete
  end
end

- (Object) handle_timer_started(event)

Handler for the :TimerStarted event.

Parameters:

  • event (Object)

    The event to process.



494
495
496
497
498
499
# File 'aws-flow-ruby/aws-flow/lib/aws/decider/async_decider.rb', line 494

def handle_timer_started(event)
  timer_id = event.attributes[:timer_id]
  return if timer_id == DecisionHelper.force_immediate_decision_timer
  @decision_helper[timer_id].consume(:handle_initiated_event)
  @decision_helper[timer_id].done?
end

- (Object) handle_workflow_execution_cancel_requested(event)

Handler for the :WorkflowExecutionCancelRequested event.

Parameters:

  • event (Object)

    The event to process.



427
428
429
430
# File 'aws-flow-ruby/aws-flow/lib/aws/decider/async_decider.rb', line 427

def handle_workflow_execution_cancel_requested(event)
  @workflow_async_scope.cancel(CancellationException.new("Cancelled from a WorkflowExecutionCancelRequested"))
  @cancel_requested = true
end

- (Object) handle_workflow_execution_signaled(event)

Handler for the WorkflowExecutionSignaled event.

Parameters:

  • event (Object)

    The event to process.



585
586
587
588
589
590
591
592
593
594
595
# File 'aws-flow-ruby/aws-flow/lib/aws/decider/async_decider.rb', line 585

def handle_workflow_execution_signaled(event)
  signal_name = event.attributes[:signal_name]
  input = event.attributes[:input] if event.attributes.keys.include? :input
  input ||= NoInput.new
  # TODO do stuff if we are @completed
  t = Task.new(nil) do
    @definition.signal_received(signal_name, input)
  end
  task_context = TaskContext.new(:parent => @workflow_async_scope.get_closest_containing_scope, :task => t)
  @workflow_async_scope.get_closest_containing_scope << t
end

- (Object) handle_workflow_execution_started(event)

Handler for the :WorkflowExecutionStarted event.

Parameters:

  • event (Object)

    The event to process.



376
377
378
379
380
381
382
383
# File 'aws-flow-ruby/aws-flow/lib/aws/decider/async_decider.rb', line 376

def handle_workflow_execution_started(event)
  @workflow_async_scope = AsyncScope.new do
    FlowFiber.current[:decision_context] = @decision_context
    input = (event.attributes.keys.include? :input) ?  event.attributes[:input] : nil
    @definition = @workflow_definition_factory.get_workflow_definition(@decision_context)
    @result = @definition.execute(input)
  end
end

- (Object) make_cancel_decision(decision_id)



299
300
301
# File 'aws-flow-ruby/aws-flow/lib/aws/decider/async_decider.rb', line 299

def make_cancel_decision(decision_id)
  CompleteWorkflowStateMachine.new(decision_id, {:decision_type => "CancelWorkflowExecution"})
end

- (Object) make_completion_decision(decision_id, decision)



296
297
298
# File 'aws-flow-ruby/aws-flow/lib/aws/decider/async_decider.rb', line 296

def make_completion_decision(decision_id, decision)
  CompleteWorkflowStateMachine.new(decision_id, decision)
end

- (Object) make_fail_decision(decision_id, failure)

failing in this execution. This information can be useful for tracing the sequence of events back from the failure.

Parameters:

  • failure (Exception)

    The exception that is associated with the failed workflow.

See Also:



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'aws-flow-ruby/aws-flow/lib/aws/decider/async_decider.rb', line 278

def make_fail_decision(decision_id, failure)
  decision_type = "FailWorkflowExecution"

  # Get the reason from the failure. Or get the message if a
  # CancellationException is initialized without a reason. Fall back to
  # a default string if nothing is provided
  reason = failure.reason || failure.message || "Workflow failure did not provide any reason."
  # Get the details from the failure. Or get the backtrace if a
  # CancellationException is initialized without a details. Fall back to
  # a default string if nothing is provided
  details = failure.details || failure.backtrace.to_s || "Workflow failure did not provide any details."

  fail_workflow_execution_decision_attributes = { reason: reason, details: details }
  decision = {:decision_type => decision_type, :fail_workflow_execution_decision_attributes => fail_workflow_execution_decision_attributes}
  CompleteWorkflowStateMachine.new(decision_id, decision)

end

- (Object) process_event(event)

Processes decider events.

Parameters:

  • event (Object)

    The event to process.



602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
# File 'aws-flow-ruby/aws-flow/lib/aws/decider/async_decider.rb', line 602

def process_event(event)
  event_type_symbol = event.event_type.to_sym
  # Mangle the name so that it is handle_ + the name of the event type in snakecase
  handle_event = "handle_" + event.event_type.gsub(/(.)([A-Z])/,'\1_\2').downcase
  noop_set = Set.new([:DecisionTaskScheduled, :DecisionTaskCompleted,
  :DecisionTaskStarted, :DecisionTaskTimedOut, :WorkflowExecutionTimedOut,
  :WorkflowExecutionTerminated, :MarkerRecorded,
  :WorkflowExecutionCompleted, :WorkflowExecutionFailed,
  :WorkflowExecutionCanceled, :WorkflowExecutionContinuedAsNew, :ActivityTaskStarted])

  return if noop_set.member? event_type_symbol

  self_set = Set.new([:TimerFired, :StartTimerFailed,
  :WorkflowExecutionCancel, :ActivityTaskScheduled,
  :WorkflowExecutionCancelRequested,
  :ActivityTaskCancelRequested, :RequestCancelActivityTaskFailed,
  :CompleteWorkflowExecutionFailed, :FailWorkflowExecutionFailed,
  :CancelWorkflowExecutionFailed, :ContinueAsNewWorkflowExecutionFailed,
  :TimerStarted, :TimerCanceled,
  :SignalExternalWorkflowExecutionInitiated,
  :RequestCancelExternalWorkflowExecutionInitiated,
  :RequestCancelExternalWorkflowExecutionFailed,
  :StartChildWorkflowExecutionInitiated, :CancelTimerFailed, :WorkflowExecutionStarted, :WorkflowExecutionSignaled])

  activity_client_set = Set.new([:ActivityTaskCompleted,
  :ActivityTaskCanceled, :ActivityTaskTimedOut,
  :ScheduleActivityTaskFailed, :ActivityTaskFailed])

  workflow_client_set =
  Set.new([:ExternalWorkflowExecutionCancelRequested,
  :ChildWorkflowExecutionCanceled, :ChildWorkflowExecutionCompleted,
  :ChildWorkflowExecutionFailed,
  :ChildWorkflowExecutionStarted, :ChildWorkflowExecutionTerminated,
  :ChildWorkflowExecutionTimedOut, :ExternalWorkflowExecutionSignaled,
  :SignalExternalWorkflowExecutionFailed,
  :StartChildWorkflowExecutionFailed])

  event_set_to_object_mapping = { self_set => self,
    activity_client_set => @activity_client,
    workflow_client_set => @workflow_client }
  thing_to_operate_on = event_set_to_object_mapping.map {|key, value|
    value if key.member? event_type_symbol }.compact.first
  thing_to_operate_on.send(handle_event, event)
    # DecisionTaskStarted is taken care of at TODO
end