Sha256: 628ad18df9aeb7ea7f3895031c9eb5aeed376f64bbac0d1b7cf6be58ae3f4b67

Contents?: true

Size: 1.71 KB

Versions: 1

Compression:

Stored size: 1.71 KB

Contents

module Kontena::Cli::Helpers
  module LogHelper

    # @param [String] url
    # @param [Hash] query_params
    def stream_logs(url, query_params)
      last_seen = nil
      streamer = lambda do |chunk, remaining_bytes, total_bytes|
        log = buffered_log_json(chunk)
        if log
          yield log
          last_seen = log['id']
        end
      end

      begin
        query_params[:follow] = 1
        query_params[:from] = last_seen if last_seen
        result = client(token).get_stream(url, streamer, query_params)
      rescue => exc
        retry if exc.cause.is_a?(EOFError) # Excon wraps the EOFerror into SocketError
        raise
      end
    end

    # @param [String] chunk
    # @return [Hash,NilClass]
    def buffered_log_json(chunk)
      @buffer = '' if @buffer.nil?
      return if @buffer.empty? && chunk.strip.empty?
      begin
        orig_chunk = chunk
        unless @buffer.empty?
          chunk = @buffer + chunk
        end
        unless chunk.empty?
          log = JSON.parse(chunk)
        end
        @buffer = ''
        log
      rescue => exc
        @buffer << orig_chunk
        nil
      end
    end

    # @param [String] container_id
    # @return [Symbol]
    def color_for_container(container_id)
      color_maps[container_id] = colors.shift unless color_maps[container_id]
      color_maps[container_id].to_sym
    end

    # @return [Hash]
    def color_maps
      @color_maps ||= {}
    end

    # @return [Array<Symbol>]
    def colors
      if(@colors.nil? || @colors.size == 0)
        @colors = %i(
          red green yellow blue magenta cyan bright_red bright_green
          bright_yellow bright_blue bright_magenta bright_cyan
        )
      end
      @colors
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kontena-cli-0.16.0.pre6 lib/kontena/cli/helpers/log_helper.rb