2012-10-14 19:54:34 -04:00
|
|
|
require 'connection_pool'
|
2012-03-31 15:45:24 -04:00
|
|
|
require 'redis'
|
2012-02-08 17:43:57 -05:00
|
|
|
|
|
|
|
module Sidekiq
|
|
|
|
class RedisConnection
|
2013-04-17 13:07:46 -04:00
|
|
|
class << self
|
2012-04-06 12:43:02 -04:00
|
|
|
|
2013-04-17 13:07:46 -04:00
|
|
|
def create(options={})
|
|
|
|
url = options[:url] || determine_redis_provider || 'redis://localhost:6379/0'
|
|
|
|
# need a connection for Fetcher and Retry
|
|
|
|
size = options[:size] || (Sidekiq.server? ? (Sidekiq.options[:concurrency] + 2) : 5)
|
2013-07-17 11:02:51 -04:00
|
|
|
pool_timeout = options[:pool_timeout] || 1
|
2013-04-17 13:07:46 -04:00
|
|
|
|
|
|
|
log_info(url, options)
|
|
|
|
|
2013-07-17 11:02:51 -04:00
|
|
|
ConnectionPool.new(:timeout => pool_timeout, :size => size) do
|
|
|
|
build_client(url, options[:namespace], options[:driver] || 'ruby', options[:network_timeout])
|
2013-04-17 13:07:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2013-07-17 11:02:51 -04:00
|
|
|
def build_client(url, namespace, driver, network_timeout)
|
2013-08-09 16:20:27 -04:00
|
|
|
client = Redis.new client_opts(url, driver, network_timeout)
|
2013-04-17 13:07:46 -04:00
|
|
|
if namespace
|
|
|
|
require 'redis/namespace'
|
|
|
|
Redis::Namespace.new(namespace, :redis => client)
|
|
|
|
else
|
|
|
|
client
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-17 11:02:51 -04:00
|
|
|
def client_opts(url, driver, timeout)
|
|
|
|
if timeout
|
|
|
|
{ :url => url, :driver => driver, :timeout => timeout }
|
|
|
|
else
|
|
|
|
{ :url => url, :driver => driver }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-04-17 13:07:46 -04:00
|
|
|
def log_info(url, options)
|
2013-04-17 12:53:01 -04:00
|
|
|
opts = options.dup
|
|
|
|
opts.delete(:url)
|
2013-04-17 13:07:46 -04:00
|
|
|
if Sidekiq.server?
|
2013-08-10 15:37:17 -04:00
|
|
|
Sidekiq.logger.info("Booting Sidekiq #{Sidekiq::VERSION} using #{url} with options #{opts}")
|
2013-04-17 13:07:46 -04:00
|
|
|
else
|
|
|
|
Sidekiq.logger.info("#{Sidekiq::NAME} client using #{url} with options #{opts}")
|
|
|
|
end
|
2013-04-17 12:53:01 -04:00
|
|
|
end
|
|
|
|
|
2013-04-17 13:07:46 -04:00
|
|
|
def determine_redis_provider
|
|
|
|
# REDISTOGO_URL is only support for legacy reasons
|
|
|
|
return ENV['REDISTOGO_URL'] if ENV['REDISTOGO_URL']
|
|
|
|
provider = ENV['REDIS_PROVIDER'] || 'REDIS_URL'
|
|
|
|
ENV[provider]
|
2012-03-14 00:19:46 -04:00
|
|
|
end
|
2012-02-08 17:43:57 -05:00
|
|
|
|
2012-10-14 17:58:20 -04:00
|
|
|
end
|
2012-02-08 17:43:57 -05:00
|
|
|
end
|
|
|
|
end
|