Sha256: ead9f4ef3fba9ad988d7e46b2093645ffe7b173386ef82701a063f5925d169d7

Contents?: true

Size: 1.53 KB

Versions: 5

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

require "redis"

supported_version = "4"

if Gem::Version.new(Redis::VERSION) < Gem::Version.new(supported_version)
  raise "redis #{Redis::VERSION} is not supported. Please upgrade to Redis #{supported_version}."
end

require_relative "../redis/patches/client"
require_relative "redis/connector"

module RailsFailover
  class Redis
    PRIMARY = :primary
    REPLICA = :replica

    def self.logger=(logger)
      @logger = logger
    end

    def self.logger
      if @logger
        @logger
      elsif defined?(::Rails)
        ::Rails.logger
      end
    end

    def self.verify_primary_frequency_seconds=(seconds)
      @verify_primary_frequency_seconds = seconds
    end

    def self.verify_primary_frequency_seconds
      @verify_primary_frequency_seconds || 5
    end

    def self.on_failover(&block)
      @on_failover_callback = block
    end

    def self.on_failover_callback!(key)
      @on_failover_callback&.call(key)
    rescue => e
      logger.warn(
        "RailsFailover::Redis.on_failover failed: #{e.class} #{e.message}\n#{e.backtrace.join("\n")}",
      )
    end

    def self.on_fallback(&block)
      @on_fallback_callback = block
    end

    def self.on_fallback_callback!(key)
      @on_fallback_callback&.call(key)
    rescue => e
      logger.warn(
        "RailsFailover::Redis.on_fallback failed: #{e.class} #{e.message}\n#{e.backtrace.join("\n")}",
      )
    end

    # For testing
    def self.clear_callbacks
      @on_fallback_callback = nil
      @on_failover_callback = nil
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rails_failover-2.1.1 lib/rails_failover/redis.rb
rails_failover-2.1.0 lib/rails_failover/redis.rb
rails_failover-2.0.1 lib/rails_failover/redis.rb
rails_failover-2.0.0 lib/rails_failover/redis.rb
rails_failover-1.0.0 lib/rails_failover/redis.rb