class CodeBuildTail::Logs

Public Class Methods

new(cloudwatch_client, poll_from_start, log_limit) click to toggle source
# 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

Public Instance Methods

get_latest_logs(group_name, stream_name) click to toggle source
# 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) click to toggle source
# 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