Class: Aws::Waiters::Waiter
- Inherits:
-
Object
- Object
- Aws::Waiters::Waiter
- Defined in:
- lib/aws-sdk-core/waiters/waiter.rb
Constant Summary collapse
- RAISE_HANDLER =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Seahorse::Client::Plugins::RaiseResponseErrors::Handler
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#before_attempt {|attempts| ... } ⇒ Object
Register a callback that is invoked before every polling attempt.
-
#before_wait {|attempts, response| ... } ⇒ Object
Register a callback that is invoked after an attempt but before sleeping.
-
#initialize(definition = {}) ⇒ Waiter
constructor
private
A new instance of Waiter.
- #wait(client, params) ⇒ Object
Constructor Details
#initialize(definition = {}) ⇒ Waiter
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.
Returns a new instance of Waiter.
9 10 11 12 13 14 15 |
# File 'lib/aws-sdk-core/waiters/waiter.rb', line 9 def initialize(definition = {}) @definition = definition @interval = definition['interval'] @max_attempts = definition['max_attempts'] @before_attempt = [] @before_wait = [] end |
Instance Attribute Details
#interval ⇒ Float
75 76 77 |
# File 'lib/aws-sdk-core/waiters/waiter.rb', line 75 def interval @interval end |
#max_attempts ⇒ Integer
78 79 80 |
# File 'lib/aws-sdk-core/waiters/waiter.rb', line 78 def max_attempts @max_attempts end |
Instance Method Details
#before_attempt {|attempts| ... } ⇒ Object
Register a callback that is invoked before every polling attempt. Yields the number of attempts made so far.
waiter.before_attempt do |attempts|
puts "#{attempts} made, about to make attempt #{attempts + 1}"
end
Throwing ‘:success` or `:failure` from the given block will stop the waiter and return or raise. You can pass a custom message to the throw:
# raises Aws::Waiters::Errors::WaiterFailed
waiter.before_attempt do |attempts|
throw :failure, 'custom-error-message'
end
# cause the waiter to stop polling and return
waiter.before_attempt do |attempts|
throw :success
end
39 40 41 |
# File 'lib/aws-sdk-core/waiters/waiter.rb', line 39 def before_attempt(&block) @before_attempt << Proc.new end |
#before_wait {|attempts, response| ... } ⇒ Object
Register a callback that is invoked after an attempt but before sleeping. Yields the number of attempts made and the previous response.
waiter.before_wait do |attempts, response|
puts "#{attempts} made"
puts response.error.inspect
puts response.data.inspect
end
Throwing ‘:success` or `:failure` from the given block will stop the waiter and return or raise. You can pass a custom message to the throw:
# raises Aws::Waiters::Errors::WaiterFailed
waiter.before_attempt do |attempts|
throw :failure, 'custom-error-message'
end
# cause the waiter to stop polling and return
waiter.before_attempt do |attempts|
throw :success
end
70 71 72 |
# File 'lib/aws-sdk-core/waiters/waiter.rb', line 70 def before_wait(&block) @before_wait << Proc.new end |
#wait(client, params) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/aws-sdk-core/waiters/waiter.rb', line 82 def wait(client, params) attempts = 0 catch(:success) do failure = catch(:failure) do loop do trigger_callbacks(@before_attempt, attempts) attempts += 1 resp = send_request(client, params) throw :success, resp if successful?(resp) throw :failure if failure?(resp) throw :failure, resp.error unless error_ignored?(resp) throw :failure, too_many(attempts) if attempts == max_attempts trigger_callbacks(@before_wait, attempts, resp) sleep(interval) end end failure = 'waiter failed' if failure.nil? raise String === failure ? Errors::WaiterFailed.new(failure) : failure end end |