Sha256: 62e375a41c559f2008deb192a572e5d61430d95682abaef9a11f6b5e628fa1d7

Contents?: true

Size: 1.24 KB

Versions: 20

Compression:

Stored size: 1.24 KB

Contents

require 'redis'

module LogStashLogger
  module Device
    class Redis < Connectable
      DEFAULT_LIST = 'logstash'

      attr_accessor :list

      def initialize(opts)
        super
        @list = opts.delete(:list) || DEFAULT_LIST
        @buffer_group = @list

        normalize_path(opts)

        @redis_options = opts
      end

      def connect
        @io = ::Redis.new(@redis_options)
      end

      def reconnect
        @io.client.reconnect
      rescue => e
        log_error(e)
      end

      def with_connection
        connect unless connected?
        yield
      rescue ::Redis::InheritedError
        reconnect
        retry
      rescue => e
        log_error(e)
        close(flush: false)
        raise
      end

      def write_batch(messages, list = nil)
        list ||= @list
        with_connection do
          @io.rpush(list, messages)
        end
      end

      def write_one(message, list = nil)
        write_batch(message, list)
      end

      def close!
        @io && @io.quit
      end

      private

      def normalize_path(opts)
        path = opts.fetch(:path, nil)
        if path
          opts[:db] = path.gsub("/", "").to_i unless path.empty?
          opts.delete(:path)
        end
      end

    end
  end
end

Version data entries

20 entries across 20 versions & 2 rubygems

Version Path
logstash-logger-yajl-0.27.0 lib/logstash-logger/device/redis.rb
logstash-logger-0.26.1 lib/logstash-logger/device/redis.rb
logstash-logger-0.26.0 lib/logstash-logger/device/redis.rb
logstash-logger-0.25.1 lib/logstash-logger/device/redis.rb
logstash-logger-0.25.0 lib/logstash-logger/device/redis.rb
logstash-logger-0.24.1 lib/logstash-logger/device/redis.rb
logstash-logger-0.24.0 lib/logstash-logger/device/redis.rb
logstash-logger-0.23.0 lib/logstash-logger/device/redis.rb
logstash-logger-0.22.1 lib/logstash-logger/device/redis.rb
logstash-logger-0.22.0 lib/logstash-logger/device/redis.rb
logstash-logger-0.21.0 lib/logstash-logger/device/redis.rb
logstash-logger-0.20.1 lib/logstash-logger/device/redis.rb
logstash-logger-0.20.0 lib/logstash-logger/device/redis.rb
logstash-logger-0.19.2 lib/logstash-logger/device/redis.rb
logstash-logger-0.19.1 lib/logstash-logger/device/redis.rb
logstash-logger-0.19.0 lib/logstash-logger/device/redis.rb
logstash-logger-0.18.1 lib/logstash-logger/device/redis.rb
logstash-logger-0.18.0 lib/logstash-logger/device/redis.rb
logstash-logger-0.17.0 lib/logstash-logger/device/redis.rb
logstash-logger-0.16.0 lib/logstash-logger/device/redis.rb