2012-10-14 16:54:34 -07:00
|
|
|
require 'connection_pool'
|
2012-03-31 12:45:24 -07:00
|
|
|
require 'redis'
|
2012-02-08 16:43:57 -06:00
|
|
|
|
|
|
|
module Sidekiq
|
|
|
|
class RedisConnection
|
2013-04-17 10:07:46 -07:00
|
|
|
class << self
|
2012-04-06 09:43:02 -07:00
|
|
|
|
2013-04-17 10:07:46 -07: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 09:02:51 -06:00
|
|
|
pool_timeout = options[:pool_timeout] || 1
|
2013-04-17 10:07:46 -07:00
|
|
|
|
|
|
|
log_info(url, options)
|
|
|
|
|
2013-07-17 09:02:51 -06:00
|
|
|
ConnectionPool.new(:timeout => pool_timeout, :size => size) do
|
|
|
|
build_client(url, options[:namespace], options[:driver] || 'ruby', options[:network_timeout])
|
2013-04-17 10:07:46 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2013-07-17 09:02:51 -06:00
|
|
|
def build_client(url, namespace, driver, network_timeout)
|
2013-08-09 17:20:27 -03:00
|
|
|
client = Redis.new client_opts(url, driver, network_timeout)
|
2013-04-17 10:07:46 -07:00
|
|
|
if namespace
|
|
|
|
require 'redis/namespace'
|
|
|
|
Redis::Namespace.new(namespace, :redis => client)
|
|
|
|
else
|
|
|
|
client
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-17 09:02:51 -06: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 10:07:46 -07:00
|
|
|
def log_info(url, options)
|
2013-04-17 09:53:01 -07:00
|
|
|
opts = options.dup
|
|
|
|
opts.delete(:url)
|
2013-04-17 10:07:46 -07:00
|
|
|
if Sidekiq.server?
|
|
|
|
Sidekiq.logger.info("Booting #{Sidekiq::NAME} #{Sidekiq::VERSION} using #{url} with options #{opts}")
|
|
|
|
else
|
|
|
|
Sidekiq.logger.info("#{Sidekiq::NAME} client using #{url} with options #{opts}")
|
|
|
|
end
|
2013-04-17 09:53:01 -07:00
|
|
|
end
|
|
|
|
|
2013-04-17 10:07:46 -07: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-13 21:19:46 -07:00
|
|
|
end
|
2012-02-08 16:43:57 -06:00
|
|
|
|
2012-10-14 14:58:20 -07:00
|
|
|
end
|
2012-02-08 16:43:57 -06:00
|
|
|
end
|
|
|
|
end
|