|
18 | 18 |
|
19 | 19 | require 'active_support/core_ext/integer/time' |
20 | 20 |
|
| 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 | + |
21 | 58 | Rails.application.configure do |
22 | 59 | # Settings specified here will take precedence over those in config/application.rb. |
23 | 60 |
|
|
111 | 148 | config.log_tags = [:request_id] |
112 | 149 |
|
113 | 150 | # 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 |
115 | 152 |
|
116 | 153 | # Use a real queuing backend for Active Job (and separate queues per environment). |
117 | 154 | config.active_job.queue_adapter = :async # TODO: Configure :resque |
|
0 commit comments