2015-12-31 15:33:35 -08:00
|
|
|
# frozen_string_literal: true
|
2019-04-01 18:20:41 +02:00
|
|
|
|
|
|
|
require "connection_pool"
|
|
|
|
require "uri"
|
2022-08-25 10:15:11 -07:00
|
|
|
require "sidekiq/redis_client_adapter"
|
2012-02-08 16:43:57 -06:00
|
|
|
|
|
|
|
module Sidekiq
|
2022-05-10 21:25:04 +02:00
|
|
|
module RedisConnection
|
2013-04-17 10:07:46 -07:00
|
|
|
class << self
|
2019-04-01 18:20:41 +02:00
|
|
|
def create(options = {})
|
2020-05-23 00:38:52 +02:00
|
|
|
symbolized_options = options.transform_keys(&:to_sym)
|
2022-08-25 10:15:11 -07:00
|
|
|
symbolized_options[:url] ||= determine_redis_provider
|
2016-05-26 12:50:37 +07:00
|
|
|
|
2022-08-25 10:15:11 -07:00
|
|
|
if symbolized_options[:logger]
|
|
|
|
log_info(symbolized_options)
|
|
|
|
symbolized_options.delete(:logger)
|
2019-04-01 18:20:41 +02:00
|
|
|
end
|
2013-04-17 10:07:46 -07:00
|
|
|
|
2022-08-29 09:18:23 -07:00
|
|
|
size = symbolized_options.delete(:size) || 5
|
|
|
|
pool_timeout = symbolized_options.delete(:pool_timeout) || 1
|
|
|
|
|
2022-08-25 10:15:11 -07:00
|
|
|
redis_config = Sidekiq::RedisClientAdapter.new(symbolized_options)
|
2019-04-01 18:20:41 +02:00
|
|
|
ConnectionPool.new(timeout: pool_timeout, size: size) do
|
2022-05-10 21:25:04 +02:00
|
|
|
redis_config.new_client
|
2013-04-17 10:07:46 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2013-10-21 20:28:38 +02:00
|
|
|
def log_info(options)
|
2014-06-20 01:12:53 -07:00
|
|
|
redacted = "REDACTED"
|
2020-04-01 08:11:44 -07:00
|
|
|
|
2021-10-12 17:14:55 -07:00
|
|
|
# Deep clone so we can muck with these options all we want and exclude
|
|
|
|
# params from dump-and-load that may contain objects that Marshal is
|
|
|
|
# unable to safely dump.
|
|
|
|
keys = options.keys - [:logger, :ssl_params]
|
2020-04-28 23:40:02 -04:00
|
|
|
scrubbed_options = Marshal.load(Marshal.dump(options.slice(*keys)))
|
2013-11-17 10:20:28 -05:00
|
|
|
if scrubbed_options[:url] && (uri = URI.parse(scrubbed_options[:url])) && uri.password
|
2014-06-20 01:12:53 -07:00
|
|
|
uri.password = redacted
|
2013-11-17 10:20:28 -05:00
|
|
|
scrubbed_options[:url] = uri.to_s
|
|
|
|
end
|
2014-06-20 01:12:53 -07:00
|
|
|
if scrubbed_options[:password]
|
|
|
|
scrubbed_options[:password] = redacted
|
|
|
|
end
|
2019-11-06 16:49:34 +00:00
|
|
|
scrubbed_options[:sentinels]&.each do |sentinel|
|
|
|
|
sentinel[:password] = redacted if sentinel[:password]
|
2019-11-03 06:27:36 -08:00
|
|
|
end
|
2022-08-25 10:15:11 -07:00
|
|
|
options[:logger].info("Sidekiq #{Sidekiq::VERSION} connecting to Redis with options #{scrubbed_options}")
|
2013-04-17 09:53:01 -07:00
|
|
|
end
|
|
|
|
|
2013-04-17 10:07:46 -07:00
|
|
|
def determine_redis_provider
|
2017-06-19 07:42:00 -07:00
|
|
|
# If you have this in your environment:
|
|
|
|
# MY_REDIS_URL=redis://hostname.example.com:1238/4
|
|
|
|
# then set:
|
|
|
|
# REDIS_PROVIDER=MY_REDIS_URL
|
|
|
|
# and Sidekiq will find your custom URL variable with no custom
|
|
|
|
# initialization code at all.
|
2018-09-17 11:51:27 -07:00
|
|
|
#
|
2019-04-01 18:20:41 +02:00
|
|
|
p = ENV["REDIS_PROVIDER"]
|
2020-06-17 15:15:20 -07:00
|
|
|
if p && p =~ /:/
|
2019-04-01 18:20:41 +02:00
|
|
|
raise <<~EOM
|
|
|
|
REDIS_PROVIDER should be set to the name of the variable which contains the Redis URL, not a URL itself.
|
|
|
|
Platforms like Heroku will sell addons that publish a *_URL variable. You need to tell Sidekiq with REDIS_PROVIDER, e.g.:
|
2018-11-06 08:56:50 -08:00
|
|
|
|
2019-04-01 18:20:41 +02:00
|
|
|
REDISTOGO_URL=redis://somehost.example.com:6379/4
|
2019-08-28 09:59:28 -07:00
|
|
|
REDIS_PROVIDER=REDISTOGO_URL
|
2019-04-01 18:20:41 +02:00
|
|
|
EOM
|
2018-11-06 08:56:50 -08:00
|
|
|
end
|
2018-09-17 11:51:27 -07:00
|
|
|
|
2017-06-19 07:42:00 -07:00
|
|
|
ENV[
|
2019-04-01 18:20:41 +02:00
|
|
|
p || "REDIS_URL"
|
2017-06-19 07:42:00 -07:00
|
|
|
]
|
2012-03-13 21:19:46 -07:00
|
|
|
end
|
2012-10-14 14:58:20 -07:00
|
|
|
end
|
2012-02-08 16:43:57 -06:00
|
|
|
end
|
|
|
|
end
|