Class: AWS::Flow::GenericClient

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

Overview

A generic activity client. This class is not usually used directly; it serves as the base class for both activity and workflow client classes.

Direct Known Subclasses

GenericActivityClient, WorkflowClient

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ GenericClient

Creates a new generic client.



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

def initialize(*args)
  @option_map = {}
end

Instance Attribute Details

#option_mapObject

The option map for the client.



24
25
26
# File 'lib/aws/decider/generic_client.rb', line 24

def option_map
  @option_map
end

Instance Method Details

#_retry(method_name, retry_function, block, args = NoInput.new) ⇒ 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.

Retries the given method using an optional retry function and block of RetryOptions.

Parameters:

  • method_name

    The method to retry.

  • retry_function

    The function to use in order to determine if a retry should be attempted.

  • args (defaults to: NoInput.new)

    Arguments to send to the method provided in the method_name parameter.

  • block

    A block of RetryOptions used to specify retry behavior.



170
171
172
173
174
# File 'lib/aws/decider/generic_client.rb', line 170

def _retry(method_name, retry_function, block, args = NoInput.new)
  bail_if_external
  retry_options = Utilities::interpret_block_for_options(ExponentialRetryOptions, block)
  _retry_with_options(lambda { self.send(method_name, *args) }, retry_function, retry_options)
end

#_retry_with_options(lambda_to_execute, retry_function, retry_options, args = NoInput.new) ⇒ 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.

Used by #retry



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/aws/decider/generic_client.rb', line 138

def _retry_with_options(lambda_to_execute, retry_function, retry_options, args = NoInput.new)
  retry_policy = RetryPolicy.new(retry_function, retry_options)
  output = Utilities::AddressableFuture.new
  result = nil
  failure = nil
  error_handler do |t|
    t.begin do
      async_retrying_executor = AsyncRetryingExecutor.new(retry_policy, self.decision_context.workflow_clock, self.decision_context.workflow_context.decision_task.workflow_execution.run_id, retry_options.return_on_start)
      result = async_retrying_executor.execute(lambda_to_execute)
    end
    t.rescue(Exception) do |error|
      failure = error
    end
    t.ensure do
      if failure.nil?
        output.set(result)
      else
        output.set(nil)
      end
    end
  end
  return output if retry_options.return_on_start
  output.get
  raise failure unless failure.nil?
  return output.get
end

#bail_if_externalObject

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.



64
65
66
# File 'lib/aws/decider/generic_client.rb', line 64

def bail_if_external
  raise "You cannot use this function outside of a workflow definition" if Utilities::is_external
end

#decision_contextObject

Returns the decision context for this client.

Returns:

  • The decision context.



206
207
208
# File 'lib/aws/decider/generic_client.rb', line 206

def decision_context
  FlowFiber.current[:decision_context]
end

#exponential_retry(method_name, *args, &block) ⇒ Object

Retries the given method using an exponential fallback function.

Parameters:

  • method_name

    The method to retry.

  • args

    Arguments (parameters) that are passed to the method specified in method_name.

  • block

    A block of RetryOptions used to specify retry behavior.



130
131
132
133
# File 'lib/aws/decider/generic_client.rb', line 130

def exponential_retry(method_name, *args, &block)
  future = self._retry(method_name, FlowConstants.exponential_retry_function, block, args)
  Utilities::drill_on_future(future)
end

#reconfigure(*method_names, &block) ⇒ Object

Note:

This functionality is limited to activity clients only.

Reconfigures an activity client with a block of passed-in options.

Parameters:

  • method_names

    The activity methods to modify with the options passed in.

  • block

    A block of ActivityOptions to use to reconfigure the client.



58
59
60
61
# File 'lib/aws/decider/generic_client.rb', line 58

def reconfigure(*method_names, &block)
  options = Utilities::interpret_block_for_options(self.class.default_option_class, block)
  method_names.each { |method_name| @option_map[method_name.to_sym] = options }
end

#retry(method_name, retry_function, *args, &block) ⇒ Object

Retries the given method using an optional retry function and block of RetryOptions.

Parameters:

  • method_name

    The method to retry.

  • retry_function

    The function to use in order to determine if a retry should be attempted.

  • args

    Arguments to send to the method provided in the method_name parameter.

  • block

    A block of RetryOptions used to specify retry behavior.



193
194
195
196
197
198
199
200
# File 'lib/aws/decider/generic_client.rb', line 193

def retry(method_name, retry_function, *args, &block)
  if retry_function.is_a? Fixnum
    retry_time = retry_function
    retry_function = lambda {|first_attempt, time_of_failure, attempt| retry_time}
  end
  future = self._retry(method_name, retry_function, block, args)
  Utilities::drill_on_future(future)
end

#send_async(task, *args, &block) ⇒ Object

Note:

Trying to use #send_async outside of a workflow will fail with an exception.

Starts an asynchronous execution of a task. This method returns immediately; it does not wait for the task to complete.

Examples:

The following two calls are equivalent.

foo.send_async :bar # plus args and block if appropriate

task do
  foo.send :bar # plus args and block if appropriate
end

Parameters:

  • task

    The task to execute.

  • args

    A number of arguments used to start the task execution.

  • block

    A code block with additional options to start the task execution.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/aws/decider/generic_client.rb', line 88

def send_async(task, *args, &block)
  bail_if_external
  # If there is no block, just make a block for immediate return.
  if block.nil?
    modified_options = Proc.new{ {:return_on_start => true } }
    # If there is a block, and it doesn't take any arguments, it will
    # evaluate to a hash. Add an option to the hash.
  elsif block.arity == 0
    modified_options = Proc.new do
      result = block.call
      # We need to copy the hash to make sure that we don't mutate it
      result = result.dup
      result[:return_on_start] = true
      result
    end
    # Otherwise, it will expect an options object passed in, and will do
    # things on that object. So make our new Proc do that, and add an
    # option.
  else modified_options = Proc.new do |x|
      result = block.call(x)
      # Same as the above dup, we'll copy to avoid any possible mutation
      # of inputted objects
      result = result.dup
      result.return_on_start = true
      result
    end
  end
  self.send(task, *args, &modified_options)
end

#with_opts(opts = {}) ⇒ GenericClient

Creates a new client that is a copy of this client instance, modified by a hash of passed-in options.

Parameters:

  • opts (defaults to: {})

    The options to set on the new client. Any options that are not set here are copied from the original client.

Returns:



40
41
42
43
44
45
46
# File 'lib/aws/decider/generic_client.rb', line 40

def with_opts(opts = {})
  modified_instance = self.dup
  opts.each_pair do |key, value|
    modified_instance.options.send("#{key}=", value) if modified_instance.options.methods.map(&:to_sym).include? "#{key}=".to_sym
  end
  modified_instance
end