Skip to content

Instantly share code, notes, and snippets.

@djpate
Created December 6, 2015 00:21
Show Gist options
  • Save djpate/fadc84fb59316c7d6ed6 to your computer and use it in GitHub Desktop.
Save djpate/fadc84fb59316c7d6ed6 to your computer and use it in GitHub Desktop.
Sharded Sidekiq Webui
REDIS_A = ConnectionPool.new { Redis.new(:db => 1) }
REDIS_B = ConnectionPool.new { Redis.new(:db => 2) }
class ShardedWeb < Sidekiq::Web
before do
desired_shard = params[:shard] || request.cookies['sidekiq_shard']
if desired_shard
Thread.current[:sidekiq_via_pool] = Object.const_get(desired_shard)
#this prevents us from having to override all links in the web ERBs
response.set_cookie 'sidekiq_shard', desired_shard
end
end
after do
Thread.current[:sidekiq_via_pool] = nil
end
end
module Sidekiq
def self.redis_pool
Thread.current[:sidekiq_via_pool] ? Thread.current[:sidekiq_via_pool] : pool_no_shard
end
def self.pool_no_shard
@redis ||= Sidekiq::RedisConnection.create
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment