Skip to content

Commit be71b16

Browse files
committed
added support for Redis Sentinel
1 parent 3a810f9 commit be71b16

5 files changed

Lines changed: 60 additions & 10 deletions

File tree

app/controllers/health_checks_controller.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ def check_database
4545
end
4646

4747
def check_redis
48-
Redis.new.ping
48+
if Greenlight::Application.config.cache_store == :null_store
49+
Redis.new.ping
50+
else
51+
Redis.new(Greenlight::Application.config.cache_store[1].except(:adapter, :channel_prefix)).ping
52+
end
4953
rescue StandardError => e
5054
raise "Unable to connect to Redis - #{e}"
5155
end

bin/start

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ if [ "$RAILS_ENV" = "production" ]; then
1717
sleep 1
1818
done
1919

20-
while ! nc -zw3 $RDHOST $RDPORT
21-
do
22-
echo "Waiting for redis to start up ..."
23-
sleep 1
24-
done
20+
if [ -z "$REDIS_SENTINEL" ]; then
21+
while ! nc -zw3 $RDHOST $RDPORT
22+
do
23+
echo "Waiting for redis to start up ..."
24+
sleep 1
25+
done
26+
fi
2527
fi
2628

2729
rails assets:precompile

config/cable.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,9 @@ test:
2222

2323
production:
2424
adapter: redis
25-
url: <%= ENV.fetch("REDIS_URL", "") %>
25+
url: <%= ENV.fetch("REDIS_URL", nil) || "redis://mymaster" %>
26+
sentinels: <%= ENV.fetch("REDIS_SENTINEL", "") %>
27+
name: mymaster
28+
role: master
29+
namespace: greenlight3
2630
channel_prefix: greenlight_production

config/environments/production.rb

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,43 @@
1818

1919
require 'active_support/core_ext/integer/time'
2020

21+
class String
22+
def string_between_markers(marker1, marker2)
23+
self[/#{Regexp.escape(marker1)}(.*?)#{Regexp.escape(marker2)}/m, 1]
24+
end
25+
end
26+
27+
def parse_sentinels(sentinel_str)
28+
work_str = sentinel_str.gsub(/[[:space:]]/, '').delete_prefix('[').delete_suffix(']') # remove all white space and surrounding []
29+
sentinel_arr = []
30+
until work_str.empty?
31+
# cut string with hash content
32+
sub_str = work_str.string_between_markers('{', '}')
33+
# convert that string to hash (while converting keys to symbols and
34+
# values to integer, if possible, else make it string without single quotes)
35+
sub_arr = sub_str.split(',').map do |h|
36+
h1, h2 = h.split(':')
37+
{ h1.to_sym => h2.match?(/\D/) ? h2.delete("'") : h2.to_i }
38+
end
39+
sub_hash = sub_arr.reduce(:merge)
40+
sentinel_arr.append(sub_hash)
41+
# remove hash string from current working string and delete any surrounding commas
42+
work_str.slice! "{#{sub_str}}"
43+
work_str = work_str.delete_prefix(',').delete_suffix(',')
44+
end
45+
sentinel_arr
46+
end
47+
48+
def config_cache_store
49+
if ENV.fetch('REDIS_SENTINEL', nil).present?
50+
[:redis_cache_store, { sentinels: parse_sentinels(ENV.fetch('REDIS_SENTINEL')),
51+
url: ENV.fetch('REDIS_URL', nil) || 'redis://mymaster',
52+
name: 'mymaster', role: :master }]
53+
else
54+
[:redis_cache_store, { url: ENV.fetch('REDIS_URL', nil) }]
55+
end
56+
end
57+
2158
Rails.application.configure do
2259
# Settings specified here will take precedence over those in config/application.rb.
2360

@@ -111,7 +148,7 @@
111148
config.log_tags = [:request_id]
112149

113150
# Use a different cache store in production.
114-
config.cache_store = :redis_cache_store, { url: ENV.fetch('REDIS_URL', nil) }
151+
config.cache_store = config_cache_store
115152

116153
# Use a real queuing backend for Active Job (and separate queues per environment).
117154
config.active_job.queue_adapter = :async # TODO: Configure :resque

sample.env

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@ BIGBLUEBUTTON_SECRET=
1515
# E.g. postgres://postgres:password@postgres:5432/greenlight-v3-production
1616
DATABASE_URL=
1717

18-
### REDIS CACHE URL
19-
# Must be in the format redis://host:port
18+
### REDIS CACHE
19+
# REDIS_URL: Must be in the format redis://host:port
2020
# E.g. redis://redis:6379
21+
# REDIS_SENTINEL: Must be an array of host, port pairs. In this case leave REDIS_URL blank or set it to e.g. redis://mymaster.
22+
# E.g. [{host: 'host1', port: 26379}, {host: 'host2', port: 26379}, {host: 'host3', port: 26379}]
2123
REDIS_URL=
24+
REDIS_SENTINEL=
2225

2326
### OPTIONAL ENV VARS
2427

0 commit comments

Comments
 (0)