Sha256: 2db6e77c76f8a4cf6a290bd7df09e8476d966dfdec8454d6a4f1c85e3977db67

Contents?: true

Size: 1.02 KB

Versions: 4

Compression:

Stored size: 1.02 KB

Contents

module Sidekiq
  module Hierarchy
    module RedisConnection
      # A translation class turning a Redis object into a ConnectionPool-alike
      class ConnectionProxy
        attr_reader :redis

        def initialize(redis_conn)
          raise 'connection must be an instance of Redis' unless redis_conn.is_a?(::Redis)
          @redis = redis_conn
        end

        def with(&blk)
          blk.call(redis)
        end
      end

      class << self
        attr_reader :redis

        # Set global redis
        def redis=(conn)
          @redis = if conn.nil?
                     nil
                   elsif conn.is_a?(::ConnectionPool)
                     conn
                   else
                     ConnectionProxy.new(conn)
                   end
        end
      end

      # Use global redis if set, with a fallback to Sidekiq's redis pool
      def redis(&blk)
        if RedisConnection.redis
          RedisConnection.redis.with(&blk)
        else
          Sidekiq.redis(&blk)
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sidekiq-hierarchy-2.1.0 lib/sidekiq/hierarchy/redis_connection.rb
sidekiq-hierarchy-2.0.1 lib/sidekiq/hierarchy/redis_connection.rb
sidekiq-hierarchy-2.0.0 lib/sidekiq/hierarchy/redis_connection.rb
sidekiq-hierarchy-1.1.0 lib/sidekiq/hierarchy/redis_connection.rb