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
|
2012-02-09 22:00:40 -08:00
|
|
|
def self.create(options={})
|
2012-10-14 14:58:20 -07:00
|
|
|
url = options[:url] || determine_redis_provider || 'redis://localhost:6379/0'
|
2012-04-06 09:43:02 -07:00
|
|
|
# need a connection for Fetcher and Retry
|
2012-04-09 08:57:47 -07:00
|
|
|
size = options[:size] || (Sidekiq.server? ? (Sidekiq.options[:concurrency] + 2) : 5)
|
2012-04-06 09:43:02 -07:00
|
|
|
|
2012-04-03 20:00:20 -07:00
|
|
|
ConnectionPool.new(:timeout => 1, :size => size) do
|
2013-03-08 21:31:57 -08:00
|
|
|
build_client(url, options[:namespace], options[:driver] || 'ruby')
|
2012-03-13 21:19:46 -07:00
|
|
|
end
|
2012-02-08 16:43:57 -06:00
|
|
|
end
|
|
|
|
|
2012-07-19 18:04:15 +02:00
|
|
|
def self.build_client(url, namespace, driver)
|
|
|
|
client = Redis.connect(:url => url, :driver => driver)
|
2012-02-08 16:43:57 -06:00
|
|
|
if namespace
|
2013-03-08 21:31:57 -08:00
|
|
|
require 'redis/namespace'
|
2012-02-09 22:00:40 -08:00
|
|
|
Redis::Namespace.new(namespace, :redis => client)
|
2012-02-08 16:43:57 -06:00
|
|
|
else
|
2012-02-09 22:00:40 -08:00
|
|
|
client
|
2012-02-08 16:43:57 -06:00
|
|
|
end
|
|
|
|
end
|
2012-02-09 22:00:40 -08:00
|
|
|
private_class_method :build_client
|
2012-10-14 14:58:20 -07:00
|
|
|
|
2012-10-14 16:07:37 -07:00
|
|
|
# Not public
|
2012-10-14 14:58:20 -07:00
|
|
|
def self.determine_redis_provider
|
2013-03-14 08:04:49 +11:00
|
|
|
# REDISTOGO_URL is only support for legacy reasons
|
2012-10-14 16:07:37 -07:00
|
|
|
return ENV['REDISTOGO_URL'] if ENV['REDISTOGO_URL']
|
|
|
|
provider = ENV['REDIS_PROVIDER'] || 'REDIS_URL'
|
2012-10-14 14:58:20 -07:00
|
|
|
ENV[provider]
|
|
|
|
end
|
2012-02-08 16:43:57 -06:00
|
|
|
end
|
|
|
|
end
|