Method: Cfer::Cfn::Client#tail

Defined in:
lib/cfer/cfn/client.rb

#tail(options = {}) ⇒ Object

Yields to the given block for each CloudFormation event that qualifies, given the specified options.

Parameters:

  • options (Hash) (defaults to: {})

    The options hash

Options Hash (options):

  • :number (Fixnum)

    The maximum number of already-existing CloudFormation events to yield.

  • :follow (Boolean)

    Set to true to wait until the stack enters a COMPLETE or FAILED state, yielding events as they occur.

  • :no_sleep (Boolean)

    Don't pause between polling. This is used for tests, and shouldn't be when polling the AWS API.

  • :backoff (Fixnum)

    The exponential backoff factor (default 1.5)

  • :backoff_max_wait (Fixnum)

    The maximum amount of time that exponential backoff will wait before polling agian (default 15s)



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/cfer/cfn/client.rb', line 154

def tail(options = {})
  q = []
  event_id_highwater = nil
  counter = 0
  number = options[:number] || 0
  for_each_event name do |fetched_event|
    q.unshift fetched_event if counter < number
    counter = counter + 1
  end

  while q.size > 0
    event = q.shift
    yield event
    event_id_highwater = event.event_id
  end

  sleep_time = 1

  running = true
  if options[:follow]
    while running
      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

          if yielding
            q.unshift fetched_event
          end
        end
      rescue Aws::CloudFormation::Errors::Throttling
        Cfer::LOGGER.debug "AWS SDK is being throttled..."
        # Keep going though.
      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 sleep_time if running unless options[:no_sleep]
    end
  end
end