1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00
mperham--sidekiq/lib/sidekiq/redis_connection.rb
jc00ke 36bb71883a Allow generic Redis providers via env var
Instead of hardcoding every provider's environment variable, Sidekiq now
supports both REDIS_PROVIDER and REDIS_URL env vars.

Let's say you run on Heroku and you use openredis. You can now set
REDIS_PROVIDER=OPENREDIS_URL and Sidekiq will use the openredis uri.

If you switch to another provider, all you need to do is reset
REDIS_PROVIDER. An example of switching to RedisGreen:
REDIS_PROVIDER=REDISGREEN_URL. Easy peasy!

Sidekiq also supports using your own url with REDIS_URL. While you can
still pass in the URL to Sidekiq::RedisConnection.create, it's also now
possible to set it with just an env var.

Closes #443
2012-10-14 14:58:20 -07:00

38 lines
1.1 KiB
Ruby

require 'connection_pool'
require 'redis'
require 'redis/namespace'
module Sidekiq
class RedisConnection
def self.create(options={})
url = options[:url] || determine_redis_provider || 'redis://localhost:6379/0'
driver = options[:driver] || 'ruby'
# need a connection for Fetcher and Retry
size = options[:size] || (Sidekiq.server? ? (Sidekiq.options[:concurrency] + 2) : 5)
ConnectionPool.new(:timeout => 1, :size => size) do
build_client(url, options[:namespace], driver)
end
end
def self.build_client(url, namespace, driver)
client = Redis.connect(:url => url, :driver => driver)
if namespace
Redis::Namespace.new(namespace, :redis => client)
else
client
end
end
private_class_method :build_client
def self.determine_redis_provider
provider = if ENV.has_key? 'REDISTOGO_URL'
'REDISTOGO_URL'
else
ENV['REDIS_PROVIDER'] || 'REDIS_URL'
end
ENV[provider]
end
private_class_method :determine_redis_provider
end
end