module SidekiqNamespaced

Constants

VERSION

Public Class Methods

decorate(base) click to toggle source
# File lib/sidekiq_namespaced.rb, line 26
def self.decorate(base)
  base.send(:extend, self)
  singleton_class = class << base
                      self
                    end
  singleton_class.send(:alias_method, :redis_without_namespace, :redis)
  singleton_class.send(:alias_method, :redis, :redis_with_namespace)
end

Public Instance Methods

redis_with_namespace(&block) click to toggle source
# File lib/sidekiq_namespaced.rb, line 14
def redis_with_namespace &block
  redis_without_namespace do |conn|
    raw_client = conn.is_a?(Redis::Namespace) ? conn.redis : conn
    if Thread.current[:sidekiq_namespace_override]
      conn_with_namespace = Redis::Namespace.new(Thread.current[:sidekiq_namespace_override], redis: raw_client)
      block.call(conn_with_namespace)
    else
      block.call(conn)
    end
  end
end
with_namespace(namespace, &block) click to toggle source
# File lib/sidekiq_namespaced.rb, line 5
def with_namespace namespace, &block
  begin
    Thread.current[:sidekiq_namespace_override] = namespace
    block.call
  ensure
    Thread.current[:sidekiq_namespace_override] = nil
  end
end