2012-02-08 16:43:57 -06:00
|
|
|
require 'connection_pool'
|
2012-03-31 12:45:24 -07:00
|
|
|
require 'redis'
|
2012-02-08 16:43:57 -06:00
|
|
|
require 'redis/namespace'
|
|
|
|
|
|
|
|
module Sidekiq
|
|
|
|
class RedisConnection
|
2012-02-09 22:00:40 -08:00
|
|
|
def self.create(options={})
|
|
|
|
url = options[:url] || ENV['REDISTOGO_URL'] || 'redis://localhost:6379/0'
|
2012-07-19 18:04:15 +02:00
|
|
|
driver = options[:driver] || 'ruby'
|
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
|
2012-07-19 18:04:15 +02:00
|
|
|
build_client(url, options[:namespace], driver)
|
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
|
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-02-08 16:43:57 -06:00
|
|
|
end
|
|
|
|
end
|