lib/cfer/cfn/client.rb in cfer-0.4.2 vs lib/cfer/cfn/client.rb in cfer-0.5.0.pre.rc1

- old
+ new

@@ -155,10 +155,13 @@ # Yields to the given block for each CloudFormation event that qualifies, given the specified options. # @param options [Hash] The options hash # @option options [Fixnum] :number The maximum number of already-existing CloudFormation events to yield. # @option options [Boolean] :follow Set to true to wait until the stack enters a `COMPLETE` or `FAILED` state, yielding events as they occur. + # @option options [Boolean] :no_sleep Don't pause between polling. This is used for tests, and shouldn't be when polling the AWS API. + # @option options [Fixnum] :backoff The exponential backoff factor (default 1.5) + # @option options [Fixnum] :backoff_max_wait The maximum amount of time that exponential backoff will wait before polling agian (default 15s) def tail(options = {}) q = [] event_id_highwater = nil counter = 0 number = options[:number] || 0 @@ -171,33 +174,41 @@ event = q.shift yield event event_id_highwater = event.event_id end + sleep_time = 1 + running = true if options[:follow] while running - stack_status = describe_stacks(stack_name: name).stacks.first.stack_status - running = running && (/.+_(COMPLETE|FAILED)$/.match(stack_status) == nil) + sleep_time = [sleep_time * (options[:backoff] || 1), options[:backoff_max_wait] || 15].min + begin + stack_status = describe_stacks(stack_name: name).stacks.first.stack_status + running = running && (/.+_(COMPLETE|FAILED)$/.match(stack_status) == nil) - yielding = true - for_each_event name do |fetched_event| - if event_id_highwater == fetched_event.event_id - yielding = false - end + yielding = true + for_each_event name do |fetched_event| + if event_id_highwater == fetched_event.event_id + yielding = false + end - if yielding - q.unshift fetched_event + if yielding + q.unshift fetched_event + end end + rescue Aws::CloudFormation::Errors::ValidationError + running = false end while q.size > 0 event = q.shift yield event event_id_highwater = event.event_id + sleep_time = 1 end - sleep 1 if running unless options[:no_sleep] + sleep sleep_time if running unless options[:no_sleep] end end end def stack_cache(stack_name)