Class: CodeBuildTail::Logs

Inherits:
Object
  • Object
show all
Defined in:
lib/code_build_tail/logs.rb

Instance Method Summary collapse

Constructor Details

#initialize(cloudwatch_client, poll_from_start, log_limit) ⇒ Logs

Returns a new instance of Logs.



4
5
6
7
8
9
# File 'lib/code_build_tail/logs.rb', line 4

def initialize(cloudwatch_client, poll_from_start, log_limit)
  @log_limit = log_limit
  @next_forward_token = nil
  @poll_from_start = poll_from_start
  @cloudwatch_client = cloudwatch_client
end

Instance Method Details

#get_latest_logs(group_name, stream_name) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/code_build_tail/logs.rb', line 11

def get_latest_logs(group_name, stream_name)
  # Sometimes when a CodeBuild job is first started it doesn't yet
  # have a CloudWatch group and/or stream name associated with it.
  # In this case, the keys for these values will be missing from
  # the returned hash so they show up in this function as nil. We
  # can simply return an empty list in this case because the app
  # logic will then update the state of the build which will cause
  # this function to work on subsquent attempts.
  return [] if group_name.nil? || stream_name.nil?

  params = {
    log_group_name: group_name,
    log_stream_name: stream_name,
    next_token: @next_forward_token,
    start_from_head: @poll_from_start,
    limit: @log_limit
  }
  resp = @cloudwatch_client.get_log_events(params)
  @next_forward_token = resp.next_forward_token
  resp.events
end

#poll_and_show_logs(group_name, stream_name) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/code_build_tail/logs.rb', line 33

def poll_and_show_logs(group_name, stream_name)
  log_events = get_latest_logs(group_name, stream_name)
  until log_events.empty?
    log_events.each do |event|
      print event.message
    end
    log_events = get_latest_logs(group_name, stream_name)
  end
end