Class: AWS::Flow::WorkflowDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/aws/decider/workflow_definition.rb

Overview

Represents a workflow definition. Every workflow implementation needs to be a subclass of this class.

Usually there should be no need to instantiate the class manually. Instead, the @execute method is called to start the workflow. You can think of this class as having factory class methods.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance, workflow_method, signals, get_state_method, converter) ⇒ WorkflowDefinition

Returns a new instance of WorkflowDefinition.



29
30
31
32
33
34
35
# File 'lib/aws/decider/workflow_definition.rb', line 29

def initialize(instance, workflow_method, signals, get_state_method, converter)
  @instance = instance
  @workflow_method = workflow_method
  @get_state_method = get_state_method
  @signals = signals
  @converter = converter
end

Instance Attribute Details

#converterObject (readonly)

Returns the value of attribute converter.



27
28
29
# File 'lib/aws/decider/workflow_definition.rb', line 27

def converter
  @converter
end

#decision_helperObject (readonly)

Returns the value of attribute decision_helper.



25
26
27
# File 'lib/aws/decider/workflow_definition.rb', line 25

def decision_helper
  @decision_helper
end

Instance Method Details

#execute(input = nil) ⇒ Object



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
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/aws/decider/workflow_definition.rb', line 37

def execute(input = nil)
  #TODO Set up all the converter stuff
  result = Future.new
  method_output = Future.new
  error_handler do |t|
    t.begin do
      if input.nil?
        method_output.set(@instance.send(@workflow_method))
      else
        ruby_input = @converter.load input
        # Have to have `ruby_input` in order to be able to handle sending
        # arbitrary arguments correctly, as otherwise it will seem as if
        # @workflow_method will always have an arity of 1.
        method_output.set(@instance.send(@workflow_method, *ruby_input))
      end
    end
    t.rescue(Exception) do |e|

      # Check if serialized exception violates the 32k limit and truncate it
      reason, converted_failure = AWS::Flow::Utilities::check_and_truncate_exception(e, @converter)

      # Wrap the exception that we got into a WorkflowException so that it
      # can be handled correctly.

      @failure = WorkflowException.new(reason, converted_failure)
    end
    t.ensure do
      raise @failure if @failure
      # We are going to have to convert this object into a string to submit it,
      # and that's where the 32k limit will be enforced, so it's valid to turn
      # the object to a string and check the size of the result
      output = @converter.dump method_output.get

      if output.to_s.size > FlowConstants::DATA_LIMIT
        raise WorkflowException.new(
          Utilities.validation_error_string_partial("Workflow"),
          ""
        )
      end
      result.set(output)
    end
    end
  return result
end

#get_workflow_stateObject



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/aws/decider/workflow_definition.rb', line 82

def get_workflow_state
  return nil if @get_state_method.nil?
  converter = @get_state_method.data_converter || @converter
  method = @get_state_method.method_name
  begin
    result = @instance.send(method)
    return converter.dump(result)
  rescue Exception => e
    raise WorkflowException.new(e.message, converter.dump(e))
  end
end

#signal_received(signal_name, input) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/aws/decider/workflow_definition.rb', line 96

def signal_received(signal_name, input)
  method_pair = @signals[signal_name]
  raise "No such signal for #{signal_name}" unless method_pair
  converter = method_pair.data_converter
  method_name = method_pair.method_name
  error_handler do |t|
    parameters = nil
    t.begin do
      if input.class <= NoInput
        @instance.send(method_name)
      else
        parameters = converter.load input
        @instance.send(method_name, *parameters)
      end
    end
    t.rescue(Exception) do |e|
      WorkflowException.new("Got an error while sending #{method_name} with parameters #{parameters}", converter.dump(e))
    end
  end
end