2020-10-05 12:01:10 +00:00
|
|
|
module Redis::Config
|
2021-06-18 14:38:58 +00:00
|
|
|
DEFAULT_SENTINEL_PORT ||= '26379'.freeze
|
2020-10-05 12:01:10 +00:00
|
|
|
class << self
|
|
|
|
def app
|
2020-10-09 07:07:42 +00:00
|
|
|
config
|
2020-10-05 12:01:10 +00:00
|
|
|
end
|
|
|
|
|
2020-10-09 07:07:42 +00:00
|
|
|
def config
|
|
|
|
@config ||= sentinel? ? sentinel_config : base_config
|
|
|
|
end
|
|
|
|
|
|
|
|
def base_config
|
|
|
|
{
|
2020-10-05 12:01:10 +00:00
|
|
|
url: ENV.fetch('REDIS_URL', 'redis://127.0.0.1:6379'),
|
2021-03-30 12:46:17 +00:00
|
|
|
password: ENV.fetch('REDIS_PASSWORD', nil).presence,
|
2022-03-24 13:55:07 +00:00
|
|
|
ssl_params: { verify_mode: Chatwoot.redis_ssl_verify_mode },
|
2021-03-30 12:46:17 +00:00
|
|
|
reconnect_attempts: 2,
|
|
|
|
network_timeout: 5
|
2020-10-05 12:01:10 +00:00
|
|
|
}
|
2020-10-09 07:07:42 +00:00
|
|
|
end
|
2020-10-05 12:01:10 +00:00
|
|
|
|
2020-10-09 07:07:42 +00:00
|
|
|
def sentinel?
|
|
|
|
ENV.fetch('REDIS_SENTINELS', nil).presence
|
|
|
|
end
|
|
|
|
|
2022-10-20 00:25:16 +00:00
|
|
|
def sentinel_url_config(sentinel_url)
|
|
|
|
host, port = sentinel_url.split(':').map(&:strip)
|
|
|
|
sentinel_url_config = { host: host, port: port || DEFAULT_SENTINEL_PORT }
|
|
|
|
password = ENV.fetch('REDIS_SENTINEL_PASSWORD', base_config[:password])
|
|
|
|
sentinel_url_config[:password] = password if password.present?
|
|
|
|
sentinel_url_config
|
|
|
|
end
|
|
|
|
|
2020-10-09 07:07:42 +00:00
|
|
|
def sentinel_config
|
|
|
|
redis_sentinels = ENV.fetch('REDIS_SENTINELS', nil)
|
|
|
|
|
|
|
|
# expected format for REDIS_SENTINELS url string is host1:port1, host2:port2
|
|
|
|
sentinels = redis_sentinels.split(',').map do |sentinel_url|
|
2022-10-20 00:25:16 +00:00
|
|
|
sentinel_url_config(sentinel_url)
|
2020-10-05 12:01:10 +00:00
|
|
|
end
|
2020-10-09 07:07:42 +00:00
|
|
|
|
|
|
|
# over-write redis url as redis://:<your-redis-password>@<master-name>/ when using sentinel
|
|
|
|
# more at https://github.com/redis/redis-rb/issues/531#issuecomment-263501322
|
|
|
|
master = "redis://#{ENV.fetch('REDIS_SENTINEL_MASTER_NAME', 'mymaster')}"
|
|
|
|
|
|
|
|
base_config.merge({ url: master, sentinels: sentinels })
|
2020-10-05 12:01:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|