Class: Moonshot::StackEventsPoller

Inherits:
Object
  • Object
show all
Defined in:
lib/moonshot/stack_events_poller.rb

Overview

The StackEventsPoller queries DescribeStackEvents every time #latest_events is invoked, filtering out events that have already been returned. It can also, optionally, filter all non-error events (@see #show_errors_only).

Instance Method Summary collapse

Constructor Details

#initialize(cf_client, stack_name) ⇒ StackEventsPoller

Returns a new instance of StackEventsPoller.



8
9
10
11
12
13
14
# File 'lib/moonshot/stack_events_poller.rb', line 8

def initialize(cf_client, stack_name)
  @cf_client = cf_client
  @stack_name = stack_name

  # Start showing events from now.
  @last_time = Time.now
end

Instance Method Details

#filter_events(events) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/moonshot/stack_events_poller.rb', line 34

def filter_events(events)
  if @errors_only
    events.select do |event|
      %w[CREATE_FAILED UPDATE_FAILED DELETE_FAILED].include?(event.resource_status)
    end
  else
    events
  end
end

#get_stack_events(token = nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/moonshot/stack_events_poller.rb', line 44

def get_stack_events(token = nil)
  opts = {
    stack_name: @stack_name
  }

  opts[:next_token] = token if token

  result = @cf_client.describe_stack_events(**opts)
  events = result.stack_events
  events += get_stack_events(result.next_token) if result.next_token

  events
end

#latest_eventsArray<Aws::CloudFormation::Event>

Build a list of events that have occurred since the last call to this method.

Returns:

  • (Array<Aws::CloudFormation::Event>)


24
25
26
27
28
29
30
31
32
# File 'lib/moonshot/stack_events_poller.rb', line 24

def latest_events
  events = get_stack_events.select do |event|
    event.timestamp > @last_time
  end

  @last_time = Time.now

  filter_events(events.sort_by(&:timestamp))
end

#show_only_errorsObject



16
17
18
# File 'lib/moonshot/stack_events_poller.rb', line 16

def show_only_errors
  @errors_only = true
end