Class: StatesLanguageMachine::States::Task

Inherits:
Base show all
Defined in:
lib/ruby_slm/states/task.rb

Constant Summary collapse

INTRINSIC_FUNCTIONS =

Intrinsic functions supported by AWS Step Functions

%w[
  States.Format States.StringToJson States.JsonToString
  States.Array States.ArrayPartition States.ArrayContains
  States.ArrayRange States.ArrayGetItem States.ArrayLength
  States.ArrayUnique States.Base64Encode States.Base64Decode
  States.Hash States.JsonMerge States.MathRandom
  States.MathAdd States.StringSplit States.UUID
].freeze

Instance Attribute Summary collapse

Attributes inherited from Base

#definition

Attributes inherited from StatesLanguageMachine::State

#end_state, #name, #next_state, #type

Instance Method Summary collapse

Methods inherited from StatesLanguageMachine::State

#end_state?, #next_state_name

Constructor Details

#initialize(name, definition) ⇒ Task

Returns a new instance of Task.

Parameters:

  • name (String)

    the name of the state

  • definition (Hash)

    the state definition



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ruby_slm/states/task.rb', line 47

def initialize(name, definition)
  super
  @resource = definition["Resource"]
  @timeout_seconds = definition["TimeoutSeconds"]
  @heartbeat_seconds = definition["HeartbeatSeconds"]
  @parameters = definition["Parameters"] || {}
  @result_path = definition["ResultPath"]
  @result_selector = definition["ResultSelector"]
  @input_path = definition["InputPath"]
  @output_path = definition["OutputPath"]
  @credentials = definition["Credentials"]
  @comment = definition["Comment"]
  @retry = definition["Retry"] || []
  @catch = definition["Catch"] || []

  # Initialize retry and catch objects
  @retry_objects = @retry.map { |r| RetryPolicy.new(r) }
  @catch_objects = @catch.map { |c| CatchPolicy.new(c) }

  validate!
end

Instance Attribute Details

#catchArray<Hash> (readonly)

Returns the catch configuration.

Returns:

  • (Array<Hash>)

    the catch configuration



19
20
21
# File 'lib/ruby_slm/states/task.rb', line 19

def catch
  @catch
end

#commentString? (readonly)

Returns the comment for the state.

Returns:

  • (String, nil)

    the comment for the state



33
34
35
# File 'lib/ruby_slm/states/task.rb', line 33

def comment
  @comment
end

#credentialsString? (readonly)

Returns the credentials ARN for the task.

Returns:

  • (String, nil)

    the credentials ARN for the task



31
32
33
# File 'lib/ruby_slm/states/task.rb', line 31

def credentials
  @credentials
end

#heartbeat_secondsInteger? (readonly)

Returns the heartbeat interval in seconds.

Returns:

  • (Integer, nil)

    the heartbeat interval in seconds



15
16
17
# File 'lib/ruby_slm/states/task.rb', line 15

def heartbeat_seconds
  @heartbeat_seconds
end

#input_pathString? (readonly)

Returns the input path.

Returns:

  • (String, nil)

    the input path



27
28
29
# File 'lib/ruby_slm/states/task.rb', line 27

def input_path
  @input_path
end

#output_pathString? (readonly)

Returns the output path.

Returns:

  • (String, nil)

    the output path



29
30
31
# File 'lib/ruby_slm/states/task.rb', line 29

def output_path
  @output_path
end

#parametersHash (readonly)

Returns the parameters to pass to the resource.

Returns:

  • (Hash)

    the parameters to pass to the resource



21
22
23
# File 'lib/ruby_slm/states/task.rb', line 21

def parameters
  @parameters
end

#resourceString (readonly)

Returns the resource ARN or URI to invoke.

Returns:

  • (String)

    the resource ARN or URI to invoke



11
12
13
# File 'lib/ruby_slm/states/task.rb', line 11

def resource
  @resource
end

#result_pathString? (readonly)

Returns the result path.

Returns:

  • (String, nil)

    the result path



23
24
25
# File 'lib/ruby_slm/states/task.rb', line 23

def result_path
  @result_path
end

#result_selectorHash? (readonly)

Returns the result selector.

Returns:

  • (Hash, nil)

    the result selector



25
26
27
# File 'lib/ruby_slm/states/task.rb', line 25

def result_selector
  @result_selector
end

#retryArray<Hash> (readonly)

Returns the retry configuration.

Returns:

  • (Array<Hash>)

    the retry configuration



17
18
19
# File 'lib/ruby_slm/states/task.rb', line 17

def retry
  @retry
end

#timeout_secondsInteger? (readonly)

Returns the timeout in seconds for the task.

Returns:

  • (Integer, nil)

    the timeout in seconds for the task



13
14
15
# File 'lib/ruby_slm/states/task.rb', line 13

def timeout_seconds
  @timeout_seconds
end

Instance Method Details

#catch_policy_for(error) ⇒ CatchPolicy?

Check if this task has a catch handler for a specific error

Parameters:

  • error (Exception)

    the error to check

Returns:

  • (CatchPolicy, nil)

    the catch policy if applicable



102
103
104
105
106
# File 'lib/ruby_slm/states/task.rb', line 102

def catch_policy_for(error)
  @catch_objects.find do |catch_policy|
    catch_policy.matches?(error)
  end
end

#execute(execution, input) ⇒ Hash

Returns the output data from the state.

Parameters:

  • execution (Execution)

    the current execution

  • input (Hash)

    the input data for the state

Returns:

  • (Hash)

    the output data from the state



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ruby_slm/states/task.rb', line 72

def execute(execution, input)
  execution.logger&.info("Executing task state: #{@name}")
  execution.context[:current_state] = @name
  execution.context[:state_entered_time] = Time.now

  begin
    if @timeout_seconds || @heartbeat_seconds
      execute_with_timeout(execution, input)
    else
      execute_without_timeout(execution, input)
    end
  rescue => error
    handle_execution_error(execution, error, input)
  end
end

#retry_policy_for(error, attempt) ⇒ RetryPolicy?

Check if this task supports retry for a specific error

Parameters:

  • error (Exception)

    the error to check

  • attempt (Integer)

    the current attempt number

Returns:

  • (RetryPolicy, nil)

    the retry policy if applicable



92
93
94
95
96
# File 'lib/ruby_slm/states/task.rb', line 92

def retry_policy_for(error, attempt)
  @retry_objects.find do |retry_policy|
    retry_policy.matches?(error, attempt)
  end
end