Skip to content

Instantly share code, notes, and snippets.

@exocode
Last active August 16, 2022 23:01
Show Gist options
  • Save exocode/9f16e531210a5fe52c2f3a44e9363524 to your computer and use it in GitHub Desktop.
Save exocode/9f16e531210a5fe52c2f3a44e9363524 to your computer and use it in GitHub Desktop.
Bitnami Redis sentinel Sidekiq Rails with
# /config/initializers/sidekiq.rb
# REDIS_URL ONLY in development environment needed
# REDIS_SENTINEL must be provided (eg: redis-headless.redis.svc.cluster.local if using Kubernetes)
# REDIS_PASSWORD must be provided, otherwise: edit this example
# in order to retrieve the Redis IP addresses.
SENTINELS = Resolv.getaddresses(ENV['REDIS_SENTINEL']).map do |address|
# 'address' will get resolved into IP addresses
{ host: address, port: 26_379, password: ENV['REDIS_PASSWORD'] } # reading from
end if Rails.env.production?
# SERVER CONFIGURATION
Sidekiq.configure_server do |config|
if Rails.env.development?
# for development simple and single Redis instance (without password) configured
config.redis = { url: ENV['REDIS_URL'], id: "Sidekiq-server-PID-#{::Process.pid}" }
elsif Rails.env.production?
config.redis = {
url: 'redis://mymaster', # when using sentinels the name of the master can be found in the Redis logs (regularly it's "mymaster")
password: ENV['REDIS_PASSWORD'],
role: :master,
sentinels: SENTINELS
}
end
# CLIENT CONFIGURATION
Sidekiq.configure_client do |config|
if Rails.env.development?
config.redis = { url: ENV['REDIS_URL'], id: "Sidekiq-server-PID-#{::Process.pid}" }
elsif Rails.env.production?
config.redis = {
url: 'redis://mymaster', # when using sentinels the name of the master can be found in the Redis logs (regularly it's "mymaster")
password: ENV['REDIS_PASSWORD'],
role: :master,
sentinels: SENTINELS
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment