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
# 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 |error|
      @failure = WorkflowException.new(error.message, @converter.dump(error))
      #TODO error handling stuff
    end
    t.ensure do
      raise @failure if @failure
      result.set(@converter.dump method_output.get)
    end
  end
  return result
end

#get_workflow_stateObject



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/aws/decider/workflow_definition.rb', line 65

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



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/aws/decider/workflow_definition.rb', line 79

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